Abstraction In Java
Abstraction in Java
Data abstraction is the process of hiding certain details and displaying only essential information to the user. It means a class only has the essential attributes and methods that a user needs to know, and hides the rest of the information.
In this article we will be discussing about Abstraction in Java.
Data Abstraction
Data abstraction can also be defined as the process of identifying only the desired characteristics of an object, ignoring irrelevant details. The properties and behavior of an object distinguish it from other objects of a similar type and also help in classifying/grouping objects.
In Java, abstraction is achieved through interfaces and abstract classes. Using interfaces we can achieve 100% abstraction.
- Abstract class: is a restricted class that cannot be used to create objects (it must be inherited from another class to access it).
- Abstract method: can only be used in an abstract class and has no body. The body is provided by a subclass (inherited from).
Data abstraction can also be defined as the process of identifying only the desired characteristics of an object, ignoring irrelevant details. The properties and behavior of an object distinguish it from other objects of a similar type and also help in classifying/grouping objects.
Example:
import java.util.*; // class definition abstract class Human { // abstract function public abstract void Sound(); public void sleep() { System.out.println("Zzz"); } } // defining extended class class Men extends Human { public void Sound() { System.out.println("The Men says: PrepInsta"); } } class Main { public static void main(String[] args) { // Object of men class Men myMen = new Men(); // function calling myMen.Sound(); myMen.sleep(); } }
Output: The Men says: PrepInsta Zzz
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment