Java Inheritance Programs and Notes
Java Inheritance Programs and Notes
// base class
class Bicycle {
// the Bicycle class has two fields
public int gear;
public int speed;
// derived class
class MountainBike extends Bicycle {
// driver class
public class Test {
public static void main(String args[])
{
----------------------------------------------------
Inheritance in Java
Syntax :
// base class
class Bicycle {
// the Bicycle class has two fields
public int gear;
public int speed;
// derived class
class MountainBike extends Bicycle {
// driver class
public class Test {
public static void main(String args[])
{
Output
No of gears are 3
speed of bicycle is 100
seat height is 25
In the above program, when an object of MountainBike
class is created, a copy of all methods and fields of the
superclass acquires memory in this object. That is why by
using the object of the subclass we can also access the
members of a superclass.
import java.io.*;
// Base or Super Class
class Employee {
int salary = 60000;
}
// Driver Class
class Gfg {
public static void main(String args[])
{
Engineer E1 = new Engineer();
System.out.println("Salary : " + E1.salary
+ "\nBenefits : " + E1.benefits);
}
}
Output
Salary : 60000
Benefits : 10000
Illustrative image of the program:
Inheritance in Java;
In practice, inheritance, and polymorphism are used
together in Java to achieve fast performance and
readability of code.
1.Single Inheritance
2.Multilevel Inheritance
3.Hierarchical Inheritance
4.Multiple Inheritance
5.Hybrid Inheritance
1. Single Inheritance
In single inheritance, a sub-class is derived from only one
super class. It inherits the properties and behavior of a
single-parent class. Sometimes, it is also known as simple
inheritance. In the below figure, ‘A’ is a parent class and
‘B’ is a child class. The class ‘B’ inherits all the properties
of the class ‘A’.
// Parent class
class One {
public void print_geek()
{
System.out.println("Geeks");
}
}
// Driver class
public class Main {
// Main function
public static void main(String[] args)
{
Two g = new Two();
g.print_geek();
g.print_for();
g.print_geek();
}
}
Output
Geeks
for
Geeks
2. Multilevel Inheritance
In Multilevel Inheritance, a derived class will be inheriting
a base class, and as well as the derived class also acts as
the base class for other classes. In the below image, class
A serves as a base class for the derived class B, which in
turn serves as a base class for the derived class C. In Java,
a class cannot directly access the grandparent’s members.
// Driver class
public class Main {
public static void main(String[] args) {
// Creating an object of class Three
Three g = new Three();
Output
Geeks
for
Geeks
Java Course
Java Arrays
Java Strings
Java OOPs
Java Collection
Java 8 Tutorial
Java Multithreading
Java Exception Handling
Java Programs
Java Project
Java Collections Interview
Java Interview Questions
Java MCQs
Spring
Spring MVC
Spring Boot
Hibernate
▲
Open In App
Inheritance in Java
Last Updated : 05 Jul, 2024
Java, 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. In Java, Inheritance means
creating new classes based on existing ones. A class that
inherits from another class can reuse the methods and
fields of that class. In addition, you can add new fields and
methods to your current class as well.
Syntax :
// base class
class Bicycle {
// the Bicycle class has two fields
public int gear;
public int speed;
// derived class
class MountainBike extends Bicycle {
// driver class
public class Test {
public static void main(String args[])
{
Output
No of gears are 3
speed of bicycle is 100
seat height is 25
In the above program, when an object of MountainBike
class is created, a copy of all methods and fields of the
superclass acquires memory in this object. That is why by
using the object of the subclass we can also access the
members of a superclass.
import java.io.*;
// Base or Super Class
class Employee {
int salary = 60000;
}
// Driver Class
class Gfg {
public static void main(String args[])
{
Engineer E1 = new Engineer();
System.out.println("Salary : " + E1.salary
+ "\nBenefits : " + E1.benefits);
}
}
Output
Salary : 60000
Benefits : 10000
Illustrative image of the program:
Inheritance in Java
In practice, inheritance, and polymorphism are used
together in Java to achieve fast performance and
readability of code.
Single Inheritance
Multilevel Inheritance
Hierarchical Inheritance
Multiple Inheritance
Hybrid Inheritance
1. Single Inheritance
In single inheritance, a sub-class is derived from only one
super class. It inherits the properties and behavior of a
single-parent class. Sometimes, it is also known as simple
inheritance. In the below figure, ‘A’ is a parent class and
‘B’ is a child class. The class ‘B’ inherits all the properties
of the class ‘A’.
Single inheritance
Single inheritance
// Parent class
class One {
public void print_geek()
{
System.out.println("Geeks");
}
}
// Driver class
public class Main {
// Main function
public static void main(String[] args)
{
Two g = new Two();
g.print_geek();
g.print_for();
g.print_geek();
}
}
Output
Geeks
for
Geeks
2. Multilevel Inheritance
In Multilevel Inheritance, a derived class will be inheriting
a base class, and as well as the derived class also acts as
the base class for other classes. In the below image, class
A serves as a base class for the derived class B, which in
turn serves as a base class for the derived class C. In Java,
a class cannot directly access the grandparent’s members.
Multilevel Inheritance
Multilevel Inheritance
// Driver class
public class Main {
public static void main(String[] args) {
// Creating an object of class Three
Three g = new Three();
Output
Geeks
for
Geeks
3. Hierarchical Inheritance
In Hierarchical Inheritance, one class serves as a
superclass (base class) for more than one subclass. In the
below image, class A serves as a base class for the
derived classes B, C, and D
class A {
public void print_A() { System.out.println("Class A"); }
}
class B extends A {
public void print_B() { System.out.println("Class B"); }
}
class C extends A {
public void print_C() { System.out.println("Class C"); }
}
class D extends A {
public void print_D() { System.out.println("Class D"); }
}
// Driver Class
public class Test {
public static void main(String[] args)
{
B obj_B = new B();
obj_B.print_A();
obj_B.print_B();
Output
Class A
Class B
Class A
Class C
Class A
Class D
4. Multiple Inheritance (Through Interfaces)
In Multiple inheritances, one class can have more than one
superclass and inherit features from all parent classes.
Please note that Java does not support multiple
inheritances with classes. In Java, we can achieve multiple
inheritances only through Interfaces. In the image below,
Class C is derived from interfaces A and B
interface One {
public void print_geek();
}
interface Two {
public void print_for();
}
// Drived class
public class Main {
public static void main(String[] args)
{
Child c = new Child();
c.print_geek();
c.print_for();
c.print_geek();
}
}
Output
Geeks
for
Geeks
5. Hybrid Inheritance
It is a mix of two or more of the above types of
inheritance. Since Java doesn’t support multiple
inheritances with classes, hybrid inheritance involving
multiple inheritance is also not possible with classes. In
Java, we can achieve hybrid inheritance only through
Interfaces if we want to involve multiple inheritance to
implement Hybrid inheritance.
However, it is important to note that Hybrid inheritance
does not necessarily require the use of Multiple
Inheritance exclusively. It can be achieved through a
combination of Multilevel Inheritance and Hierarchical
Inheritance with classes, Hierarchical and Single
Inheritance with classes. Therefore, it is indeed possible to
implement Hybrid inheritance using classes alone, without
relying on multiple inheritance type.
Hybrid Inheritance
Hybrid Inheritance
class SolarSystem {
}
class Earth extends SolarSystem {
}
class Mars extends SolarSystem {
}
public class Moon extends Earth {
public static void main(String args[])
{
SolarSystem s = new SolarSystem();
Earth e = new Earth();
Mars m = new Mars();
Output
true
true
true