Presented By :Group-6
⚬AFFANUL HAQUE (230101120273)
⚬PRASHANT KUMAR (230101120274)
⚬MAHI VERMA (230101120275)
⚬RANJEET KUMAR (230101120276)
⚬SAURABH KUMAR (230101120277)
TOPIC - INHERITANCE IN
JAVA
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.
why do we need java inheritance
(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.
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.
Implementing Inheritance in
Java
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.
• 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.
real world examples
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.
Inheritance in java.pptx_20241025_101324_0000.pptx.pptx

Inheritance in java.pptx_20241025_101324_0000.pptx.pptx

  • 2.
    Presented By :Group-6 ⚬AFFANULHAQUE (230101120273) ⚬PRASHANT KUMAR (230101120274) ⚬MAHI VERMA (230101120275) ⚬RANJEET KUMAR (230101120276) ⚬SAURABH KUMAR (230101120277) TOPIC - INHERITANCE IN JAVA Guided by - Prof. Satyanarayana Sahoo
  • 3.
    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.
  • 4.
    terms used ininheritance • 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.
  • 5.
    why do weneed java inheritance (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.
  • 6.
    Declare Parent Class Startby 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. Implementing Inheritance in Java
  • 7.
  • 8.
    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.
  • 9.
    3. Hierarchical Inheritance InHierarchical 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.
  • 10.
    different terms ininheritance • 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.
  • 11.
    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.
  • 12.
    • 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. real world examples
  • 13.
    Conclusion Inheritance is afundamental 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.