Java Inheritance
Inheritance is a fundamental concept in object-oriented programming that allows you to create new classes based on existing classes. In Java, inheritance is achieved through the use of the "extends" keyword.
The idea behind inheritance is to create a new class that inherits the attributes and behavior of an existing class. The new class is called a subclass or derived class, and the existing class is called the superclass or base class.
For example, consider the following classes:
class Animal {
int age;
String name;
public void eat() {
System.out.println("Animal is eating");
}
}
class Dog extends Animal {
int height;
public void bark() {
System.out.println("Dog is barking");
}
}
In this example, the class Dog
is a subclass of the class Animal
. The Dog
class inherits all the attributes and methods of the Animal
class, including the age
and name
instance variables and the eat
method. Additionally, the Dog
class has its own instance variable height
and its own method bark
.
One of the main benefits of inheritance is code reuse. By inheriting from an existing class, you can reuse its code, which can save time and effort. Additionally, inheritance makes it easier to create and maintain large and complex systems, because it enables you to create new classes that are based on existing classes, and it allows you to override methods in the subclass as needed.
In Java, you can also use inheritance to create a hierarchy of classes, where each subclass inherits from its superclass, and so on. This hierarchy can be used to model real-world relationships between objects, and it makes it easier to understand and maintain your code.
Inheritance in Java is a mechanism that allows a class to inherit properties and methods of another class, thereby promoting code reuse and modularity.
There are five types of inheritance in Java:
Single Inheritance
When a class extends only one class, it is called Single Inheritance. For example, class B extends class A, and class B inherits the properties and methods of class A.
Multi-level Inheritance
When a class extends another class, which in turn extends another class, it is called Multi-level Inheritance. For example, class C extends class B, and class B extends class A, so class C inherits the properties and methods of both class B and class A.
Hierarchical Inheritance
When multiple classes inherit from a single class, it is called Hierarchical Inheritance. For example, class B and class C extend class A, so both class B and class C inherit the properties and methods of class A.
Multiple Inheritance
When a class extends multiple classes, it is called Multiple Inheritance. However, Java does not support multiple inheritance due to the "Diamond Problem" which occurs when two classes have a common parent and a subclass inherits properties from both classes.
Hybrid Inheritance
When a combination of more than one type of inheritance is used, it is called Hybrid Inheritance. For example, a class can inherit from multiple classes using Hierarchical Inheritance and also implement an interface to inherit its properties.
class Animal {
void eat() {
System.out.println("Animal eating");
}
}
class Dog extends Animal {
void bark() {
System.out.println("Dog barking");
}
}
class Labrador extends Dog {
void play() {
System.out.println("Labrador playing");
}
}
Here, the Labrador class inherits the properties and methods of both Animal and Dog classes using Multi-level Inheritance.
Here are some of the commonly used terms in Inheritance in Java:
-
-
Superclass: A class that is being inherited from.
-
Subclass: A class that inherits from a superclass.
-
Method Overriding: The process of providing a different implementation of a method in a subclass that is already defined in its superclass.
-
Method Overloading: The process of having multiple methods with the same name but different parameters in a class.
-
Constructor Overriding: The process of providing a different implementation of a constructor in a subclass that is already defined in its superclass.
-
Base class: Another term for the superclass.
-
Derived class: Another term for the subclass.
-
Reusability: The ability of the subclass to inherit and use the properties and methods of its superclass, thereby reducing code duplication and increasing code maintainability.
-
IS-A relationship: A relationship between a subclass and its superclass where the subclass "is a" type of the superclass.
-
Has-A relationship: A relationship between two classes where one class has an instance of another class as a member variable.
-