To reduce complexity, abstraction in Object-Oriented Programming refers to showing only the essential features of an object to the user and hiding the inner details. In other words, the user only needs to know “what an object does?” rather than “how it does?”
Example
![]() |
Image by Author |
The above sketch is a good real-world example of abstraction.
A user can only use and connect with the application’s limited functionality and is uninformed of the system architecture or how the application was built up. Typically, users are only involved with an application’s functionality.
Nothing at all is hidden from an administrator, who has access to many more features of the application. The administrator can monitor user activity, understand how the application was created, and implement new features by implementing them in the system.
In the previous example, the abstraction is implemented to the user but not to the administrator.
![]() |
Image by Author |
Let’s take a look at another example of abstraction. Take a look at the Volume control on a television remote. We ask the television to turn up the volume with a single click of a button. Assume the button invokes the volumeUp() function. The television responds by making a louder sound than before. We have no idea how the T.V.’s inner circuitry implements this, but we do know the exposed function needed to communicate with the T.V.’s volume.
An Example from Java
One can easily see abstraction in action in Java. Let’s look at the Java Math class as an example. This class contains a plethora of built-in methods that can be used by the programmer to get things done.
Let’s use the following methods in our code to access the built-in functionality.
You can see the output if you execute this program in your machine too.
Explanation of the above code
To find the smallest of two numbers, use the Math.min() method.
To find 2 to the power of 3, use the Math.pow() method.
However, the user is not required to be aware of the implementation of these two methods within the Math class.
Abstract Data Types
An abstract data type (or class) is one that only defines ‘what functions are to be performed?’ rather than ‘how functions are to be performed?’
The concept of abstract data types only provides users with the essentials, namely the functionality of those data types, while the ‘how the implementation should be done to accomplish the required functionality?’ part remains hidden.
An example of an abstract data type is a built-in stack class in Java, for which the user is aware of the push(), pop(), size(), and other methods but is unaware of how they are implemented.
How to Reach Abstraction?
To achieve abstraction in Java, we use the following components:
1. Interfaces
2. Abstract Classes
Keyword: abstract
Abstraction is impossible to achieve in Java without the use of the abstract keyword. The abstract keyword can only be used with methods and classes.
When the keyword abstract is used with classes or methods, those methods or classes only specify “what operations should be done,” and whoever uses this method or class in their code must deal with the method or class’s implementation details.
Isn’t the definition of abstraction the same? That is, to tell someone what should be done and how it is to be done is another story.
Abstract Classes and Methods
Abstract Methods
Definition
A method with the keyword abstract in its declaration is known as an abstract method.
Rules to consider
In contrast to a concrete/normal Java method, an abstract method lacks a body/definition, instead consisting of only a declaration or method signature within an abstract class or an interface (more on these later).
An abstract method can only be declared within an abstract class or interface.
In other words, in order to implement any abstract method, a class must be declared as an abstract class, because non-abstract classes cannot have abstract methods.
Because an abstract method must be implemented in another class, it cannot be declared private.
Declaration
Moving on to the syntax, the generalized declaration of an abstract method looks like this.
public abstract void methodName(parameter(s));
The declaration of an abstract method includes,
1. An access identifier
2. The keyword abstract
3. A return type
4. A name of the method
5. The parameter(s) to be passed
6. A semicolon(;) to end the declaration
At this point, one may ask about the definition or body of an abstract method, e.g., “Where do we implement the body of an abstract method?”
Abstract Classes
Definition
An abstract class is one that is declared with the keyword abstract.
Rules to consider
An abstract class cannot be instantiated, that is, an object of an abstract class cannot be created.
An abstract class can have the declaration of abstract method(s), but it is not required to have any (because the body of an abstract method cannot be implemented in an abstract class).
In an abstract class, non-abstract/normal methods can be implemented.
An abstract class must be inherited in order to be used.
The abstract class’s inheriting class must implement all of the abstract methods declared in the parent abstract class.
Everything else in an abstract class is the same as it is in a regular Java class, such as a constructor, static variables, and methods.
Declaration
In terms of syntax, the following is the declaration of an abstract class in Java.
abstract class ClassName {
// Implementation here
}
Implementation
In Java, abstract classes are being used to achieve abstraction. Consider modeling an animal kingdom with Java.
1. A base abstract class named Animal
2. A child class named Dog
3. A child class named Cat
4. A child class named Sheep
All of these animals make different sounds.
In the preceding example, one can see that all of the animals’ common traits should be implemented in the Animal class. All other type-specific traits should be implemented within the child's classes. The abstract classes provide the programmer with the same functionality.
Let’s implement this.
We can see from the above example how useful an abstract class can be,
Because all animals can move, and this is a common trait, the move() method is implemented in the Animal class, and all child classes can use it without any implementation.
Because all of the animals make different sounds, an abstract method is declared in the Animal class, and all of the child classes must @Override this method in their own unique ways.
Hope this can help. Share your thoughts too.
Comments
Post a Comment