- Java Programming Basics
- Java Tutorial
- Java Environment Setup
- Java Separators
- Java Data Types
- Java Variables
- Java Variable Scope
- Java Type Casting
- Java Operators
- Java Increment Decrement
- Java Left Shift
- Java Right Shift
- Java Bitwise Operators
- Java Ternary Operator
- Java Control Statements
- Java if-else statement
- Java for Loop
- Java while Loop
- Java do-while Loop
- Java switch Statement
- Java break Statement
- Java continue Statement
- Java Popular Topics
- Java Arrays
- Java Multidimensional Array
- Java Strings
- Java Methods
- Java Date and Time
- Java Exception Handling
- Java File Handling
- Java OOP
- Java Classes and Objects
- Java Constructors
- Java Constructor Overloading
- Java Object as Parameter
- Java Returning Objects
- Java Encapsulation
- Java Abstraction
- Java Inheritance
- Java Polymorphism
- Java Packages
- Java Import Statement
- Java Multithreading
- Java Suspend Resume Stop Thread
- Java Programming Examples
- Java Programming Examples
Abstraction in Java with an example
Abstraction is a fundamental principle of Java object-oriented programming (OOP). It is the practice of concealing the complexity of an object's implementation and only exposing a simplified interface through which other objects can interact. In other words, abstraction enables us to concentrate on the essential characteristics of an object while ignoring its implementation details.
We can use abstract classes and interfaces to implement abstraction in Java.
An abstract class is one that cannot be instantiated and is intended to serve as a base class from which other classes can inherit. It may contain one or more declared but undefined abstract methods that must be implemented by any class that inherits from it. Abstract classes are used to define shared behavior between related classes while allowing each class to implement its own distinct functionality.
To declare an abstract class in Java, use the "abstract" keyword. Here is the general form:
public abstract class ClassName {
// abstract methods and variables
}
An interface is a collection of abstract methods that define a contract for an object's behavior. It only contains method signatures and no implementation. A class can implement multiple interfaces, allowing it to behave like multiple objects. Interfaces are used to ensure consistency across different classes with similar behavior without requiring them to share the same base class.
To declare an abstract method in Java, use the "abstract" keyword again. Here is the general form:
public abstract void methodName();
Java's keyword "public" makes classes and methods available to the entire program, "abstract" indicates that a class or method is unfinished and must be implemented by a subclass, "class" declares a class, and "void" indicates that a method does not return a value.
Java abstraction example
Consider the following program as an example demonstrating the abstraction in Java.
// Abstract class Vehicle abstract class Vehicle { // Private instance variables brand and year private String brand; private int year; // Constructor to set brand and year public Vehicle(String brand, int year) { this.brand = brand; this.year = year; } // Getter methods to access brand and year public String getBrand() { return brand; } public int getYear() { return year; } // Abstract method start(), which is not implemented in this class abstract void start(); } // Concrete class Car, which extends the abstract class Vehicle class Car extends Vehicle { // Constructor to set brand and year using the superclass constructor public Car(String brand, int year) { super(brand, year); } // Implementation of the abstract method start() void start() { System.out.println("Starting the " + getBrand() + " car."); } } // Concrete class Motorcycle, which extends the abstract class Vehicle class Motorcycle extends Vehicle { // Constructor to set brand and year using the superclass constructor public Motorcycle(String brand, int year) { super(brand, year); } // Implementation of the abstract method start() void start() { System.out.println("Starting the " + getBrand() + " motorcycle."); } } // Main class AbstractionExample public class AbstractionExample { public static void main(String[] args) { // Create instances of Car and Motorcycle Vehicle car = new Car("Ford", 2023); Vehicle motorcycle = new Motorcycle("Harley Davidson", 2022); // Call the start() method on each object, without knowing the specific implementation details car.start(); motorcycle.start(); } }
Starting the Ford car. Starting the Harley Davidson motorcycle.
The main method in the AbstractionExample class creates instances of the Car and Motorcycle classes and calls their start() methods. The start() method is called on the Vehicle objects without knowing the specific details of how the method is implemented in each subclass.
This demonstrates the concept of abstraction, where we can define a common interface for objects (in this case, the Vehicle class with its start() method), without needing to know the specific implementation details of each individual object.
Abstraction is when an object's implementation details are kept secret while only a minimal interface is made available to other objects. Methods that are not implemented in an abstract class but are expected to be implemented by subclasses are called abstract. Hiding implementation details and defining a standard interface for objects are two ways in which abstraction facilitates code reuse, maintainability, and modularity.
Through the use of abstraction, we are able to create code that is portable across implementations of an interface or abstract class. Through the use of abstraction, complex systems can be designed with fewer moving parts that can be independently implemented and tested.
Advantages of abstraction in Java
- Abstraction facilitates code reuse, maintainability, and modularity by concealing implementation details and defining a common interface for objects.
- Abstraction enables developers to concentrate on an object's essential characteristics while ignoring its implementation details, which can simplify the development process and make code easier to comprehend.
- Complex systems can be designed using abstraction by decomposing them into smaller, more manageable components that can be independently implemented and tested.
- Abstraction can facilitate code updates and maintenance because modifications to the implementation of an abstract class or interface can be made without impacting the code that utilizes it.
- Abstraction can enhance the performance of code by enabling the use of polymorphism, which allows different objects to be treated as if they were of the same type.
Disadvantages of abstraction in Java
- By requiring the creation of abstract classes and interfaces, which can be labor-intensive to design and implement, abstraction can complicate the development process.
- Overusing abstraction can result in unnecessary abstraction that makes it more difficult to understand and maintain code.
- Performance problems can occasionally result from abstraction because polymorphism usage in some circumstances can add overhead.
- Because it can be more challenging to track down errors in an abstract class or interface, abstraction can make it more difficult to debug programs.
« Previous Tutorial Next Tutorial »