220 likes | 260 Views
Explore polymorphism in Java programming, including dynamic method lookup, abstract classes, final methods, inheritance hierarchy, and object-oriented concepts. Learn how to override methods like toString() and equals() for custom object comparison.
E N D
Advanced Java Programming CSS446 Spring 2014 Nan Wang
Chapter Goals • To understand the concept of polymorphism • To be familiar with the common superclass Object and its methods
Polymorphism • Processing objects of different types in the same program
Polymorphism • Which display() method is called determined by contents of parameter variable q. • In Java, method calls are always determined by the type of the actual object, not the type of the variable containing the object reference. This is called dynamic method lookup. • Dynamic method lookup allows us to treat objects of different classes in a uniform way. This feature is called polymorphism.
Question • Is the method call Math.sqrt(2) resolved through dynamic method lookup?
Abstract Methods • Let us force programmers to override the method. • An abstract method has no implementation. This forces the implementors of subclasses to specify concrete implementations of this method.
Abstract Classes • You can not construct objects of classes with abstract methods. • A class for which you can not create objects is called an abstract class. • A class for which you can create objects is called a concrete class.
If a class extends an abstract class with out providing an implementation of all abstract methods, it is abstract class too!! • You can not construct an object of an abstract class, but you still have an object reference whose type is an abstract class, NOTE: the actual object to which it refers must be an instance of a concrete subclass
Final Methods and Classes • Abstract method and class, you force programmer to create a subclasses of abstract class and override abstract methods. • Final method and class, you prevent programmers from creating subclasses or overriding final methods.
Protected Access • Protected data in an object can be accessed by the methods of the object’s class and all its subclasses. • BUT, it is not good choice!!! Avoid it!!! • Use getter and setter instead!
Developing an Inheritance Hierarchy • Step 1: List the classes that are part of the hierarchy. • Step 2: Organize the classes into an inheritance hierarchy. • Step 3: Determine the common responsibilities. • Step 4: Decide which methods are overridden in subclasses. • Step 5: Declare the public interface of each subclass. • Step 6: Identify instance variables. • Step 7: Implement constructors and methods. • Step 8: Construct objects of different subclasses and process them.
Excellent Video Tutorial • http://www.youtube.com/watch?v=Lsdaztp3_lw
Object • Very general methods: • toString(): yields a string descripting the object • equals(): compares objects with each other • hashCode(): yields a numerical code for storing the object in a set
Object class: toString() • toString() yields a string describing the object • Numbers are not objects, and there is no toString() method for them. • Name of class followed by the hashCode (hashCode is used to tell objects apart, different objects have different hash codes)
equals() method • the equals() method checks whether two objects have the same contents. • == operator tests whether two references are identical, referring to the same object.
Overriding the equals() method • If you know that an object belongs to a given class, use a cast to convert the type.
The instanceof Operator • The instanceof operator tests whether an object belongs to a particular type.
public boolean equals(Object otherObject) { if (otherObject == null) { return false; } if (getClass() != otherObject.getClass()) { return false; } Stamp other = (Stamp) otherObject; return color.equals(other.color) && value == other.value; } public CollectibleStamp extends Stamp { private int year; . . . public boolean equals(Object otherObject) { if (!super.equals(otherObject)) { return false; } CollectibleStamp other = (CollectibleStamp) otherObject; return year == other.year; } }