24
Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno

Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno

  • View
    218

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno

Huron High SchoolAP Computer Science

Instructor: Kevin BehmerS. Teacher: Guillermo Moreno

Page 2: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno

Inheritance

How to establish inheritance relationships among classes?

Code reuse can be achieved by this technique?

Page 3: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno

What is “extends”?

What is variable hiding?

Occurs when a variable in one class has the same name as a variable in a superclass.

Page 4: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno

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?

Page 5: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno

The “super” keyword

Used to invoke a superclass method or constructor.

Public TokenTest() {super(“Testing Class”);. . .

Page 6: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno

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.

Page 7: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno

Syntax:

class ClassName2 extends ClassName1 {

// class body

}

Page 8: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno

Objects:

A class may directly extend only one superclass.

However, it may have several subclasses.

Each subclass may itself have several subclasses.

Page 9: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno

To declare a variable

To declare a variable that references an object:

ClassName variableName;

Page 10: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno

To summarize:

A superclass reference can refer to an object of its class or any object derived from that class.

Page 11: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno

Class Burger {}class Hotdog extends Burger {}class Inheritance {

public static void main(..) {Burger cheese;System.out.println(“Instantiating”);cheese = new Burger();}

Page 12: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno

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;

Page 13: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno

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); }

Page 14: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno

Class PopulationUpdate {public static void

main(..) {Seoul pp = new

Seoul();pp.display();}

}

Page 15: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno

Output:

pp = 10 331 244

pp = 48 289 037

Page 16: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno
Page 17: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno
Page 18: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno
Page 19: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno
Page 20: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno
Page 21: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno
Page 22: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno
Page 23: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno
Page 24: Huron High School AP Computer Science Instructor: Kevin Behmer S. Teacher: Guillermo Moreno