0% found this document useful (0 votes)
43 views13 pages

Inheritance in Java - PPTX - 20241025 - 101324 - 0000

Uploaded by

rupkumar8084
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)
43 views13 pages

Inheritance in Java - PPTX - 20241025 - 101324 - 0000

Uploaded by

rupkumar8084
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

TOPIC - INHERITANCE IN JAVA

Presented By :Group-1
⚬Pavani (230101120241)
⚬Pawan Kalyan (230101120243)
⚬Achhutha Reddy (230101120245)
⚬Rup Kumar (230101120277)

Guided by - Prof. Satyanarayana Sahoo


What is Inheritance?
• 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.
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.
• 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.
(Application)
• Code Reusability: The code written in the Superclass is common
to all subclasses. Child classes can directly use the parent class
code.

• Hierarchical Organization: Creates a structured and modular


design.

• Polymorphism: Enables different objects to be treated as


instances of a common superclass.

• 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.
Implementing Inheritance in Java
Declare Parent Class
Start by defining the parent class, which will serve as the base for
the inheritance hierarchy.

Declare Child Class


Create the child class and use the 'extends' keyword to specify
the parent class it inherits from.

Inherit Members
The child class automatically inherits all non-private fields and
methods from the parent class, allowing for code reuse.
inheritance syntax
Types of 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. In the figure, ‘A’ is a parent class and ‘B’ is a
child class. The class ‘B’ inherits all the properties of the
class ‘A’.

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 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.
3. Hierarchical Inheritance
In Hierarchical Inheritance, one class serves as a
superclass (base class) for more than one
subclass. In the image, class A serves as a base
class for the derived classes B, C, and D.

4. Multiple Inheritance
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, Class C is derived from
interfaces A and B.
different terms in inheritance
• Keyword (extends)
1.Used to establish the inheritance relationship between classes.
2.The subclass inherits all non-private members of the superclass.

• Access Modifiers and Inheritance


1.public: Accessible anywhere.
2.private: Accessible only within the class.
3.protected: Accessible within the package and subclasses.
4.default: Accessible within the package

• Method Overriding
1.A subclass can provide a specific implementation for a method inherited
from its superclass.
2.The method signature (name, parameters, and return type) must be the
same.
3.The @Override annotation can be used to indicate method overriding.

• Method Overloading
1.A class can have multiple methods with the same name but
different parameters.
2.Method overloading is not directly related to inheritance but can be
used in conjunction with it.
Advantages of Inheritance
• Code Reusability: Reduces code duplication and promotes efficient
development.

• Modularity: Encourages the creation of well-structured, modular code.

• Extensibility: Allows for easy addition of new features and functionalities.

• Polymorphism: Enables flexible and dynamic code, where objects of


different types can be treated as objects of a common superclass.

• Hierarchical Organization: Creates a clear and understandable class


hierarchy, improving code readability and maintainability.
Real world examples

• GUI Frameworks: In GUI frameworks like Swing and JavaFX,


various UI components (buttons, labels, text fields) inherit from a
common base class, sharing common properties and behaviors.
• Game Development: Game objects like characters, enemies, and
items can be organized into a class hierarchy, inheriting attributes
and behaviors from a base class.
• Enterprise Applications: Complex enterprise applications often use
inheritance to model business objects and their relationships,
promoting code reuse and maintainability.
Conclusion
Inheritance is a fundamental concept in Java that enables
code reuse, hierarchical organization, and polymorphic
behavior. By understanding and properly implementing
inheritance, developers can create more efficient, flexible,
and maintainable Java applications.

You might also like