1 / 14

Object-oriented Programming in Java

Object-oriented Programming in Java. What is OOP?. The goal is (subtype) polymorphism Achieved by Classes (user-defined types) Inheritance (is-a, substitutability) Dynamic Function Binding Function to run is bound according to the dynamic type of the object. Upcasting.

alice-rivas
Download Presentation

Object-oriented Programming in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-oriented Programming in Java

  2. What is OOP? • The goal is (subtype) polymorphism • Achieved by • Classes (user-defined types) • Inheritance (is-a, substitutability) • Dynamic Function Binding • Function to run is bound according to the dynamic type of the object

  3. Upcasting • Storing a reference to a subtype in a supertype variable • Makes sense because of the is-a relationship • Allows any subtype object to be used in a context expecting a supertype object • Employee e = new SalariedEmployee();

  4. The instanceof operator • Returns true if the left operand is the same type or a subtype of the right operand • new HourlyEmployee() instanceof Employee is true • Often used before downcasting • To avoid an exception

  5. Polymorphism • Dynamic variation of behavior • According to an object’s dynamic type • Calls the function corresponding to the type the reference indicates at the moment • Invisibly to the program • Employee e = new HourlyEmployee();e.computePay();e = new SalariedEmployee();e.computePay(); // A different function! • Example: Figure 2

  6. Benefits of OOP • High degree of separation among components • Low coupling • Insulates components from changes in other components • Example: payroll program • Uses the Employee interface • The dynamic types are invisible

  7. Downcasting • When an object must be interpreted as a subtype • When extracting from a collection, for example • Must use with care • The object may not be the type you’re expecting • Checked at runtime • Examples: Figures 4, 5

  8. Abstract Classes • Not meant to be instantiated • Define an interface • Also define part of the implementation • Subclasses complete the implementation • abstract keyword • Both for classes and methods

  9. java.lang.Object • The Mother of all Classes • All objects are subtypes of Object • Object methods: • int hashCode( ); • boolean equals(Object); • String toString( ); • Among others…

  10. Object.toString • Provides a String representation of an object • Prints the class name and the hashCode • Should override

  11. Object.equals(Object) • Used for a value-based equality test • Override carefully: • Test == this first • if true, return true • Test instanceof TheClass • if false, return false • Then check all the fields • Use equals() recursively for object fields • Can super.equals() for inherited fields • Examples: ColorPoint (next slide) and SuperEquals.java • Always override hashCode() if you override equals()

  12. ColorPoint public boolean equals(Object other) { if (this == other) return true; else if (!(other instanceof ColorPoint)) return false; ColorPoint p = (ColorPoint) other; return (super.equals(p) && p.color.equals(this.color)); } Instance variables in parent class, Point: Instance variable in child class, ColorPoint: int x, y Color color

  13. Floating-point Fields • Don’t compare for equality with ==! • Has to do with special values (NaN, etc.) • Convert floats to int with Float.floatToIntBits • Convert doubles to long with Double.doubleToLongBits • Compare the resulting integers with ==

  14. hashCode • Used to associate an object with an int • Used in hash tables and other containers • Should be “as unique as possible” • Rules: • Boolean: (f ? 0 : 1) • byte, char, or short: (int) f • Long: (int)(f ^ (f>>>32)) • Float: Float.floatToIntBits(f) • Double: Double.doubleToLongBits(f) • Combine with prime numbers: h = 37*f1; h = 37*h;… • Objects: combine their hashcodes (see pp. 197-198)

More Related