🧬 Inheritance
Master class inheritance, super keyword, method overriding, and inheritance types.
What is Inheritance?
Inheritance is a mechanism in which one object acquires all the properties and behaviors of a parent object. It represents an IS-A relationship (e.g., a Dog IS-A Animal). In Java, you use the extends keyword to inherit from a class.
Types of Inheritance in Java
- Single Inheritance: A subclass inherits from one superclass.
- Multilevel Inheritance: A subclass inherits from a class that inherits from another class.
- Hierarchical Inheritance: Multiple subclasses inherit from a single superclass.
Multilevel Inheritance Example
class Animal { void eat() { System.out.println("Eating..."); } } class Dog extends Animal { void bark() { System.out.println("Barking..."); } } class GoldenRetriever extends Dog { void fetch() { System.out.println("Fetching ball..."); } } public class MultilevelDemo { public static void main(String[] args) { GoldenRetriever myDog = new GoldenRetriever(); myDog.eat(); // Inherited from Animal myDog.bark(); // Inherited from Dog myDog.fetch(); // Own method } }
Hierarchical Inheritance
class Shape { void draw() { System.out.println("Drawing a shape."); } } class Circle extends Shape { void drawCircle() { System.out.println("Drawing Circle."); } } class Rectangle extends Shape { void drawRectangle() { System.out.println("Drawing Rectangle."); } } public class HierarchicalDemo { public static void main(String[] args) { Circle c = new Circle(); c.draw(); c.drawCircle(); Rectangle r = new Rectangle(); r.draw(); r.drawRectangle(); } }
The super Keyword
The super keyword is a reference variable used to refer to an immediate parent class object. It can be used to invoke parent class methods, access parent class fields, and invoke parent class constructors.
class Employee { String name; double salary; Employee(String n, double s) { this.name = n; this.salary = s; } void displayDetails() { System.out.println("Name: " + name + ", Salary: $" + salary); } } class Manager extends Employee { double bonus; Manager(String n, double s, double b) { super(n, s); // Invokes parent class constructor this.bonus = b; } @Override void displayDetails() { super.displayDetails(); // Invokes parent class method System.out.println("Bonus: $" + bonus); } } public class EmployeeManager { public static void main(String[] args) { Manager mgr = new Manager("Eve", 80000, 15000); mgr.displayDetails(); } }
Method Overriding & The final Keyword
Method Overriding happens when a subclass provides a specific implementation for a method that is already defined in its parent class. The @Override annotation is used to ensure the method correctly overrides the parent's method.
The final keyword can be used to prevent inheritance or overriding:
- A final class cannot be inherited.
- A final method cannot be overridden by subclasses.
class Base { final void show() { System.out.println("Final method in Base class."); } } class Derived extends Base { // void show() { ... } // ERROR: Cannot override the final method from Base } final class Leaf { // Class contents } // class SubLeaf extends Leaf { } // ERROR: Cannot subclass a final class