Download presentation
Presentation is loading. Please wait.
1
Advanced Java Programming
Session #, Speaker Name Advanced Java Programming CSS446 Spring 2014 Nan Wang 08/24/11
2
Chapter Goals To learn about inheritance
To implement subclasses that inherit and override superclass methods
3
Inheritance Hierarchies
Inheritance is a relationship between a more general class (called the superclass) and a more specialized class (called the subclass).
4
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);
5
Example
6
Implementing Subclasses
Subclass inherits all methods that it does not override.
7
choiceQuestion.setAnswer("2");
question.addChoice("Canada", true); setAnswer(choiceString);
8
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.
9
Override display() in subclass
Display the question text. Display the answer choices. Use the reserved word super to call a superclass method
10
Constructor with Superclass Initializer
12
Override vs. Overload
13
Program Assignment 2
14
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.