You are on page 1of 16

Inheritance

CS F213: Object Oriented Programming

Dipanjan Chakraborty
Department of CS&IS

1
Advantages and Features

Cornerstones of OOP

Allows creation of hierarchical classifications

General Class defines common features
– Called Superclass in Java

Specific classes inherit the general class and add features which are
unique to them
– Called Subclass in Java

Example: a student is a subclass of a person

2
Advantages and Features

Java Syntax:
– Class B extends A {}


Java does not support multiple inheritance
– This is not allowed:
● class B extends A, C {}


Subclass includes all members of superclass, but can not
access private members unlike nested class

3
The super keyword

Can call the super class’ constructor:
super(args)

Can pass object of same type to super class’
constructor:
SubClass(Subclass sc) {
super(sc);
}

4
The super keyword

Can be used to refer to the super class’ non-
private members (data and methods)

Specially useful when the superclass members
are hidden (usage of same name in subclass
and super class

5
Inheritance

Extend the Rectangle class to create a Square
class

6
class square extends rectangle
Subclass References
{
}
Square s = new Square(5);
Rectangle r = new Rectangle();
r = s;
r.diagonal = 9;

● Add a data member int diagonal in the Square


class. Can an object of the Rectangle class, r, refer
to the member diagonal?
– No. The Rectangle class does not have a data
member diagonal
7
Multi-level Hierarchy
● class A {}
● class B extends A {}
● class C extends B {}

C inherits all traits of both A and B

Visualise a hierarchy of classes from polygons to squares, triangles, etc.

Create a class People, class Faculty extends People and class Student extends
People

What attributes and methods would you define?

super() in Multi-level inheritance refers to the closest superclass

8
Multi-level Hierarchy

In which order are constructors called?
– In the order of derivation
– If class A {}
– class B extends A {}
– class C extends B {}
– A(), B(), C() are called in that order

Let’s look at an example

9
Method Overriding

In a class hierarchy, when a method in a subclass has the same name and type
signature as a method in its superclass, then the method in the subclass is said to
override the method in the superclass

When an overridden method is called through its subclass, it will always refer to the
version of that method defined by the subclass

The version of the method defined by the superclass will be hidden
● Superclass’ hidden method may be executed using the super keyword super.meth_name


Method overriding occurs only when the names and the type signatures of the two
methods are identical. If they are not, then the two methods are simply overloaded.

Note: what is the difference between overloading and overriding?
Overloaded methods must have the same name but different parameter lists, and they can differ in return type, access modifiers, and exceptions thrown.

Overloading occurs within the same class or in a subclass. resolved at compile time

Method overriding allows a subclass to provide a specific implementation of a method that is already defined in its superclass.
10
Overridden methods must have the same name, parameter list, and return type as the method in the superclass.
Dynamic Method Dispatch

Runtime polymorphism

A call to an overridden method is resolved at
run time, rather than at compile time

The type of object being referred to, rather than
the reference variable type, determines which
version of the overridden method is called

11
Dynamic Method Dispatch
1
2
Square s = new Square();
Rectangle r = new Rectangle();

In line 6, Although r is
3
4 s.getArea();
of type Rectangle, it
5
6
r.getArea();
r = s;
refers to an object of
7 r.getArea(); type Square.

So s.getArea() would
be trigered in line 7.

12
Abstract Classes

Sometimes superclasses might declare the interface
for a method without defining it

It is left to the subclasses to define the
implementation

Example: area for a polygon would be undefined, but
for a triangle or a rectangle would be defined
separately

13
Abstract Classes and Methods
● Abstract methods must be declared with the keyword abstract
– abstract type name(parameter-list);

Any class that contains one or more abstract methods must also be declared abstract
– abstract class A {}


There can be no objects of an abstract class

There can not be abstract constructors or abstract static methods. Abstract classes can
contain concrete methods in addition to abstract methods

Any subclass of an abstract class must either implement all of the abstract methods in the
superclass, or be declared abstract itself.

Because overridden method resolution happens at the run time, abstract classes can be used
to create object references

Let’s take the example of classes Polygon, Rectangle, Triangle
14
final keyword for methods and
classes

Recap:
– methods declared final can not be
overridden
– classes declared final can not be inherited

15
The Object Class

The slides have been misplaced. Please follow
the book

16

You might also like