What is Inheritance?
Inheritance allows a class (child/subclass) to inherit fields and methods from another class (parent/superclass). It promotes code reuse and establishes an "is-a" relationship.
java
// Parent class
public class Animal {
String name;
public void eat() {
System.out.println(name + " is eating.");
}
}
// Child class
public class Dog extends Animal {
public void bark() {
System.out.println(name + " is barking!");
}
}
// Usage
Dog d = new Dog();
d.name = "Buddy";
d.eat(); // Inherited from Animal
d.bark(); // Defined in DogThe extends Keyword
Use extends to create a subclass:
java
public class Vehicle {
String brand;
int maxSpeed;
public void displayInfo() {
System.out.println(brand + " | Max Speed: " + maxSpeed + " km/h");
}
}
public class Car extends Vehicle {
int numDoors;
public void honk() {
System.out.println(brand + ": Beep beep!");
}
}Method Overriding
A subclass can override a parent method to provide its own implementation:
java
public class Shape {
public double area() {
return 0;
}
}
public class Circle extends Shape {
double radius;
public Circle(double radius) {
this.radius = radius;
}
@Override
public double area() {
return Math.PI * radius * radius;
}
}
public class Rectangle extends Shape {
double width, height;
public Rectangle(double width, double height) {
this.width = width;
this.height = height;
}
@Override
public double area() {
return width * height;
}
}The super Keyword
super refers to the parent class:
java
public class Employee {
String name;
double salary;
public Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
public void display() {
System.out.printf("%s earns ₹%.2f%n", name, salary);
}
}
public class Manager extends Employee {
String department;
public Manager(String name, double salary, String department) {
super(name, salary); // Call parent constructor
this.department = department;
}
@Override
public void display() {
super.display(); // Call parent method
System.out.println("Department: " + department);
}
}Types of Inheritance in Java
| Type | Description | Supported? |
|---|---|---|
| Single | A extends B | ✓ Yes |
| Multilevel | C extends B extends A | ✓ Yes |
| Hierarchical | B, C both extend A | ✓ Yes |
| Multiple | A extends B, C (two parents) | ✗ No (use interfaces) |
Java does not support multiple inheritance with classes to avoid the "Diamond Problem." Use interfaces instead.
Practical Example
java
public class Person {
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void introduce() {
System.out.printf("Hi, I'm %s, %d years old.%n", name, age);
}
}
public class Student extends Person {
String course;
double gpa;
public Student(String name, int age, String course, double gpa) {
super(name, age);
this.course = course;
this.gpa = gpa;
}
@Override
public void introduce() {
super.introduce();
System.out.printf("I'm studying %s with GPA %.1f%n", course, gpa);
}
}Summary
- Inheritance lets a child class reuse parent class code with
extends - Use
@Overrideto override parent methods in the child class - The
superkeyword accesses the parent's constructor and methods - Java supports single, multilevel, and hierarchical inheritance
- Java does not support multiple class inheritance — use interfaces instead
- Inheritance models an "is-a" relationship: a Dog is an Animal