Abstraction in Java - GeeksforGeeks
Abstraction in Java - GeeksforGeeks
Abstraction in Java
Last Updated : 15 Dec, 2023
Abstraction in Java is the process in which we only show essential
details/functionality to the user. The non-essential implementation details are
not displayed to the user.
In this article, we will learn about abstraction and what abstract means.
AD
Data Abstraction may also be defined as the process of identifying only the
required characteristics of an object ignoring the irrelevant details. The
https://www.geeksforgeeks.org/abstraction-in-java-2/ 1/17
4/25/24, 6:25 PM Abstraction in Java - GeeksforGeeks
Consider a real-life example of a man driving a car. The man only knows
that pressing the accelerators will increase the speed of a car or applying
Java Arrays Java Strings Java OOPs Java Collection Java 8 Tutorial Java Multithreading Java Exception H
brakes will stop the car, but he does not know 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 the accelerator,
brakes, etc in the car. This is what abstraction is.
There are situations in which we will want to define a superclass that declares
the structure of a given abstraction without providing a complete
implementation of every method. Sometimes we will want to create a
superclass that only defines a generalization form that will be shared by all of
its subclasses, leaving it to each subclass to fill in the details.
Example 1:
https://www.geeksforgeeks.org/abstraction-in-java-2/ 3/17
4/25/24, 6:25 PM Abstraction in Java - GeeksforGeeks
Java
double length;
double width;
https://www.geeksforgeeks.org/abstraction-in-java-2/ 4/17
4/25/24, 6:25 PM Abstraction in Java - GeeksforGeeks
System.out.println(s1.toString());
System.out.println(s2.toString());
}
}
Output
Example 2:
Java
https://www.geeksforgeeks.org/abstraction-in-java-2/ 5/17
4/25/24, 6:25 PM Abstraction in Java - GeeksforGeeks
// Java Abstraction
// Abstracted class
class Dog extends Animal {
public Dog(String name) { super(name); }
// Abstracted class
class Cat extends Animal {
public Cat(String name) { super(name); }
// Driver Class
public class AbstractionExample {
// Main Function
public static void main(String[] args)
{
Animal myDog = new Dog("Buddy");
Animal myCat = new Cat("Fluffy");
myDog.makeSound();
myCat.makeSound();
}
}
Output
https://www.geeksforgeeks.org/abstraction-in-java-2/ 6/17
4/25/24, 6:25 PM Abstraction in Java - GeeksforGeeks
Buddy barks
Fluffy meows
Interface
Interfaces are another method of implementing abstraction in Java. The key
difference is that, by using interfaces, we can achieve 100% abstraction in Java
classes. In Java or any other language, interfaces include both methods and
variables but lack a method body. Apart from abstraction, interfaces can also
be used to implement interfaces in Java.
Java
https://www.geeksforgeeks.org/abstraction-in-java-2/ 7/17
4/25/24, 6:25 PM Abstraction in Java - GeeksforGeeks
Output
Advantages of Abstraction
Here are some advantages of abstraction:
1. Abstraction can make it more difficult to understand how the system works.
2. It can lead to increased complexity, especially if not used properly.
3. This may limit the flexibility of the implementation.
4. Abstraction can add unnecessary complexity to code if not used
appropriately, leading to increased development time and effort.
5. Abstraction can make it harder to debug and understand code, particularly
for those unfamiliar with the abstraction layers and implementation details.
https://www.geeksforgeeks.org/abstraction-in-java-2/ 9/17
4/25/24, 6:25 PM Abstraction in Java - GeeksforGeeks
Also Read:
Interfaces in Java
Abstract classes in Java
Difference between abstract class and interface
abstract keyword in Java
3. Frameworks and APIs: Java has numerous frameworks and APIs that
use abstract classes. By using abstract classes developers can save time
https://www.geeksforgeeks.org/abstraction-in-java-2/ 10/17
4/25/24, 6:25 PM Abstraction in Java - GeeksforGeeks
Encapsulation Abstraction
https://www.geeksforgeeks.org/abstraction-in-java-2/ 11/17
4/25/24, 6:25 PM Abstraction in Java - GeeksforGeeks
Here are some key difference b/w Abstract Classes and Interfaces in Java:
Example: Example:
Feeling lost in the vast world of Backend Development? It's time for a change!
Join our Java Backend Development - Live Course and embark on an exciting
journey to master backend development efficiently and on schedule.
What We Offer:
Comprehensive Course
Expert Guidance for Efficient Learning
Hands-on Experience with Real-world Projects
Proven Track Record with 100,000+ Successful Geeks
https://www.geeksforgeeks.org/abstraction-in-java-2/ 12/17
4/25/24, 6:25 PM Abstraction in Java - GeeksforGeeks
Previous Next
Similar Reads
Abstraction by Parameterization and Difference Between Data Hiding and
Specification in Java Abstraction in Java
G gaurav mi…
Article Tags : Java-Abstract Class and Interface , Java-Object Oriented , Java , Misc
Practice Tags : Java, Misc
https://www.geeksforgeeks.org/abstraction-in-java-2/ 13/17
4/25/24, 6:25 PM Abstraction in Java - GeeksforGeeks
Company Explore
About Us Hack-A-Thons
Legal GfG Weekly Contest
Careers DSA in JAVA/C++
In Media Master System Design
Contact Us Master CP
Advertise with us GeeksforGeeks Videos
GFG Corporate Solution Geeks Community
Placement Training Program
Languages DSA
Python Data Structures
Java Algorithms
C++ DSA for Beginners
PHP Basic DSA Problems
GoLang DSA Roadmap
SQL Top 100 DSA Interview Problems
R Language DSA Roadmap by Sandeep Jain
Android Tutorial All Cheat Sheets
Tutorials Archive
https://www.geeksforgeeks.org/abstraction-in-java-2/ 14/17
4/25/24, 6:25 PM Abstraction in Java - GeeksforGeeks
https://www.geeksforgeeks.org/abstraction-in-java-2/ 15/17
4/25/24, 6:25 PM Abstraction in Java - GeeksforGeeks
https://www.geeksforgeeks.org/abstraction-in-java-2/ 16/17
4/25/24, 6:25 PM Abstraction in Java - GeeksforGeeks
Linux CS Subjects
Excel
https://www.geeksforgeeks.org/abstraction-in-java-2/ 17/17