What is inheritance in java?

TechLoons
2 min readJul 29, 2023

In Java, inheritance is a fundamental concept of object-oriented programming (OOP) that allows a class to inherit properties and behaviors (i.e., fields and methods) from another class. The class that inherits from another class is known as the subclass or derived class, and the class being inherited from is called the superclass or base class. Inheritance creates an “is-a” relationship between classes, where a subclass is a specialized version of the superclass.

Inheritance is denoted in Java using the extends keyword. Here’s the basic syntax for defining a subclass that inherits from a superclass:

class Superclass {
// Fields and methods of the superclass
}

class Subclass extends Superclass {
// Fields and methods specific to the subclass
}

Key points about inheritance in Java:

  1. Code Reusability: Inheritance promotes code reusability by allowing the subclass to inherit the properties and behaviors of the superclass. This means that the common features can be defined once in the superclass and reused in multiple subclasses.
  2. Access Modifiers: The members (fields and methods) of a superclass may have different access modifiers (e.g., public, protected, private). The subclass can access the public and protected members of the superclass. However, the private members are not directly accessible in the subclass.
  3. Single Inheritance: Java supports single inheritance, which means a class can only inherit from one superclass. Unlike some other programming languages that support multiple inheritance, Java avoids complexities related to the “diamond problem.”
  4. Object Class: In Java, if a class doesn’t explicitly extend another class, it implicitly inherits from the Object class, which is the root of the class hierarchy in Java.
  5. Overriding Methods: Subclasses can override methods from the superclass to provide their own implementation. This allows customization of behavior while maintaining the same method signature.

Example of inheritance in Java:

class Animal {
void makeSound() {
System.out.println("Animal makes a sound");
}
}

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

In this example, the Dog class is a subclass of the Animal class. The Dog class inherits the makeSound() method from the Animal class but provides its own implementation to override the behavior of the superclass method. When you create an instance of Dog and call makeSound(), it will print “Dog barks” instead of “Animal makes a sound.”

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

TechLoons
TechLoons

Written by TechLoons

Welcome to TechLoons, your go-to source for the latest tips and information on a wide range of topics.

No responses yet

Write a response