Inheritance in Java

Inheritance is a mechanism in Java that allows a new class to inherit properties and behaviors (methods) from an existing class, known as the superclass or parent class. The class that inherits these properties is called the subclass or child class. This relationship between classes forms an “is-a” hierarchy, where a subclass is essentially a specialized version of its superclass.

In the below PDF we discuss about Inheritance in Java  in detail in simple language, Hope this will help in better understanding.

Syntax of Inheritance in Java:

In Java, inheritance is achieved using the extends keyword. Here’s a basic syntax:

class Superclass {
// Superclass members
}

class Subclass extends Superclass {
// Subclass members
}

In this syntax:

  • Superclass is the name of the superclass.
  • Subclass is the name of the subclass.
  • The extends keyword establishes the inheritance relationship, indicating that Subclass inherits from Superclass.

Types of Inheritance in Java:

1. Single Inheritance:

Single inheritance refers to the concept where a subclass inherits from only one superclass.
In Java, each class can have only one direct superclass.
Example:

class Animal {
// Animal class properties and methods
}
class Dog extends Animal {
// Dog inherits from Animal
}

2. Multilevel Inheritance:

Multilevel inheritance involves a chain of inheritance where a subclass inherits from another subclass, which in turn inherits from a superclass.
Example:

class Animal {
// Animal class properties and methods
}
class Dog extends Animal {
// Dog inherits from Animal
}
class Bulldog extends Dog {
// Bulldog inherits from Dog
}

3. Hierarchical Inheritance:

Hierarchical inheritance occurs when multiple subclasses inherit from the same superclass.
It results in a hierarchical structure where one superclass is inherited by multiple subclasses.
Example:

class Animal {
// Animal class properties and methods
}

class Dog extends Animal {
// Dog inherits from Animal
}

class Cat extends Animal {
// Cat inherits from Animal
}

4. Multiple Inheritance (Through Interfaces):

Java does not support multiple inheritance of classes (i.e., inheriting from more than one superclass).
However, Java supports multiple inheritance through interfaces, where a class can implement multiple interfaces.
Interfaces provide a way to achieve multiple inheritance of behavior without the complexities associated with multiple inheritance of state.
Example:

interface Swimming {
void swim();
}
interface Flying {
void fly();
}
class Bird implements Flying {
// Bird implements Flying interface
}
class Fish implements Swimming {
// Fish implements Swimming interface
}

Conclusion

Inheritance is a powerful mechanism in Java that facilitates code reuse, promotes modularity, and enables polymorphic behavior. By understanding inheritance, you can design robust and maintainable Java applications with a clear hierarchical structure. Whether you’re building small projects or large-scale enterprise applications, mastering inheritance is essential for becoming a proficient Java developer. So, embrace the power of inheritance and unlock the full potential of object-oriented programming in Java.

Must Read: OOPs Concepts In Java, Classes & Objects in Java and Abstraction in Java

Related Question

Inheritance in Java is a mechanism by which a class (subclass) can acquire the properties and behaviors (methods and fields) of another class (superclass). It promotes code reusability and establishes a relationship between classes.

In Java, inheritance is implemented using the extends keyword. A subclass can extend a single superclass. This allows the subclass to inherit all non-private members of the superclass.

A superclass, also known as a parent class or base class, is the class being inherited from. A subclass, also known as a child class or derived class, is the class that inherits from the superclass.

Java supports single inheritance, where a subclass can only inherit from one superclass. However, it supports multilevel inheritance and hierarchical inheritance, where classes can be extended further down the hierarchy.

Method overriding occurs when a subclass provides a specific implementation of a method that is already provided by its superclass. The method signature in the subclass must match that of the superclass, and the subclass method can provide its own implementation.

Relevant

A Complete Guide to Java

Java Regex | Regular Expressions

Java Collection Framework The Java

Multithreading in Java Multithreading refers

File Handling in Java File

Exception Handling in Java An

Packages in Java A package

Leave a Comment

Your email address will not be published. Required fields are marked *

// Sticky ads
Your Poster