How to Achieve Abstraction in Java?31 Mar 2025 | 4 min read Object-oriented programming has four pillars that are an abstraction, polymorphism, encapsulation, and inheritance. In this section, we will discuss one of them, abstraction. Along with this, we can also learn how to achieve abstraction in Java. AbstractionAbstraction is a feature of OOPs. The feature allows us to hide the implementation detail from the user and shows only the functionality of the programming to the user. Because the user is not interested to know the implementation. It is also safe from the security point of view. Let's understand the abstraction with the help of a real-world example. The best example of abstraction is a car. When we derive a car, we do not know how is the car moving or how internal components are working? But we know how to derive a car. It means it is not necessary to know how the car is working, but it is important how to derive a car. The same is an abstraction. The same principle (as we have explained in the above example) also applied in Java programming and any OOPs. In the language of programming, the code implementation is hidden from the user and only the necessary functionality is shown or provided to the user. We can achieve abstraction in two ways:
Using Abstract ClassAbstract classes are the same as normal Java classes the difference is only that an abstract class uses abstract keyword while the normal Java class does not use. We use the abstract keyword before the class name to declare the class as abstract. Note: Using an abstract class, we can achieve 0-100% abstraction.Remember that, we cannot instantiate (create an object) an abstract class. An abstract class contains abstract methods as well as concrete methods. If we want to use an abstract class, we have to inherit it from the base class. If the class does not have the implementation of all the methods of the interface, we should declare the class as abstract. It provides complete abstraction. It means that fields are public static and final by default and methods are empty. The syntax of abstract class is: It is used to define generic types of behavior at the top of an OOPs class hierarchy and use its subclasses to provide implementation details of the abstract class. Let's see an example of an abstract class. MainClass.java Output: Abstract method called. Let's see another example of the abstract class in which we have used a non-abstract method. Owner.java Output: The car engine has been started. The car engine is not started. Using InterfaceIn Java, an interface is similar to Java classes. The difference is only that an interface contains empty methods (methods that do not have method implementation) and variables. In other words, it is a collection of abstract methods (the method that does not have a method body) and static constants. The important point about an interface is that each method is public and abstract and does not contain any constructor. Along with the abstraction, it also helps to achieve multiple inheritance. The implementation of these methods provided by the clients when they implement the interface. Note: Using interface, we can achieve 100% abstraction.Separating interface from implementation is one way to achieve abstraction. The Collection framework is an excellent example of it. Features of Interface:
Syntax: To use an interface in a class, Java provides a keyword called implements. We provide the necessary implementation of the method that we have declared in the interface. Let's see an example of an interface. Car.java Output: The car engine has been started. The car engine has been stopped. Next TopicLcm of two numbers in java |
Decagonal Numbers in Java
In this section, we will learn what is Decagonal number and also create Java programs to that calculates the Decagonal number. The Decagonal number program is frequently asked in Java coding interviews and academics. Decagonal Number A decagonal number is a figurate number, which is recursively defined as: D(n)...
5 min read
Java vs PHP
Difference Between Java and PHP Java and PHP are both the two most popular programming languages. There are so many differences and similarities between both of them. Let's understand both of them one by one, and after that, we understand the similarities and differences between them. Java Java is...
4 min read
Check if a Number Has Digits in the Given Order or Not in Java
Problem Statement Given a number n. The task is to check if the digits follow a given order (strictly increasing, decreasing, or another pattern). Example 1: Input: 1234 Output: YES Explanation: The digits are in strictly increasing order, so the number follows the required pattern. Example 2: Input: 4321 Output: YES Explanation: The digits are...
8 min read
Java Map Generic
Java is one of the most popular programming languages in the world, known for its versatility and wide range of applications. One of the most powerful features of Java is its collection framework, which includes classes and interfaces for managing collections of objects. One of the...
4 min read
How to Find the Percentage of Uppercase Letters, Lowercase Letters, Digits, and Special Characters in a String Using Java
? In Java, analyzing the composition of a string and calculating the percentage of different character types, such as uppercase letters, lowercase letters, digits, and special characters, can be achieved by utilizing string manipulation and character classification methods. This section will guide you through the process step-by-step,...
3 min read
Ambiguity in Java
Java, a versatile and widely-used programming language, boasts a plethora of features that contribute to its popularity among developers. However, as with any sophisticated tool, it comes with its own set of challenges. One of these challenges is ambiguity - a concept that can leave even...
4 min read
GoF Design Pattern Java
The book "Design Patterns: Elements of Reusable Object-Oriented Software" has 23 design patterns, which are grouped together as Gangs of Four Design Patterns. One of the most well-liked books to understand design patterns was first published in 1994. Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides...
3 min read
Java and Apache OpenNLP
In the ever-evolving landscape of technology, Natural Language Processing (NLP) plays a pivotal role in bridging the gap between human communication and computer understanding. Java, a versatile and widely-used programming language, offers developers the capability to harness the potential of NLP through various libraries and frameworks....
3 min read
Image Processing in Java - Contrast Enhancement
Digital image analysis and computer vision both heavily rely on image processing. In order to obtain the intended results, this calls for image alteration. Contrast enhancement is a fundamental method of image processing that highlights the things in the image so that they are more...
8 min read
Input from console in Java
Reading data from keyboard There are many ways to read data from the keyboard. For example: InputStreamReader Console Scanner DataInputStream etc. InputStreamReader class InputStreamReader class can be used to read data from keyboard.It performs two tasks: connects to input stream of keyboard converts the byte-oriented stream into character-oriented stream BufferedReader class BufferedReader class can be used...
1 min read
We request you to subscribe our newsletter for upcoming updates.

We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India