Download presentation
Presentation is loading. Please wait.
Published byDaniela Andra Gilmore Modified over 9 years ago
1
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism
2
2 Review: The Java mechanism for defining a inheritance relationship between two classes is called extension: In Dog.java: public class Dog{…} In Beagle.java: public class Beagle extends Dog{…} The extends keyword establishes the inheritance relationship extends means “is a kind of” SE-1020 Dr. Mark L. Hornick
3
Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 3 Polymorphism allows a single variable to refer to objects from different subclasses in the same inheritance hierarchy For example, if Beagle is a subclass of Dog, then the following statements are valid: Dog myPet; myPet = new Dog(“rex”,3); // myPet can also reference a subclass of Dog myPet = new Beagle(“rusty”, 1);
4
Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 4 Polymorphism also allows the JVM to determine (at runtime) which specific implementation of a method should be called For example, if Beagle overrides the speak() method of Dog, the JVM will use late-binding to call Beagle’s speak() method: Dog myPet; myPet = new Dog(“rex”,3); myPet.speak(); // call Dog’s speak() method // myPet can also reference a subclass of Dog myPet = new Beagle(“rusty”, 1); myPet.speak(); // call Beagle’s speak() method
5
Portions adapted with permission from the textbook author. SE-1020 Dr. Mark L. Hornick 5 Sometimes, a derived class may want to use the base class implementation AND provide some additional behavior of its own Within Dog class: public void speak(){ // Dog’s method System.out.println(“Woof”); } Within a Beagle class: public void speak(){ // Beagle’s method super.speak(); // invoke Dog’s speak() System.out.println(“hooowwwl”); }
6
SE-1020 Dr. Mark L. Hornick 6 The instanceof operator can help us learn the class of an object The following code counts the number of Beagles in a collection of Dogs int howlerCount = 0; for ( Dog d: kennel ) { // kennel is a ArrayList if ( d instanceof Beagle ) { howlerCount++; }
7
Polymorphism allows us to use a derived class anywhere we would normally use a base class Portions adapted with permission from the textbook author. CS-1020 Dr. Mark L. Hornick 7 ArrayList kennel = new ArrayList (); kennel.add( new Dog(“rex”, 3) ); // add a Dog kennel.add( new Dog(“sam”, 2) ); // add another Dog // The add() method nomally takes a Dog reference, but… kennel.add( new Beagle( “rusty”, 1) ); // add a Beagle
8
SE-1020 Dr. Mark L. Hornick 8 Every Java class has a common ancestor: the Object class If a class declaration does not explicitly designate the superclass with the extends clause, then the class’s superclass is the Object class public class SomeClass { … } Is the same as: public class SomeClass extends Object { … }
9
SE-1020 Dr. Mark L. Hornick 9 Because of inheritance, every Java class ultimately extends Object The Object class is the source of the following methods which every Java class inherits: clone() – makes a copy of the object equals() – compares an object to another object toString() – converts an object to its String representation and several others (see the Javadoc for the Object class)
10
The toString() method The Object class provides a default implementation of toString(), which is why you can always call it for any class If you create classes that don’t override toString(), you’ll inherit Object’s default implementation Which just prints out the class name and hashcode (related to the object’s memory address) Many Java API classes override toString() in order to provide an implementation that makes more sense CS-1020 Dr. Mark L. Hornick 10
11
The equals() method The Object class provides a default implementation of equals(), which is why you can always call it for any class If you create classes that don’t override equals(), you’ll inherit Object’s default implementation Which just compares memory addresses Many Java API classes override equals() in order to provide an implementation that makes more sense CS-1020 Dr. Mark L. Hornick 11
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.