Java Inheritances
Presented by:
T.Vignesh
22911A7217
Ai&Ds
Introduction to Java Inheritances
• Inheritance in Java allows a class to inherit properties and
behaviors from another class.
• It promotes code reusability and helps in building a
hierarchical structure of classes.
• The subclass inherits the fields and methods of the
superclass.
Types of Inheritance in Java
• Java supports single, multi-level, hierarchical, and hybrid
inheritance.
• Single inheritance allows a class to inherit from only one
superclass.
• Multi-level inheritance involves a chain of classes where a
subclass inherits from another subclass.
Access Modifiers in Inheritance
• In Java, access modifiers control the visibility of fields and
methods in a class.
• The access modifiers - public, private, protected, and
default - play a crucial role in inheritance.
• Private members are not inherited by subclasses, while
protected and public members are inherited.
Method Overriding in Java
• Method overriding allows a subclass to provide a specific
implementation of a method that is already provided by its
superclass.
• The overridden method in the subclass should have the
same method signature as the superclass method.
• It is used to achieve runtime polymorphism in Java.
Super Keyword in Inheritance
• The super keyword in Java is used to refer to the superclass
of a subclass.
• It can be used to access superclass methods, constructors,
and fields.
• The super keyword is especially useful in cases of method
overriding.
Best Practices for Java Inheritances
• Prefer composition over inheritance to avoid complex class
hierarchies.
• Use interfaces to define contracts and achieve multiple
inheritance-like behavior.
• Ensure proper access control using access modifiers to
maintain encapsulation and data hiding.