Inheritance and Polymorphism Chapter 11 1 For Next Time Read - - PowerPoint PPT Presentation

inheritance and polymorphism
SMART_READER_LITE
LIVE PREVIEW

Inheritance and Polymorphism Chapter 11 1 For Next Time Read - - PowerPoint PPT Presentation

Inheritance and Polymorphism Chapter 11 1 For Next Time Read Chapter 11 2 New classes from existing classes public class NewClass extends ExistingClass { /* . . . */ } NewClass is the subclass ; ExistingClass is the superclass


slide-1
SLIDE 1

1

Inheritance and Polymorphism

Chapter 11

slide-2
SLIDE 2

2

For Next Time

 Read Chapter 11

slide-3
SLIDE 3

3

New classes from existing classes

public class NewClass extends ExistingClass { /* . . . */ }

NewClass is the subclass; ExistingClass is the superclass NewClass inherits everything from ExistingClass NewClass can add new features

slide-4
SLIDE 4

4

Other terminology

 Superclass is to subclass as

Base class is to derived class Parent class is to child class General class is to specific class

 Inheritance is also called

subclassing specialization extension

slide-5
SLIDE 5

5

Example

Extend the traffic light class to add a left turn arrow [( ) ( ) ( ) (<) ]

slide-6
SLIDE 6

6

Visualizing inheritance

The Unified Modeling Language (UML)

slide-7
SLIDE 7

7

The is a relationship

An instance of a subclass is a instance of a superclass as well

A TurnLightModel object is a TrafficLightModel object, since TurnLightModel is a subclass of TrafficLightModel

An instance of a subclass can be used in any context that requires an instance of a superclass of that object

slide-8
SLIDE 8

8

Is a transitivity

slide-9
SLIDE 9

9

Polymorphism

 When a method is called on an object reference, the code that executes depends on the type of the object  Because of inheritance and the is a relation, we cannot always know at compile-time what code will be executed when a method is called  Polymorphism is the runtime determination of which method to execute based on the exact type of the object on whose behalf the the method is called

slide-10
SLIDE 10

10

Next . . .

Some standard classes . . .