You are on page 1of 7

INHERITANCE IN JAVA

Inheritance is an important part of Obect Oriented Progamming System. It


is a mechanism which helps a class to acquire properties and behavior of
another class.

When we inherit from an existing class, we can reuse methods and fields of
the parent class. Moreover, we can add new methods and fields in your
current class also.

• The basic idea behind inheritance is the reusability of codes.


• For Method Overriding (so runtime polymorphism can be achieved).
• Inheritance represents the IS-A relationship which is also known as a
parent-child relationship.

Terms used in Inheritance

• Class: A class
is a group of objects which have common properties. It is a
template or blueprint from which objects are created.

• 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.

• 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.

HARSH (04324302019)
The syntax Of Java Inheritance:
class Subclass-name extends Superclass-name
{
//methods and fields
}

Look at the example given below. Here Vehicle is the superclass and Sports is the
subclass . The relationship between the two classes is Sports IS-A Vehicle. It means
that Sports is a type of Vehicle.

Types of inheritance in java:


Single Inheritance
Multilevel Inheritance
Hierarchical Inheritance

• In java programming, multiple and hybrid inheritance is supported through


interface only.

HARSH (04324302019)
Single Inheritance
The example discussed above was an example of single inheritance.

In single inheritance, subclasses inherit the features of one superclass.

Output:-

Multilevel in heritance
When there is a chain of inheritance, it is known as multilevel inheritance.

Output:-

Hierarchical Inheritance
In Hierarchical Inheritance, one class serves as a superclass (base class) for more than one
subclass.

HARSH (04324302019)
Output:-

Interface?
An interface in Java is a blueprint of a class. It has static constants and abstract
methods.

The reasons to use interface :-

• It is used to achieve abstraction.


• By interface, we can support the functionality of multiple inheritance.

How to declare interface?

interface <interface_name>{
// declare constant fields
// declare methods that abstract
// by default.
}

• Interfaces are implemented in class.


• Interfaces can be extended to another interface.

HARSH (04324302019)
Multiple Inheritance
In Multiple inheritances, one class can have more than one superclass and inherit features from
all parent classes. Java does not support multiple inheritances with classes. In java, we can
achieve multiple inheritances only through Interfaces.

Output:-

Abstract class
Abstraction is a process of hiding the implementation details and showing only
functionality to the user.

A class which is declared with the abstract keyword is known as an abstract class in
Java. It can have abstract and non-abstract methods (method with the body).

• No objects are created for abstract class.


• It is implemented inn its subclass.

Output :-

Difference between Interface and Abstract class

HARSH (04324302019)
Method Overloading
If a class has multiple methods having same name but different in parameters, it is
known as Method Overloading.

Given below are the signatures to overload the method in java

• By changing number of arguments


• By changing the data type
• By changing the order of arguments

Output:-

Method overriding
If subclass (child class) has the same method as declared in the parent class, it is
known as method overriding in Java.

Usage of Java Method Overriding


• Method overriding is used to provide the specific implementation of a method
which is already provided by its superclass.
• Method overriding is used for runtime polymorphism

Rules for Java Method Overriding


The method must have the same name as in the parent class
The method must have the same parameter as in the parent class.

HARSH (04324302019)
-> Base class method gets hidden.

Output:-

Use of “Super”
Usage of Java super Keyword
1. super can be used to refer immediate parent class instance variable.
2. super can be used to invoke immediate parent class method.
3. super() can be used to invoke immediate parent class constructor.

Output:-

Using ”Super” to access base class field:

Output:-

Using “Super” to invoke constructor of Base class

Output:-

HARSH (04324302019)

You might also like