Introduction
Object-oriented programming is a programming paradigm based on the concept of “objects” (in Java, everything is an Object) that contain data in the form of fields that are commonly referred to as attributes; and code, in the form of procedures, usually referred to as methods.
These two topics come under OOPs (OOP – Object Oriented Programming). Inside Object-Oriented Programming, we learn how to build codes in which we can store both methods and data .Explore the difference between Abstraction and Encapsulation or abstraction vs encapsulation.
Before moving on to the question, let’s revise what these terms actually mean and the utilization of Abstraction and Encapsulation.
Also see, Duck Number in Java and Hashcode Method in Java
What is Abstraction?
The literal meaning of the word abstract is things that summarize or concentrate the essentials of a bigger thing or several things.
In the Abstraction process, we try and obtain an abstract view, model, or structure of a true-life problem and reduce its unnecessary details. With definition of properties of problems, including the info which are affected and therefore the operations which are identified, the model abstracted from problems may be a customary solution to the present kind of problems. It's an efficient way since there are nebulous real-life problems that have similar properties.
In simple terms, it's hiding the unnecessary details & showing only the essential parts/functionalities to the user.
Let us understand abstraction with a real-world example.
You have all the info of citizens of a country like their name, age, gender, favorite movie, favorite food, etc. You're asked to arrange a chart mentioning the total number of females, males, age, and full name. During this case, you merely need three data, and the rest all the information doesn't seem to be of any use. So, this process of choosing useful data and hiding unwanted data is abstraction.
Let us understand this idea via code:
//abstract class
abstract class Car {
abstract void accelerate();
}
//concrete class
class Suzuki extends Car {
void accelerate() {
System.out.println("Honda::retard");
}
}
class Main {
public static void main(String args[]) {
Car obj = new Suzuki(); //Car object =>contents of Suzuki
obj.accelerate(); //call the method
}
}
Output:
Honda::retard
Try this code by yourself on Online Java Compiler.