Advanced Java Programming Session #, Speaker Name Advanced Java Programming CSS446 Spring 2014 Nan Wang 08/24/11
Chapter Goals To learn about inheritance To implement subclasses that inherit and override superclass methods
Inheritance Hierarchies Inheritance is a relationship between a more general class (called the superclass) and a more specialized class (called the subclass).
Inheritance Hierarchies The subclass inherits data and behavior from the superclass. You can always use a subclass object in place of a superclass object. E.g. void processVehicle(Vehicle v) Because Car is a subclass of Vehicle, you can call that method with a Car object: Car myCar = new Car(. . .); processVehicle(myCar);
Example
Implementing Subclasses Subclass inherits all methods that it does not override.
choiceQuestion.setAnswer("2"); question.addChoice("Canada", true); setAnswer(choiceString);
Implementing Subclasses Override: Subclass can override a superclass method by providing a new implementation. An overriding method can extend or replace the functionality of the superclass method. Display the question text. Display the answer choices.
Override display() in subclass Display the question text. Display the answer choices. Use the reserved word super to call a superclass method
Constructor with Superclass Initializer
Override vs. Overload
Program Assignment 2
Program Assignment 2 Due Date: 1/30/2014 Submit to usm.java@gmail.com P1: 100 points Bonus: P2: 20 points and P3 30 points