Download presentation
Presentation is loading. Please wait.
1
Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno
2
Inheritance How to establish inheritance relationships among classes? Code reuse can be achieved by this technique?
3
What is “extends”? What is variable hiding? Occurs when a variable in one class has the same name as a variable in a superclass.
4
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?
5
The “super” keyword Used to invoke a superclass method or constructor. Public TokenTest() { super(“Testing Class”);...
6
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.
7
Syntax: class ClassName2 extends ClassName1 { // class body }
8
Objects: A class may directly extend only one superclass. However, it may have several subclasses. Each subclass may itself have several subclasses.
9
To declare a variable To declare a variable that references an object: ClassName variableName;
10
To summarize: A superclass reference can refer to an object of its class or any object derived from that class.
11
Class Burger { } class Hotdog extends Burger { } class Inheritance { public static void main(..) { Burger cheese; System.out.println(“Instantiating”); cheese = new Burger(); }
12
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;
13
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); }
14
Class PopulationUpdate { public static void main(..) { Seoul pp = new Seoul(); pp.display(); } }
15
Output: pp = 10 331 244 pp = 48 289 037
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.