Inheritance is a concept in object-oriented programming (OOP) that allows a class to inherit properties and methods from another class. The class that is being inherited from is called the base class or the parent class, and the class that is inheriting from it is called the derived class or the child class.
The idea behind inheritance is that a derived class can reuse code from the base class, and can also add its own code on top of it. This can help to reduce code duplication and increase code reusability.
When a derived class inherits from a base class, it automatically gets all the properties and methods of the base class. The derived class can then override or extend these properties and methods as needed.
Inheritance in Java is a principle of Object-Oriented Programming (OOP) that allows one class to inherit properties and methods from another class. This functionality enables programmers to create new classes that reuse, extend, and modify the behavior defined in other classes.
Here are the key terms and concepts involved:
Superclass (or Parent class): This is the class from which another class inherits properties and methods.
Subclass (or Child class): This is the class that inherits properties and methods from another class. A subclass can also add new fields and methods, or it can override the methods of the superclass.
Inheritance: Inheritance represents the IS-A relationship, also known as parent-child relationship.
When a subclass inherits from a superclass, it gets:
All the non-private fields and methods from the superclass.
If a superclass does not have a no-arg (no argument) constructor, the compiler requires an explicit call to a superclass's constructor in each subclass constructor.
The subclass can still define its own fields and methods, including methods that have the same name as methods in the superclass (which is known as method overriding).
The subclass can also have its own constructors.
The key benefit of inheritance is that it allows for code reuse. Instead of writing the same code over and over again for similar objects, you can create a general class that includes common features and then create more specific classes that inherit from the general class while adding unique features of their own.
Java only supports single inheritance (a class can only inherit from one superclass), but it supports multiple levels of inheritance (where a class inherits from a subclass which inherits from another subclass and so on). Multiple inheritance is not supported in Java because of the "Diamond Problem," which can lead to ambiguity when a class would inherit from two classes that have a method with the same name.
Finally, remember that Java provides a mechanism to prevent a class from being inherited, by using the keyword final
. When a class is declared as final
, it cannot be subclassed. This is often done for reasons of security or efficiency.
Real-life objects are typically specialized versions of other more general objects.
The term “insect” describes a very general type of creature with numerous characteristics.
Grasshoppers and bumblebees are insects
They share the general characteristics of an insect.
However, they have special characteristics of their own.
grasshoppers have a jumping ability, and
bumblebees have a stinger.
Grasshoppers and bumblebees are specialized versions of an insect.
The “is a” relationship
The relationship between a superclass and an inherited class is called an “is a” relationship.
A grasshopper “is a” insect.
A poodle “is a” dog.
A car “is a” vehicle.
A specialized object has:
all of the characteristics of the general object, plus
additional characteristics that make it special.
In object-oriented programming, inheritance is used to create an “is a” relationship among classes
We can extend the capabilities of a class.
Inheritance involves a superclass and a subclass.
The superclass is the general class and
the subclass is the specialized class.
The subclass is based on, or extended from, the superclass.
Superclasses are also called base classes, and
subclasses are also called derived classes.
The relationship of classes can be thought of as parent classes and child classes.