Last Updated: 3 December, 2023
Abstraction is one of the strong concepts of object-oriented programming (OOP). In this tutorial, we will learn all about abstraction in Java with examples.
Abstraction is a mechanism of hiding the implementation details and showing only functionality to the user. Abstraction enables developers to create modular, reusable, and maintainable code in Java.
One simple example of Abstraction, we use mobile phones every day, but we know only the functionality of the phones but not their internal details.
Code Reusability: Abstraction provides code reusability, which means we can encapsulate common functionality in abstract classes or interfaces and reuse them without replicating.
Maintainability: Abstraction improves code maintainability by reducing complexity and making it easier to modify code without affecting other parts of the system.
Modular Design: Abstraction helps in modular design in Java, complex system breaks down into smaller, independent modules that can be easily understood and maintained.
Simplicity: Abstraction simplifies the code by hiding unnecessary details, making it easier to understand and use.
📝 Abstraction is a cornerstone of object-oriented programming, and its effective use in Java leads to well-structured, maintainable, and reusable code. By understanding and applying abstraction principles, developers can create robust and scalable software applications.
There are two ways to achieve abstraction in Java:
Using the Interface we can achieve 100% abstraction (complete abstraction) whereas through the abstract class we can achieve 0-100% abstraction (partial or complete abstraction) depending on the implementation of the abstract class.
A class that is declared with an abstract keyword is known as an abstract class in Java. An abstract class cannot be instantiated, which means we cannot create the object of an abstract class.
An abstract class may or may not contain abstract methods (methods that do not have a body, such as public void display();). But, if a class has at least one abstract method, then the class must be declared as an abstract class.
To use an abstract class, we have to inherit it from another class and provide implementations for the abstract methods in it. It's mandatory to provide implementations for all the abstract methods in it. If any abstract method is not implemented, then that sub-class should be declared as ‘abstract’.
Syntax of Abstract Class
Abstract method in Java
A method that is declared with an abstract keyword is known as an abstract method in Java. An abstract method doesn't have its body in the abstract class we provide implementations of the abstract method in the subclass class only.
An Abstract Method is just a prototype for the method with the following attributes:
Example: An abstract method declared inside an abstract class
Let's see a complete example of an abstract class with abstract and non-abstract methods in Java program.
Example: An abstract class with abstract and non-abstract methods
Output
Mobile Common Features
RealMe7 Mobile - Base Features
RealMe7 Mobile - New Features
Mobile Common Features
RealMe8 Mobile - Base Features
RealMe8 Mobile - New Features
In Java, An interface is an abstract type that is used to specify a behavior that classes must implement. An Interface contains only method declaration(no method body) and constant variables without Constructor and static methods.
In Interface all methods by default public and abstract and all variables by default public, static and final.
Use the interface keyword we can create Interface in Java but we cannot create objects of Interface.
Syntax of Java Interface
Example: A sample Java program of Inheritance
Output
Cow eats grass
Cow runs slow
Cow has total legs: 4
That's all guys, hope this Java article is helpful for you.
Happy Learning... 😀
feedback@javabytechie.com