1 / 21

Advanced Java Programming

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.

dianae
Download Presentation

Advanced Java Programming

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. Advanced Java Programming CSS446 Spring 2014 Nan Wang

  2. Chapter Goals • To understand the concept of polymorphism • To be familiar with the common superclass Object and its methods

  3. Polymorphism • Processing objects of different types in the same program

  4. 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.

  5. Polymorphism

  6. Question • Is the method call Math.sqrt(2) resolved through dynamic method lookup?

  7. Dynamic Method Lookup and the Implicit Parameter

  8. 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.

  9. 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.

  10. 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

  11. 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.

  12. 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!

  13. 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.

  14. Excellent Video Tutorial • http://www.youtube.com/watch?v=Lsdaztp3_lw

  15. 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

  16. 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)

  17. Overriding the toString()

  18. 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.

  19. Overriding the equals() method • If you know that an object belongs to a given class, use a cast to convert the type.

  20. The instanceof Operator • The instanceof operator tests whether an object belongs to a particular type.

  21. 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; } }

More Related