Java Inheritance –OOP Concepts
Detailed Types of Inheritance with
Examples
2.
What is Inheritance?
•• One class acquires properties of another
class
• • Promotes code reusability
• • Enables method overriding
• • Parent = Superclass, Child = Subclass
3.
Types of Inheritancein Java
• • Single Inheritance
• • Multilevel Inheritance
• • Hierarchical Inheritance
• Note: Multiple inheritance with classes is NOT
supported.
4.
Single Inheritance
• Example:
•class A {
• void show() { System.out.println("Class A"); }
• }
• class B extends A {
• void display() { System.out.println("Class
B"); }
• }
• Output: A → B
5.
Multilevel Inheritance
• Example:
•class A {
• void show() { System.out.println("A"); }
• }
• class B extends A {
• void display() { System.out.println("B"); }
• }
• class C extends B {
• void print() { System.out.println("C"); }
6.
Hierarchical Inheritance
• Example:
•class A {
• void show() { System.out.println("A"); }
• }
• class B extends A {
• void display() { System.out.println("B"); }
• }
• class C extends A {
• void print() { System.out.println("C"); }
7.
Why Inheritance?
• •Reduces duplicate code
• • Supports method overriding
• • Helps implement dynamic polymorphism
• • Makes code cleaner and easier to maintain