What is Inheritance?
Java enables a class to inherit properties and actions from another class called as
inheritance. Through inheritance, a subclass can access members of its superclass (fields and
methods),
Inheritance represents the IS-A relationship which is also known as a parent-child
relationship. E.g. A kiwi is a fruit
Why use inheritance?
o For Method Overriding (so runtime polymorphism can be achieved).
o For Code Reusability.
Terms used in Inheritance
o Class: A class is a group of objects which have common properties. It is a template or
blueprint from which objects are created.
o Sub Class/Child Class: Subclass is a class which inherits the other class. It is also called
a derived class, extended class, or child class.
o Super Class/Parent Class: Superclass is the class from where a subclass inherits the
features. It is also called a base class or a parent class.
o Reusability: As the name specifies, reusability is a mechanism which facilitates you to
reuse the fields and methods of the existing class when you create a new class. You
can use the same fields and methods already defined in the previous class.
The Syntax of Java Inheritance class Subclass-
name extends Superclass-name
//methods and fields
The extends keyword indicates that we are making a new class that derives from an existing
class. The meaning of "extends" is to increase the functionality.
Java Inheritance Example
class Employee{
float salary=40000; }
class Programmer extends Employee{
int bonus=10000;
public class Main{ public static void
main(String args[]){
Programmer p=new Programmer();
System.out.println("Programmer salary is:"+p.salary);
System.out.println("Bonus of Programmer is:"+p.bonus);
Output:
Programmer salary is:40000.0
Bonus of programmer is:10000
Types of Inheritance in Java
In Java programming, multiple and hybrid inheritance is supported through interface only.
1.Single Inheritance
When a class inherits another class, it is known as a single inheritance.
In the example given below, Dog class inherits the Animal class, so there is the single
inheritance.
Example
class Animal{ void
eat() {
System.out.println("eating...");
class Dog extends Animal{ void
bark(){
System.out.println("barking...");
public class Main{ public static void
main(String args[]){ Dog d=new
Dog();
d.bark();
d.eat();
Output:
barking...
eating...
2.Multilevel Inheritance
When there is a chain of inheritance, it is known as multilevel inheritance.
As you can see in the example given below, BabyDog class inherits the Dog class which again
inherits the Animal class, so there is a multilevel inheritance.
To read more: Multilevel Inheritance in Java
Example
class Animal{ void
eat(){
System.out.println("eating...");
class Dog extends Animal{ void
bark(){
System.out.println("barking...");
class BabyDog extends Dog{
void weep(){
System.out.println("weeping...");
public class Main{ public static void
main(String args[]){ BabyDog
d=new BabyDog();
d.weep();
d.bark();
d.eat();
Output:
weeping...
barking...
eating...
3.Hierarchical Inheritance
When two or more classes inherits a single class, it is known as hierarchical inheritance.
In the example given below, Dog and Cat classes inherits the Animal class, so there is
hierarchical inheritance.
Example
class Animal{ void eat()
{System.out.println("eating...");}
class Dog extends Animal{ void bark()
{System.out.println("barking...");}
class Cat extends Animal{ void meow()
{System.out.println("meowing...");}
}
public class Main{ public static void
main(String args[]){ Cat c=new
Cat();
c.meow();
c.eat();
c.bark();
Output:
meowing...
eating...
4.Multiple Inheritance
Multiple Inheritance A class's capacity to inherit traits from several classes is referred to as
multiple inheritances.
Multiple inheritances, however, can result in issues like the diamond problem, which occurs
when two superclasses share the same method or field and causes conflicts. Java uses
interfaces to implement multiple inheritances in order to prevent these conflicts.
Example
interface Character {
void attack();
}
interface Weapon {
void use();
class Warrior implements Character, Weapon {
public void attack() {
System.out.println("Warrior attacks with a sword.");
public void use() {
System.out.println("Warrior uses a sword.");
class Mage implements Character, Weapon {
public void attack() {
System.out.println("Mage attacks with a wand.");
public void use() {
System.out.println("Mage uses a wand.");
public class Main { public static
void main(String[] args) {
Warrior warrior = new Warrior(); Mage mage =
new Mage(); warrior.attack(); // Output: Warrior
attacks with a sword. warrior.use(); // Output: Warrior
uses a sword. mage.attack(); // Output: Mage attacks
with a wand. mage.use(); // Output: Mage uses a
wand.
}
}
Output:
Warrior attacks with a sword.
Warrior uses a sword.
Mage attacks with a wand.
Mage uses a wand.
How to achieve Multiple Inheritance in Java?
Java supports multiple inheritance through interfaces only, where a class can implement
multiple interfaces. Multiple inheritance in Java is not possible by class, but it is possible
through interfaces.
5.Hybrid Inheritance in Java
The hybrid inheritance is the composition of two or more types of inheritance. The main
purpose of using hybrid inheritance is to modularize the code into well-defined classes. It
also provides the code reusability.
The hybrid inheritance can be achieved by using the following combinations:
o Single and Multiple Inheritance (not supported but can be achieved
through interface) o Multilevel and Hierarchical Inheritance o
Hierarchical and Single Inheritance o Multiple and Multilevel
Inheritance
Example:
class C
public void disp()
System.out.println("C");
class A extends C
public void disp()
System.out.println("A");
class B extends C
public void disp()
System.out.println("B");
public class D extends A
public void disp()
{
System.out.println("D");
public static void main(String args[])
D obj = new D(); obj.disp();
Output:
Abstract Class in Java
An abstract class in Java acts as a partially implemented class that itself cannot be
instantiated.
It exists only for subclassing purposes, and provides a template for its subcategories to
follow.
Abstract classes can have implementations with abstract methods.
Abstract methods are declared to have no body, leaving their implementation to subclasses.
Abstract Method in Java
A method which is declared as abstract and does not have implementation is known as an
abstract method.
Example of abstract method abstract void printStatus();//no
method body and abstract
Example of Abstract Class that has an Abstract Method
Example
//Creating an abstract class having abstract method
abstract class Bike{
abstract void run();
//Creating a child class and override abstract
method class Honda extends Bike{ void run()
{System.out.println("running safely");}
//Creating a Main class to create object and call
methods public class Main{ public static void
main(String args[]){ Bike obj = new Honda();
obj.run();
Output:
running safely
Abstract Class having Constructor, Data Member and Methods
An abstract class can have a data member, abstract method, method body (non-abstract
method), constructor, and even main() method. Example
abstract class Bike{
Bike(){System.out.println("bike is created");}
abstract void run(); void changeGear()
{System.out.println("gear changed");}
//Creating a Child class which inherits Abstract class class
Honda extends Bike{
void run(){System.out.println("running safely..");}
//Creating a Main class which calls abstract and non-abstract methods
public class Main{ public static void main(String args[]){ Bike
obj = new Honda(); obj.run(); obj.changeGear();
Output:
bike is created
running safely..
gear changed
Key Features of Abstract Classes
Abstract Methods: Abstract classes can have abstract methods, which are declared without
a body. Subclasses must provide concrete implementations for these methods.
Concrete Methods: Abstract classes can also contain concrete methods with defined
behavior. Subclasses inherit these methods along with their implementations.
Cannot be Instantiated: Abstract classes cannot be instantiated directly. They serve as a
blueprint for other classes and must be extended to be used.
Can Have Constructors: Abstract classes can have constructors, which are invoked when a
subclass object is created. These constructors are used to initialize the state of the
abstract class.
Interface in Java
• An interface in Java is a blueprint of a class. It has static constants and abstract
methods.
• There can be only abstract methods in the Java interface, not a method body.
Why use a Java interface?
There are mainly three reasons to use an interface. They are given below.
o It is used to achieve abstraction.
o By interface, we can support the functionality of multiple inheritance.
o It is used to achieve loose coupling.
How to declare an interface?
• An interface is declared by using the interface keyword.
• It provides total abstraction; it means all the methods in an interface are declared
with an empty body, and all the fields are public, static and final by default.
• A class that implements an interface must implement all the methods declared in the
interface.
Syntax:
interface interface_name{
// declare constant fields //
declare methods that abstract
// by default.
Declaring Interface
interface Animal {
void eat(); void
sleep();
}
Relationship Between Classes and Interfaces
Java Interface Example
//Creating an interface
interface Printable{
void print();
//Creating a class that implements Printable
class Printer implements Printable{ public
void print(){System.out.println("Hello");}
//Creating a class that creates objects and call
methods public class Main{ public static void
main(String args[]){ Printable p=new Printer();
p.print();
Output:
Hello
Multiple Inheritance in Java by Interface
If a class implements multiple interfaces, or an interface extends multiple interfaces, it is
known as multiple inheritance.
Example
//Creating two interfaces
interface Printable{
void print();
interface Showable{
void show();
//Creating a class that implements two interfaces class
Computer implements Printable,Showable{ public void
print(){System.out.println("printing data...");} public
void show(){System.out.println("showing data...");}
//Creating a Main class to create object and call methods
public class Main{ public static void main(String
args[]){ Computer c = new Computer();
c.print();
c.show();
Output:
printing data...
showing data...
Inheritance of Interfaces
A class implements an interface, but one interface can extend another interface.
Example
interface Printable{ void
print();
interface Showable extends Printable{ void
show();
class Main implements Showable{ public void
print(){System.out.println("Hello");} public void
show(){System.out.println("Welcome");}
public static void main(String args[]){ Main
obj = new Main();
obj.print(); obj.show();
Output:
Hello
Welcome
Difference between Abstract Class and Interface in Java
Abstract class and interface both are used to achieve abstraction where we can declare the
abstract methods.
Abstract class and interface both can't be instantiated.
Abstract class Interface
1) Abstract class can have abstract and Interface can have only abstract methods. Since
nonabstract methods. Java 8, it can have default and static methods
also.
2) Abstract class doesn't support multiple Interface supports multiple inheritance.
inheritance.
3) Abstract class can have final, non-final, static Interface has only static and final variables.
and non-static variables.
4) Abstract class can provide the implementation Interface can't provide the implementation of
of interface. abstract class.
5) The abstract keyword is used to declare The interface keyword is used to declare
abstract class. interface.
6) An abstract class can extend another Java class An interface can extend another Java interface
and implement multiple Java interfaces. only.
7) An abstract class can be extended using An interface can be implemented using keyword
keyword "extends". "implements".
8) A Java abstract class can have class members Members of a Java interface are public by
like private, protected, etc. default.
9)Example: Example: public interface
public abstract class Shape{ public Drawable{ void draw();
abstract void draw(); }
}