View
218
Download
0
Tags:
Embed Size (px)
Citation preview
Huron High SchoolAP Computer Science
Instructor: Kevin BehmerS. Teacher: Guillermo Moreno
Inheritance
How to establish inheritance relationships among classes?
Code reuse can be achieved by this technique?
What is “extends”?
What is variable hiding?
Occurs when a variable in one class has the same name as a variable in a superclass.
Method overriding
Occurs when a method in one class has the same type signature as a method in a superclass.
What is run-time polymorphism?
The “super” keyword
Used to invoke a superclass method or constructor.
Public TokenTest() {super(“Testing Class”);. . .
Object Oriented Programming
Inheritance allows one class to reuse the functionality provided by its superclasses.
The extends clause in a class declaration establishes an inheritance relationship.
Syntax:
class ClassName2 extends ClassName1 {
// class body
}
Objects:
A class may directly extend only one superclass.
However, it may have several subclasses.
Each subclass may itself have several subclasses.
To declare a variable
To declare a variable that references an object:
ClassName variableName;
To summarize:
A superclass reference can refer to an object of its class or any object derived from that class.
Class Burger {}class Hotdog extends Burger {}class Inheritance {
public static void main(..) {Burger cheese;System.out.println(“Instantiating”);cheese = new Burger();}
The super keyword
The super keyword enables you to access the superclass variable and construct a subclass by using the same variable name.
super.variableName;
class Korea {int pp = 48289037;}class Seoul extends Korea {int pp = 10331244 ;void display() {Sys.out.println(“pp=“ + pp);Sys.out.println(“pp=“ +
super.pp); }
Class PopulationUpdate {public static void
main(..) {Seoul pp = new
Seoul();pp.display();}
}
Output:
pp = 10 331 244
pp = 48 289 037