Inheritance and Abstraction


Inheritance
Inheritance is the mechanism in Java by which one class is allowed to inherit the features (fields and methods) of another 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.




      β€œIt is not that I’m so smart. But I stay with the questions much longer.”    
      ― Albert Einstein