0% found this document useful (0 votes)
8 views36 pages

Topic 09 - Inheritance in Java

Uploaded by

kusaltharindu739
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views36 pages

Topic 09 - Inheritance in Java

Uploaded by

kusaltharindu739
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 36

ITC 1233 - OBJECT ORIENTED

PROGRAMMING
OBJECT ORIENTED CONCEPT 2: INHERITANCE

Ms.Nirasha Kulasooriya,
Lecturer, Dept. of ICT, FOT,USJ.
HAVE YOU HEARD ABOUT INHERITANCE?
SO, WHAT WOULD BE THE MAIN IMPORTANCE OF
USING INHERITANCE?
INHERITANCE
INHERITANCE
• A mechanism in which one object acquires properties
and behaviours of its parent object.

• The idea behind inheritance in Java is that you can


create new classes that are built upon existing classes.
When you inherit from an existing class, you can reuse
methods and attributes of its parent class, and you can
add new methods and attributes too.
INHERITANCE
• Inheritance represents an IS-A relationship or a

parent-child (superclass-subclass) relationship.


With inheritance –
• code reuse
• method overriding (polymorphism)
INHERITANCE
• Super-class is the class that is being inherited from.

• Sub-class inherits from another class.

• To inherit from a class, use the extends keyword.


EXAMPLE: INHERITANCE
ACTIVITY 01
• Think on a real world scenario and explain how inheritance
can be involved for the functioning.

• Explain 02 advantages regarding the scenario chosen.


EXAMPLE 01: INHERITANCE
Vehicle.java

Car.java
EXAMPLE 01: INHERITANCE
Car inherits the colour attribute and the accelerate method
from its super-class Vehicle.
Test.java
EXAMPLE 01: INHERITANCE
When the car object is created, the default constructor in the
Vehicle super-class is executed first, then the default constructor
in the Car sub-class.
EXAMPLE 02: INHERITANCE
Vehicle.java
EXAMPLE 02: INHERITANCE
Car.java
EXAMPLE 02: INHERITANCE
super keyword can be used to access the attributes and
methods of an immediate super-class (Vehicle) from a
sub-class (Car).
Test.java
EXAMPLE 02: INHERITANCE
When the car object is created:
1. The control goes to the sub-class (Car) constructor, but, the body is
not executed.
2. Then the control goes to the super-class (Vehicle) default constructor,
and the body of it gets executed.
3. Then the control comes back to the sub-class (Car) constructor and
its body gets executed.
ACTIVITY 02
• Simply build up a program achieving inheritance for the
scenario you have chosen in activity 01.
CONSTRUCTOR CALL ORDER - SINGLE INHERITANCE
CONSTRUCTOR CALL ORDER - SINGLE INHERITANCE
When the child object is created:
1. The control goes to the sub-class (Child) constructor, but, the body is

not executed.

2. Then the control goes to the super-class (Parent) constructor, and the

body of it gets executed.

3. Then the control comes back to the sub-class (Child) constructor and

its body gets executed.


“Super” KEYWORD
Keyword super in Java refers to the immediate parent class
object.

Some of the usages of the super keyword are listed


below:
• Refer instance variables of the immediate parent class object.
• Invoke instance methods of the immediate parent class object.
• super() can be used to invoke immediate parent class constructor.
EXAMPLE 03: INHERITANCE
Vehicle.java
EXAMPLE 03: INHERITANCE
Car.java
EXAMPLE 03: INHERITANCE
Since ‘colour’ is a private instance variable, it cannot be accessed
by the Car sub-class. Private attributes or methods cannot be
accessed from outside of the class that they are declared in.
EXAMPLE 03: INHERITANCE
Test.java
EXAMPLE 03: INHERITANCE
When the car object is created:
1. The control goes to the sub-class (Car) constructor, but, the body is not
executed.
2. Then the control goes to the super-class (Vehicle) constructor, and the
body of it gets executed.
• In this case, the super-class constructor is explicitly called using super()
• In all the examples before, the super-class constructor was called implicitly.

3. Then the control comes back to the sub-class (Car) constructor and its
body gets executed.
INHERITANCE ACCESS MODIFIERS
Attributes/methods in a class that have the access modifier(s) –
• private: cannot be accessed from its sub-classes.
• public/protected: can be accessed from its sub-classes.
• default: can be accessed from the sub-classes in the same
package, but cannot if the sub-classes are in a different
package.
INHERITANCE ACCESS MODIFIERS
TYPES OF INHERITANCE
MULTILEVEL INHERITANCE
HIERARCHICAL INHERITANCE
HIERARCHICAL INHERITANCE
HIERARCHICAL INHERITANCE
HIERARCHICAL INHERITANCE
ANY QUESTIONS?

You might also like