Inheritance
Inheritance is the mechanism in Java by which one class is allowed to inherit the features (fields and methods) of another class.
Superclass:
A class whose features are inherited is known as superclass (or a base class or a parent class).
Subclass:
A class that inherits another class is known as subclass (derived class, extended class, or child class).
The subclass can add its own fields and methods in addition to the superclass fields and methods.
Reusability:
When we want to create a new class and there is already a class that includes some of the code that we want, we can derive our new class from the existing class.
By doing this, we are reusing the fields and methods of the existing class.
The keyword used for inheritance is extends.
The syntax is as follows:
class derived-class extends base-class {
//methods and fields
}
Abstraction
Data abstraction is the property by virtue of which only the essential details are displayed to the user.
The trivial or the non-essentials units are not displayed to the user.
For example, a car is viewed as a car rather than its individual components.
Consider a real-life example of a man driving a car.
The man only knows that pressing the accelerators will increase the speed of car or applying brakes will stop the car, but he does not know about how on pressing the accelerator the speed is actually increasing, he does not know about the inner mechanism of the car or the implementation of accelerator, brakes, etc. in the car.
This is what abstraction is.
In Java, abstraction is achieved by interfaces and abstract classes.
We can achieve 100% abstraction using interfaces.