Inheritance is an important concept in Object-Oriented Programming (OOP). It allows one class to inherit accessible fields and methods from another class, helping developers reuse and extend existing code.
Inheritance allows a new class to extend an existing class. The existing class is called the parent class or superclass, and the new class is called the child class or subclass.
class Animal {
void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog is barking");
}
public static void main(String[] args) {
Dog d1 = new Dog();
d1.eat();
d1.bark();
}
}
Animal is eating
Dog is barking
The Dog class inherits the accessible
eat() method from Animal.
For example, several vehicle classes may share common features:
class Vehicle {
void start() {
System.out.println("Vehicle started");
}
}
class Car extends Vehicle {
void drive() {
System.out.println("Car is driving");
}
}
The Car class can use the inherited
start() method and define its own
drive() method.
The basic syntax uses the extends keyword:
class Parent {
// Parent members
}
class Child extends Parent {
// Additional child members
}
Example:
class Person {
String name = "Rahul";
}
class Student extends Person {
int age = 20;
void display() {
System.out.println(name);
System.out.println(age);
}
public static void main(String[] args) {
Student s1 = new Student();
s1.display();
}
}
Rahul
20
Single inheritance occurs when one child class extends one parent class.
class Animal {
void eat() {
System.out.println("Eating food");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Barking");
}
public static void main(String[] args) {
Dog d1 = new Dog();
d1.eat();
d1.bark();
}
}
Eating food
Barking
Dog inherits from one direct superclass, Animal.
Multilevel inheritance occurs when a class extends another child class, creating a chain of inheritance.
class Animal {
void eat() {
System.out.println("Animal eats");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
class Puppy extends Dog {
void weep() {
System.out.println("Puppy weeps");
}
public static void main(String[] args) {
Puppy p1 = new Puppy();
p1.eat();
p1.bark();
p1.weep();
}
}
Animal eats
Dog barks
Puppy weeps
Puppy inherits accessible members through the Dog and Animal classes.
Hierarchical inheritance occurs when multiple child classes extend the same parent class.
class Animal {
void eat() {
System.out.println("Animal eats");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barks");
}
}
class Cat extends Animal {
void meow() {
System.out.println("Cat meows");
}
}
class Main {
public static void main(String[] args) {
Dog d1 = new Dog();
Cat c1 = new Cat();
d1.eat();
d1.bark();
c1.eat();
c1.meow();
}
}
Animal eats
Dog barks
Animal eats
Cat meows
Java does not allow a class to extend multiple classes. However, a class can implement multiple interfaces.
Invalid class inheritance:
// Invalid Java syntax
// class C extends A, B {
// }
Multiple inheritance through interfaces:
interface Printable {
void print();
}
interface Showable {
void show();
}
class Demo implements Printable, Showable {
public void print() {
System.out.println("Printing");
}
public void show() {
System.out.println("Showing");
}
public static void main(String[] args) {
Demo d1 = new Demo();
d1.print();
d1.show();
}
}
Printing
Showing
The super keyword can be used to access
accessible superclass members and invoke a
superclass constructor.
class Animal {
String name = "Animal";
void display() {
System.out.println("Parent method");
}
}
class Dog extends Animal {
String name = "Dog";
void show() {
System.out.println(super.name);
super.display();
}
public static void main(String[] args) {
Dog d1 = new Dog();
d1.show();
}
}
Animal
Parent method
Here, super.name accesses the accessible
superclass field, and super.display()
invokes the superclass method.
Method overriding occurs when a subclass provides its own implementation of an inherited instance method.
class Animal {
void sound() {
System.out.println("Animal makes a sound");
}
}
class Dog extends Animal {
@Override
void sound() {
System.out.println("Dog barks");
}
public static void main(String[] args) {
Dog d1 = new Dog();
d1.sound();
}
}
Dog barks
The overridden method in Dog runs when
d1.sound() is called.
Constructors are not inherited. However, creating a child object involves invoking a superclass constructor before the subclass constructor completes.
class Parent {
Parent() {
System.out.println("Parent constructor");
}
}
class Child extends Parent {
Child() {
System.out.println("Child constructor");
}
public static void main(String[] args) {
Child c1 = new Child();
}
}
Parent constructor
Child constructor
The child constructor implicitly invokes the superclass's no-argument constructor in this example.
Advantages:
Limitations and considerations:
class Employee {
String name;
double salary;
Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
void display() {
System.out.println("Name: " + name);
System.out.println("Salary: " + salary);
}
}
class Manager extends Employee {
String department;
Manager(String name, double salary, String department) {
super(name, salary);
this.department = department;
}
@Override
void display() {
super.display();
System.out.println("Department: " + department);
}
}
class Main {
public static void main(String[] args) {
Manager m1 = new Manager(
"Amit", 50000, "IT"
);
m1.display();
}
}
Name: Amit
Salary: 50000.0
Department: IT
Manager inherits the name, salary, and display functionality from Employee, then adds a department and overrides the display method.
super() to call a parent
constructor.
1. Which keyword is used to inherit a class?
2. Can a Java class extend two classes directly?
3. Which keyword can invoke a superclass constructor?