Inheritance in Java
Inheritance in Java
• Purpose:
• - Code reusability
• - Better organization
• - Maintainability
Why Use Inheritance?
• - Avoids code duplication
• - Enhances code maintainability
• - Establishes relationships between classes (IS-
A relationship)
• - Enables method overriding for
polymorphism
Types of Inheritance in Java
• 1. Single Inheritance
• 2. Multilevel Inheritance
• 3. Hierarchical Inheritance
• 4. Hybrid Inheritance (Not directly supported
in Java)
• 5. Multiple Inheritance (Through Interfaces)
Syntax of Inheritance
• class Parent {
• void display() { System.out.println("This is
the Parent class"); }
• }
• Example:
• class Parent {
• void message()
{ System.out.println("Message from Parent"); }
• }
• Example:
• interface A { void methodA(); }
• interface B { void methodB(); }
• class C implements A, B { public void
Conclusion
• - Inheritance allows code reuse and improves
structure.
• - Helps implement polymorphism.
• - Important for designing scalable applications.
Q&A - Any Questions?