Inheritance Overview
Inheritance enables one class to inherit fields and methods from another. It supports code reusability and establishes an IS-A relationship.
- Parent class โ Super class / Base class
- Child class โ Sub class / Derived class
- Keyword:
extends
Types of Inheritance
| Type | Description | Java Support |
|---|---|---|
| Single | One child inherits one parent | โ Yes |
| Multi-level | AโBโC (chain) | โ Yes |
| Hierarchical | One parent, multiple children | โ Yes |
| Multiple | One child, multiple parents | โ Via classes โ โ Via interfaces |
| Hybrid | Combination of above | โ Via interfaces |
// Multi-level inheritance
class Animal { void breathe() { System.out.println("Breathes"); } }
class Mammal extends Animal { void walk() { System.out.println("Walks"); } }
class Dog extends Mammal { void bark() { System.out.println("Barks"); } }
Dog d = new Dog();
d.breathe(); d.walk(); d.bark(); // all three accessiblesuper Keyword
The super keyword refers to the immediate parent class. It is used to:
- Access parent class fields (when shadowed by child)
- Call parent class methods (especially overridden ones)
- Call parent class constructor โ
super()must be first line
class Shape {
String color;
Shape(String color) { this.color = color; }
void draw() { System.out.println("Drawing " + color + " shape"); }
}
class Rectangle extends Shape {
double width, height;
Rectangle(String color, double w, double h) {
super(color); // call parent constructor
width = w; height = h;
}
void draw() {
super.draw(); // call parent method
System.out.println("Rectangle: " + width + "x" + height);
}
}Method Overriding
Method Overriding occurs when a subclass provides its own implementation of a method already defined in the parent class. Rules:
- Same method name, same parameter list, same or covariant return type
- Access modifier can be same or wider (not more restrictive)
- Cannot override
static,final, orprivatemethods - Use
@Overrideannotation for compile-time safety
class Bank {
double getInterestRate() { return 4.0; }
}
class SBI extends Bank {
@Override
double getInterestRate() { return 7.0; }
}
class HDFC extends Bank {
@Override
double getInterestRate() { return 6.5; }
}final with Inheritance
| Usage | Effect |
|---|---|
final class | Class cannot be extended at all |
final method | Method cannot be overridden in subclass |
final variable | Acts as a constant; cannot be changed |
String, Integer, and other wrapper classes are all declared final in Java's standard library.Abstract Classes
An abstract class is a class that cannot be instantiated. It serves as a template, defining methods that subclasses must implement.
- Declared with the
abstractkeyword - Can have both abstract (no body) and concrete (with body) methods
- If a class has even one abstract method, the class must also be abstract
- Subclass must override ALL abstract methods, or itself be abstract
abstract class Vehicle {
int speed;
// abstract method โ no body
abstract void fuelType();
// concrete method
void showSpeed() { System.out.println("Speed: " + speed); }
}
class Car extends Vehicle {
@Override
void fuelType() { System.out.println("Petrol / Diesel"); }
}
class ElectricCar extends Vehicle {
@Override
void fuelType() { System.out.println("Electric Battery"); }
}Runtime Polymorphism
Dynamic Method Dispatch is the mechanism by which an overridden method is called through a parent class reference at runtime. The JVM determines which version to call based on the actual object type โ not the reference type.
public class Test {
public static void main(String[] args) {
Bank b; // parent reference
b = new SBI();
System.out.println("SBI rate: " + b.getInterestRate()); // 7.0
b = new HDFC();
System.out.println("HDFC rate: " + b.getInterestRate()); // 6.5
// Array of Bank references โ different objects
Bank[] banks = { new SBI(), new HDFC(), new Bank() };
for (Bank bank : banks)
System.out.println(bank.getInterestRate()); // 7.0, 6.5, 4.0
}
}Download PDF Notes
Save these notes as a PDF for offline reference