20
Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common features. First we define a class that contains the common features of the entities. Then we define classes as an extension of the common class The common class is called the superclass and all classes that inherit from it subclasses. The superclass is an ancestor of the descendant subclass.

Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common

Embed Size (px)

Citation preview

Page 1: Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common

Inheritance

In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common features.

First we define a class that contains the common features of the entities. Then we define classes as an extension of the common class

The common class is called the superclass and all classes that inherit from it subclasses. The superclass is an ancestor of the descendant subclass.

Page 2: Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common

Sample Inheritance HierarchyAccount

Savings Checking

SuperSaver Regular StudentInterestBearing

ATMChecking

Page 3: Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common

Defining Classes with Inheritance

Suppose we want to model graduate and undergraduate students in maintaining a class roster.

For both types of students we keep their name, three test scores, and the final course grade. The formula for deriving the final course grades are different for undergraduate and graduate students.

How shall we implement the two types of students?

Page 4: Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common

Student with Two Subclasses

GraduateStudent

computeGrade

UndergraduateStudent

computeGrade

Student

Student

setTestScore

getTestScore

NUM_OF_TESTS

3

courseGrade

name

test[ ]…

We will define three classes:

StudentGraduateStudentUndergraduateStudent

We will define three classes:

StudentGraduateStudentUndergraduateStudent

Implementation of computeGrade is unique to each subclass.

Implementation of computeGrade is unique to each subclass.

Page 5: Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common

Inheritance

Subclasses inherit from their superclass

Everything that's in the superclass, and not in the subclass, is inherited by the subclass

If a subclass defined something that is in the superclass, the subclass elements override the superclass elements

Objects created from the subclass consist of the inherited components and any new components defined in the subclass

Page 6: Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common

Accessng Superclass Data and Methods

Constructors of a subclass can call constructors of their superclass using super()

If a subclass constructor does not call the superclass constructor, then the compiler adds a call.

Components of a superclass that have been overridden in a subclass can be accessed using super.<the component>

Page 7: Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common

Example Program

Ding … out to reality … Student.java, GraduateStudent.java, UndergraduateStudent.java, UseStudent1.java

Page 8: Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common

Inheritance and Member Accessibility

Which members of a superclass are accessible from its subclasses?

There is a third visibility modifier called protected.

public

private

protected

Superclass

Subclass

Page 9: Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common

Access from the Subclass Methods

Public and protected elements of the superclass (objects) are visible to its subclasses (objects).

mySub

Subclass

Superclass

inaccessible

accessible

Page 10: Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common

Access from the Outside

Only the public elements are accessible from the outside objects.

mySub

Subclass

Superclass

mySuper

Superclass

Client

test

Page 11: Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common

Access from Another Instance

All elements of an object are accessible from other instances of the same class.

anInstance

AClass

anotherInstance

AClass

Page 12: Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common

Example Program

Dong … out to reality … HouseOccupant.java, Human.java, Pet.java, UseHouseOccupant.java

Page 13: Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common

Polymorphism

Polymorphism allows a variable to refer to objects from different (but related by inheritance) classes.

References to data or methods are correctly resolved according to the object’s class.

Requires that the superclass have the inherited data or method

Student student;

student = new GraduateStudent();

student.computeGrade( );

Student student;

student = new UndergraduateStudent();

student.computeGrade( );

This will call the method of GraduateStudent

This will call the method of GraduateStudent

This will call the method of UndergraduateStudent

This will call the method of UndergraduateStudent

Page 14: Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common

Creating the roster Array

Student roster[ ] = new Student[40]; roster is an array of Student objects.

roster is an array of Student objects.

Create instances of the subclasses of Student.

Create instances of the subclasses of Student.

roster[0] = new GraduateStudent( );roster[1] = new UndergraduateStudent( );roster[2] = new UndergraduateStudent( );roster[3] = new GraduateStudent( );

0 1 2 3 4 36 37 38 39roster

Graduate-Student

Under-graduate-Student

Under-graduate-Student

Graduate-Student

Page 15: Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common

Processing the roster Array

for (int i = 0; I < numberOfStudents; i++ ) {

roster[i].computeGrade( );}

Use instanceOf to determine the class the object belongs to.

Use instanceOf to determine the class the object belongs to.

int undergradCount = 0;for (int i = 0; i < numberOfStudents; i++ ) {

if ( roster[i] instanceOf UndergraduateStudent ) {

undergradCount++;}

}

Notice how the polymorphism is usedhere.

Page 16: Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common

Example Programs

Dung … out to reality … Student.java, GraduateStudent.java, UndergraduateStudent.java, UseStudent2.java

Page 17: Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common

Abstract Classes

Often we want to place elements common to all subclasses in their superclass.

But we do not want any instances to be created from the superclass.

In such case, we designate the superclass as an abstract class.

This is also a way to enforce existence of methods in subclasses.

Page 18: Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common

Abstract Methods

It is common for an abstract class to include an abstract method that must be implemented by the descendant classes.

If a class includes an abstract method, then it is considered as an abstract class and must have the abstract modifier in the class declaration.

Page 19: Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common

Sample Abstract Class

Abstract method has no method body.

Abstract method has no method body.

abstract class Student{

. . .abstract public void computeGrade( );. . .

}

Reserved word abstract in the classdeclaration.

class GraduateStudent extends Student{

. . .public void computeGrade( ){

//method body comes here}. . .

}

Abstract method is must be fully implemented in the descendant class.

Abstract method is must be fully implemented in the descendant class.

Page 20: Inheritance In object-oriented programming, a mechanism called inheritance is used to design two or more entities that are different but share many common

Example Programs

Deng … out to reality … H2O.java, Ice.java, Water.java, Steam.java, UseH2O.java