Presentation is loading. Please wait.

Presentation is loading. Please wait.

Two big words Inheritance Polymorphism

Similar presentations


Presentation on theme: "Two big words Inheritance Polymorphism"— Presentation transcript:

1 Two big words Inheritance Polymorphism
Defining a class that, by default, has all the same behavior of some other (parent) class We can re-define some of the parent functions/attributes We can add new behaviors and attributes to supplement the original Polymorphism We can take advantage of the fact that all of a certain set of classes have common attributes/behaviors Can avoid redundancy by specifying requirements to be in the particular class

2 Subclasses Used to add member functions/attributes to existing classes, without modifying the original Syntax:

3 Subclasses Naturally, we can apply the same principle to subclasses, extending them further This creates a class-hierarchy

4 Subclasses In the left diagram: A is the superclass of B, C, D, and E
All others have all members of A B, C, and D share existing attributes/behaviors of A, but may have totally independent features E inherits from both D and A

5 Subclasses Example: vehicle database
What are the defining attributes of a vehicle? Color Make Model Number of wheels Variable, but can also be an indication of more fine-grained classification (see right)

6 Vehicles Idea: Can add more specific attributes:
Define the vehicle class to have the attribute: int numberOfWheels; In the subclasses, we can specify exactly how many in the constructor or class definition Can add more specific attributes: Car: numberOfDoors Truck: numberOfAxels Motorcycle: hasSideCar

7 Class and Subclass Variables
Very Important Fact An variable that can hold a reference to an object of class A can also hold a reference to an object of any subclass of A. E.g. if “Car” is a subclass of “Vehicle”, the following is legal: Vehicle v; Car c = new Car(); v = c; // We don’t even lose the Car information!

8 Polymorphism With some planning, we can use some objects/classes in our program without knowing exactly what they are at runtime (!!!) Recall that: Very Important Fact: any variable that holds a reference to a class can also hold a reference to any of its subclasses Subclasses can override base class members

9 Polymorphism Architectural idea
Write functions to accept the most general type of parameter possible, assuming all subclasses have responsibly overridden any necessary methods We have seen this already in the form of the Object.toString() method override!

10 Polymorphism Example: Example 2:
In our vehicle example: make sure each vehicle defines its own “print()” method Add a method in the driver class to call “.print()” on a Vehicle parameter Example 2: Add an “upgrade()” method to the Vehicle class Do nothing Override in each subclass, upgrading where appropriate Upgrade some vehicles, then print


Download ppt "Two big words Inheritance Polymorphism"

Similar presentations


Ads by Google