Abstraction is a fundamental concept in Java programming that allows developers to create complex systems by simplifying them into manageable parts. In this tutorial, we will explore abstraction in Java, from its definition and principles to practical examples and real-world applications.
Abstraction is the process of simplifying complex systems by focusing on essential features while hiding irrelevant details. It enhances the clarity and efficiency of code.
An abstract class is a class in Java that cannot be instantiated on its own. It serves as a blueprint for other classes and is often used to define common methods and fields that subclasses will inherit.
A class which is declared as abstract is known as an abstract class. It can have abstract and non-abstract methods. It needs to be extended and its method implemented. It cannot be instantiated.
Key Characteristics: Abstract classes can contain both abstract methods (methods without a body) and concrete methods (methods with implementation). They are declared using the abstract keyword.
Syntax: To declare an abstract class, you use the abstract keyword followed by the class keyword, just like a regular class declaration.
abstract class MyClassName{ }
The important points to remember about abstract classes in Java as follows:
// Abstract class definition abstract class Animal { String name; // Constructor public Animal(String name) { this.name = name; } // Abstract method (no implementation) abstract void makeSound(); // Concrete method void sleep() { System.out.println(name + " is sleeping."); } } // Subclass Dog class Dog extends Animal { public Dog(String name) { super(name); } // Implementing the abstract method void makeSound() { System.out.println(name + " says Woof!"); } } // Subclass Cat class Cat extends Animal { public Cat(String name) { super(name); } // Implementing the abstract method void makeSound() { System.out.println(name + " says Meow!"); } } public class Main { public static void main(String[] args) { Dog dog = new Dog("Buddy"); Cat cat = new Cat("Whiskers"); dog.makeSound(); // Output: Buddy says Woof! cat.makeSound(); // Output: Whiskers says Meow! dog.sleep(); // Output: Buddy is sleeping. cat.sleep(); // Output: Whiskers is sleeping. } }
In this example:
Abstract methods are methods declared in an abstract class without providing an implementation. Subclasses are required to implement these methods.
Let's provide a short and straightforward example to demonstrate how to implement abstract methods in subclasses.
Suppose we have an abstract class Shape with an abstract method area() that calculates the area of different shapes. We want to create concrete subclasses for specific shapes like Circle and Rectangle and provide implementations for the area() method.
// Abstract class definition abstract class Shape { // Abstract method to calculate area abstract double area(); } // Subclass Circle class Circle extends Shape { private double radius; // Constructor public Circle(double radius) { this.radius = radius; } // Implementing the abstract method double area() { return Math.PI * radius * radius; } } // Subclass Rectangle class Rectangle extends Shape { private double length; private double width; // Constructor public Rectangle(double length, double width) { this.length = length; this.width = width; } // Implementing the abstract method double area() { return length * width; } } public class Main { public static void main(String[] args) { Circle circle = new Circle(5.0); Rectangle rectangle = new Rectangle(4.0, 6.0); System.out.println("Circle Area: " + circle.area()); // Output: Circle Area: 78.53981633974483 System.out.println("Rectangle Area: " + rectangle.area()); // Output: Rectangle Area: 24.0 } }
In this example:
Purpose: Abstract classes and methods are used to hide complex implementation details and only reveal the essential parts of an object.
Security: They enhance security by allowing you to hide the inner workings of an object, exposing only what's necessary.
When: