Introduction
Inheritance allows a class to inherit properties and methods from another class.
- Superclass (Parent/Base): Class being inherited from
- Subclass (Child/Derived): Class that inherits
Basic Concept
class Superclass {
// Fields and methods
}
class Subclass extends Superclass {
// Inherits from Superclass
// Can add own fields and methods
}
Simple Example
// Superclass
class Animal {
String name;
void eat() {
System.out.println(name + " is eating");
}
void sleep() {
System.out.println(name + " is sleeping");
}
}
// Subclass
class Dog extends Animal {
void bark() {
System.out.println(name + " is barking");
}
}
public class Main {
public static void main(String[] args) {
Dog dog = new Dog();
dog.name = "Buddy";
dog.eat(); // Inherited from Animal
dog.sleep(); // Inherited from Animal
dog.bark(); // Own method
}
}
Output:
Buddy is eating
Buddy is sleeping
Buddy is barking
Inheritance Hierarchy
Animal (Superclass)
|
+------+------+
| |
Dog Cat (Subclasses)
| |
Puppy Kitten (Sub-subclasses)
Complete Example
// Superclass
class Person {
String name;
int age;
void display() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
}
}
// Subclass
class Student extends Person {
int rollNo;
double marks;
void displayStudent() {
display(); // Call parent method
System.out.println("Roll No: " + rollNo);
System.out.println("Marks: " + marks);
}
}
public class Main {
public static void main(String[] args) {
Student s = new Student();
s.name = "John"; // Inherited field
s.age = 20; // Inherited field
s.rollNo = 101; // Own field
s.marks = 85.5; // Own field
s.displayStudent();
}
}
What is Inherited?
Inherited:
- Public members
- Protected members
- Default members (same package)
NOT Inherited:
- Private members
- Constructors
- Static members (belong to class, not inherited)
Accessing Inherited Members
class Parent {
int x = 10;
void show() {
System.out.println("Parent: x = " + x);
}
}
class Child extends Parent {
int y = 20;
void display() {
System.out.println("x (inherited): " + x);
System.out.println("y (own): " + y);
show(); // Call inherited method
}
}
public class Main {
public static void main(String[] args) {
Child c = new Child();
c.display();
}
}
Types of Inheritance in Java
1. Single Inheritance:
class A { }
class B extends A { } // B inherits from A
2. Multilevel Inheritance:
class A { }
class B extends A { }
class C extends B { } // C inherits from B, B from A
3. Hierarchical Inheritance:
class A { }
class B extends A { }
class C extends A { } // Both B and C inherit from A
NOT Supported:
// Multiple Inheritance (NOT allowed)
// class C extends A, B { } // ✗ Error
Java doesn’t support multiple inheritance with classes (to avoid Diamond Problem).
Real-World Example
// Superclass
class Vehicle {
String brand;
int year;
void start() {
System.out.println(brand + " is starting");
}
void stop() {
System.out.println(brand + " is stopping");
}
}
// Subclass 1
class Car extends Vehicle {
int doors;
void openTrunk() {
System.out.println("Trunk opened");
}
}
// Subclass 2
class Bike extends Vehicle {
boolean hasCarrier;
void ringBell() {
System.out.println("Ring ring!");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car();
car.brand = "Toyota";
car.year = 2020;
car.doors = 4;
car.start(); // Inherited
car.openTrunk(); // Own method
System.out.println();
Bike bike = new Bike();
bike.brand = "Hero";
bike.year = 2021;
bike.hasCarrier = true;
bike.start(); // Inherited
bike.ringBell(); // Own method
}
}
extends Keyword
class Subclass extends Superclass {
// Subclass code
}
- extends creates inheritance relationship
- Only one class can be extended (single inheritance)
- All non-private members inherited
Object Class
Every class in Java inherits from Object class (automatically).
class MyClass { }
// Automatically: class MyClass extends Object { }
Object class provides:
toString()equals()hashCode()getClass()- etc.
IS-A Relationship
Inheritance represents IS-A relationship.
class Animal { }
class Dog extends Animal { }
// Dog IS-A Animal ✓
// Animal IS-A Dog ✗
Examples:
- Student IS-A Person ✓
- Car IS-A Vehicle ✓
- Manager IS-A Employee ✓
Benefits of Inheritance
- Code Reusability: Reuse existing code
- Method Overriding: Modify inherited behavior
- Polymorphism: One interface, multiple implementations
- Extensibility: Add features to existing classes
- Maintainability: Change in one place
Multilevel Inheritance Example
class GrandParent {
void display1() {
System.out.println("GrandParent");
}
}
class Parent extends GrandParent {
void display2() {
System.out.println("Parent");
}
}
class Child extends Parent {
void display3() {
System.out.println("Child");
}
}
public class Main {
public static void main(String[] args) {
Child c = new Child();
c.display1(); // From GrandParent
c.display2(); // From Parent
c.display3(); // Own method
}
}
Hierarchical Inheritance Example
class Shape {
void draw() {
System.out.println("Drawing 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 Main {
public static void main(String[] args) {
Circle c = new Circle();
c.draw(); // Inherited
c.drawCircle(); // Own
Rectangle r = new Rectangle();
r.draw(); // Inherited
r.drawRectangle(); // Own
}
}
Quick Reference
// Superclass
class Parent {
int x;
void method1() { }
}
// Subclass
class Child extends Parent {
int y;
void method2() { }
// Inherits: x, method1()
}
// Usage
Child c = new Child();
c.x = 10; // Inherited field
c.y = 20; // Own field
c.method1(); // Inherited method
c.method2(); // Own method
Common Mistakes
1. Multiple Inheritance:
// ✗ Wrong
class C extends A, B { } // Not allowed
2. Accessing Private Members:
class Parent {
private int x = 10;
}
class Child extends Parent {
void display() {
// System.out.println(x); // ✗ Error: x is private
}
}
Exam Tips
Remember:
- Use extends keyword for inheritance
- Single inheritance only (one superclass)
- All classes inherit from Object
- Private members not inherited
- Constructors not inherited
- Represents IS-A relationship
- Code reusability main benefit
- Subclass can add own members
- Can access public/protected members
- Multilevel and hierarchical supported
Common Questions:
- What is inheritance?
- Types of inheritance?
- What is inherited?
- What is not inherited?
- extends keyword?
- IS-A relationship?
- Benefits of inheritance?
- Multiple inheritance in Java?