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

Java Inheritance

The document provides an overview of inheritance in Java, explaining its significance in object-oriented programming, including code reusability, extensibility, and method overriding. It outlines different types of inheritance such as single, multilevel, hierarchical, and hybrid inheritance, along with the concept of polymorphism. Additionally, it discusses the rules for method overriding and the use of final variables in Java.

Uploaded by

Neha Sharma
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)
3 views

Java Inheritance

The document provides an overview of inheritance in Java, explaining its significance in object-oriented programming, including code reusability, extensibility, and method overriding. It outlines different types of inheritance such as single, multilevel, hierarchical, and hybrid inheritance, along with the concept of polymorphism. Additionally, it discusses the rules for method overriding and the use of final variables in Java.

Uploaded by

Neha Sharma
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/ 35

Object Oriented Programming with Java

Java
Inheritance

Rita Nagar
Assistant Professor, CSE Department MPGI
Outlines
• Prerequisite
• Inheritance
• How can we implement inheritance?
• Why do we need inheritance?
• Types of inheritance
• References ,
Prerequisite

• Class

• Object creation
Java Inheritance
In Java, 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.

When one object acquires all the properties and


behaviours of parent object known as
inheritance.

Inheritance means creating new classes based on


existing ones.

Object Oriented Programming in


• The existing class is called the parent class, or superclass, or base class
• The derived class is called the child class or subclass.
• As the name implies, the child inherits characteristics of the parent
• That is, the child class inherits the methods and data defined for the parent class

Object Oriented Programming in


Inheritance

Class (parent,base,super)

Child class Child class


Child ,Derived class,subclass

Object Oriented Programming in


• The extends keyword indicates that you are making a new class that derives
from an existing class. The meaning of "extends" is to increase the
functionality.

• In the terminology of Java, a class which is inherited is called a parent or


superclass, and the new class is called child or subclass.

Object Oriented Programming in


Why Do We Need Java Inheritance?
•Code Reusability: The basic need of an inheritance is to reuse the features. If you
have defined some functionality once, by using the inheritance you can easily use them
in other classes and packages.

•Extensibility: The inheritance helps to extend the functionalities of a class. If you have
a base class with some functionalities, you can extend them by using the inheritance in
the derived class.

• Method Overriding: Method Overriding is achievable only through Inheritance. It is


one of the ways by which Java achieves Run Time Polymorphism.

•Abstraction: The concept of abstract where we do not have to provide all details is
achieved through inheritance. Abstraction only shows the functionality to the user.

Object Oriented Programming in


Types of inheritance in JAVA
Types of inheritance

In single inheritance, subclasses


inherit the features of one superclass.
In the image below, class A serves as
a base class for the derived class B.

Object Oriented Programming in


Example

Student

University

Object Oriented Programming in


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.

Object Oriented Programming in


Example

Student

Courses

University

Object Oriented Programming in


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

Object Oriented Programming in


Example

Object Oriented Programming in


Output
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 Interface.

Object Oriented Programming in


Example

Object Oriented Programming in


It allows to inherit the properties of a class into another class
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

Object Oriented Programming in


Example

C
Hierarchical Inheritance

B A
Single Inheritance

D
Method overriding

• Declaring a method in sub class which is already present in parent class is known
as method overriding.

Method overriding in Java allows a subclass to provide a specific


implementation of a method that is already provided by its superclass. This
enables you to define a method in a subclass that has the same signature
(name, parameters, and return type) as a method in its superclass.
Advantage of method overriding

• The main advantage of method overriding is that the class can give its own
specific implementation to a inherited method without even modifying the
parent class code.
• This is helpful when a class has several child classes, so if a child class needs to
use the parent class method, it can use it and the other classes that want to
have different implementation can use overriding feature to make changes
without touching the parent class code.
• 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.
• There must be an IS-A relationship (inheritance).

Is-A relationship: Whenever one class inherits another class, it is called an IS-A relationship
Example
polymorphism

• Polymorphism is one of the OOPs feature that allows us to perform a single action
in different ways.

Polymorphism is that in which we can perform a task in multiple forms or ways.


It is applied to the functions or methods. Polymorphism allows the object to
decide which form of the function to implement at compile-time as well as run-
time.
Types of Polymorphism are:

• Compile-time polymorphism (Method overloading)

• Run-time polymorphism (Method Overriding)


Difference between method overloading and method overriding

S. No Method Overloading Method Overriding

Method overriding is used to provide the


Method overloading is used to increase the
1) specific implementation of the method that is
readability of the program.
already provided by its super class.

Method overriding occurs in two classes that


2) Method overloading is performed within class.
have IS-A (inheritance) relationship.

In case of method overloading, parameter must In case of method overriding, parameter must
3)
be different. be same.
Method overloading is the example of compile Method overriding is the example of run time
4)
time polymorphism. polymorphism.
In java, method overloading can't be performed
by changing return type of the method only.
Return type must be same in method
5) Return type can be same or different in method
overriding.
overloading. But you must have to change the
parameter.
Final Variable
• A final variable, also called a “constant,” is a variable whose value you can't change after it's
been initialized.

• For example, you might use a final variable to define a constant value, such as pi.

When a variable is declared with the final keyword, its value can’t be changed, essentially, a constant. This
also means that you must initialize a final variable.
References

1) https://www.geeksforgeeks.org/inheritance-in-java/
2) https://www.javatpoint.com/inheritance-in-java
3) https://www.w3schools.com/java/java_inheritance.asp
4) E Balagurusamy Programming with JAVA McGraw Hill Publications
5) Naughton & Schildt The Complete Reference Java 2 Tata McGraw Hill
6) Horstmann & Cornell “Core Java 2” (Vol I & II ) Sun Microsystems
7) Deitel Java- How to Program Pearson Education, Asia
8) lvan Bayross Java 2.0 BPB publications
21

You might also like