0% found this document useful (0 votes)
7 views

Demo Presentation

Inheritance in Java is a key concept of Object-Oriented Programming that allows one class to inherit features from another class. It includes important terminology such as superclass and subclass, and supports reusability of code. Various types of inheritance are discussed, including single, multilevel, multiple, hierarchical, and hybrid inheritance, along with examples for each type.

Uploaded by

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

Demo Presentation

Inheritance in Java is a key concept of Object-Oriented Programming that allows one class to inherit features from another class. It includes important terminology such as superclass and subclass, and supports reusability of code. Various types of inheritance are discussed, including single, multilevel, multiple, hierarchical, and hybrid inheritance, along with examples for each type.

Uploaded by

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

Java Programming

“Inheritance”

Sidra Rani
Riphah School of Computing and Innovation
What is Inheritance

Inheritance is an important pillar of


OOP(Object-Oriented Programming). It
is the mechanism in java by which one
class is allowed to inherit the
features(fields and methods) of another
class.
Important terminology:
 Super Class: The class whose features are
inherited is known as superclass(or a base
class or a parent class).
 Sub Class: The class that inherits the other
class is known as a subclass(or a derived
class, extended class, or child class).
 Reusability: Inheritance supports the
concept of “reusability”, i.e. when we want
to create a new class and there is already a
class that includes some of the code that
we want, we can derive our new class from
the existing class
How to use inheritance in Java

The keyword used for inheritance


is extends.
Syntax :
class derived-class extends base-class
{
//methods and fields
}
Types of Inheritance in Java

Single Inheritance
Multilevel Inheritance
Multiple Inheritance
Hierarchal Inheritance
Hybrid Inheritance
Single inheritance
When a class extends another
one class only then we call it a
single inheritance. The below flow
diagram shows that class B
extends only one class which is A.
Here A is a parent class of B and
B would be a child class of A.
Multiple Inheritance
"Multiple Inheritance" refers to
the concept of one class
extending (Or inherits) more than
one base class.
The problem with "multiple
inheritance" is that the derived
class will have to manage the
dependency on two base classes.
Multilevel inheritance
Multilevel inheritance refers to a
mechanism in which one class
can inherit from a derived class,
thereby making this derived class
the base class for the new class.
As you can see in below flow
diagram C is subclass or child
class of B and B is a child class of
A.
Hierarchical
Inheritance
In such kind of inheritance one
class is inherited by many sub
classes.In below example class
B,C and D inherits the same class
A.A is parent class (or base
class) of B,C & D.
Single Inheritance
Example:
Class A
{
Public void methodA ()
{
System.out.println("Base class method”); }
}
Class B extends A
{
public void methodB()
{
System.out.println("Child class method");}

public static void main(String args[]){


B obj = new B();
obj.methodA(); //calling super class method
obj.methodB (); //calling local method.
}
}
Multilevel inheritance
Example
class X
{
public void methodX(){
System.out.println("Class X method");}
}
Class Y extends X
public void methodY(){
System.out.println("class Y method");}
}
Class Z extends Y
public void methodz(){
System.out.println("class Z method");}
public static void main(String args[])
{
z obj = new Z();
obj.methodX(); //calling grand parent class method
obj.methodY(); //calling parent class method
obj.methodz(); //calling local method
}
}
Hierarchical Inheritance
Example
Class A
{
public void methodA()
System.out.println("method of Class A");
}
Class B extends A
{
public void methodB()
System.out.println("method of Class B");
}
Class C extends A
{
public void methodC()
System.out.println("method of Class C");
}
Continued…
Class D extends A
{
public void methodD()
System.out.println("method of Class D");
}
Class MyClass
public static void main(String args[])
{
B obj1 = new B();
C obj2 = new C();
D obj3 = new D();
obj1.methodA();
obj2.methodA();
obj3.methodA();
}
}

You might also like