Java Inheritance
Creating new classes based on existing ones
🧬 What is Inheritance?
Inheritance allows a class to inherit properties and methods from another class. The child class gets all features of the parent class and can add new ones.
// Parent class
class Animal {
public void eat() {
System.out.println("Animal is eating");
}
}
// Child class inherits from Animal
class Dog extends Animal {
public void bark() {
System.out.println("Dog is barking");
}
}
Output:
Dog can both eat() and bark()
Key Inheritance Concepts
Parent Class
The class being inherited from
class Vehicle {
String brand;
}
Child Class
The class that inherits
class Car extends Vehicle {
int doors;
}
Method Override
Child class changes parent method
@Override
public void start() {
// New implementation
}
extends Keyword
Creates inheritance relationship
class Student extends Person {
// Student inherits from Person
}
🔹 Complete Inheritance Example
Here's a practical inheritance example:
// Parent class
class Person {
protected String name;
protected int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public void introduce() {
System.out.println("Hi, I'm " + name + ", age " + age);
}
public void sleep() {
System.out.println(name + " is sleeping");
}
}
// Child class
class Student extends Person {
private String school;
private String major;
public Student(String name, int age, String school, String major) {
super(name, age); // Call parent constructor
this.school = school;
this.major = major;
}
// Override parent method
@Override
public void introduce() {
System.out.println("Hi, I'm " + name + ", a " + major +
" student at " + school);
}
// New method specific to Student
public void study() {
System.out.println(name + " is studying " + major);
}
}
// Usage
public class Main {
public static void main(String[] args) {
Student student = new Student("Alice", 20, "MIT", "Computer Science");
student.introduce(); // Overridden method
student.study(); // Student-specific method
student.sleep(); // Inherited method
}
}
Output:
Hi, I'm Alice, a Computer Science student at MIT
Alice is studying Computer Science
Alice is sleeping
🔹 Types of Inheritance
Java supports different inheritance patterns:
🔸 Single Inheritance
class Animal { }
class Dog extends Animal { } // Dog inherits from Animal
🔸 Multilevel Inheritance
class Animal { }
class Mammal extends Animal { }
class Dog extends Mammal { } // Dog → Mammal → Animal
🔸 Hierarchical Inheritance
class Animal { }
class Dog extends Animal { }
class Cat extends Animal { } // Both inherit from Animal
Note: Java doesn't support multiple inheritance (one class extending multiple classes) to avoid complexity and conflicts.
🔹 Method Overriding Rules
- @Override annotation: Recommended for clarity
- Same method signature: Name, parameters, return type
- Access level: Cannot be more restrictive than parent
- final methods: Cannot be overridden
class Shape {
public void draw() {
System.out.println("Drawing a shape");
}
}
class Circle extends Shape {
@Override
public void draw() { // Overriding parent method
System.out.println("Drawing a circle");
}
public void calculateArea() { // New method
System.out.println("Calculating circle area");
}
}