DEV Community

Kanishka Shrivastava
Kanishka Shrivastava

Posted on

#java #oop #inheritance #programming

Java Concepts I’m Mastering – Part 9: Inheritance in Java (extends Keyword)

Continuing my journey of mastering Java fundamentals.

Today’s focus: Inheritance — the mechanism that allows one class to acquire properties and behavior of another class.

What is Inheritance?

Inheritance means:

A child class can reuse the fields and methods of a parent class.

This promotes:

Code reusability

Clean hierarchy

Logical relationships

Syntax


class ChildClass extends ParentClass {
    // additional features
}

Enter fullscreen mode Exit fullscreen mode

Example

class Animal {
    void eat() {
        System.out.println("This animal eats food");
    }
}

class Dog extends Animal {
    void bark() {
        System.out.println("Dog barks");
    }
}

Enter fullscreen mode Exit fullscreen mode

Now:

Dog d = new Dog();
d.eat();   // inherited
d.bark();  // own method

Enter fullscreen mode Exit fullscreen mode

The Dog class reuses behavior from Animal.

Types of Inheritance in Java

Single

Multilevel

Hierarchical

(Java does not support multiple inheritance with classes — but it does through interfaces.)

Why It Matters

Inheritance helps in:

Building scalable systems

Reducing code duplication

Implementing runtime polymorphism

It works closely with method overriding, which I covered earlier in this series.

What I Learned

Use inheritance for “is-a” relationships

Avoid unnecessary deep hierarchies

Combine with abstraction for clean design

Inheritance is a core building block of object-oriented design.

Next in the series: Final Keyword in Java (final variable, method, class)

Top comments (0)