Abstraction is one of the fundamental concepts of object-oriented programming (OOP). It is the process of hiding the implementation details of a class and exposing only the necessary information to the user. In Java, abstraction is achieved through abstract classes and interfaces.
Abstraction is used to create a simplified view of a complex system. In Java, abstraction is achieved through abstract classes and interfaces. An abstract class is a class that cannot be instantiated, meaning you cannot create an object of that class. Instead, it is used as a base class for other classes to inherit from. An abstract class can have both abstract and non-abstract methods. An abstract method is a method that does not have a body and must be implemented by the subclass that inherits from the abstract class.
Interfaces are similar to abstract classes in that they cannot be instantiated. However, they only contain abstract methods and constants. A class can implement multiple interfaces, but can only inherit from one class. Interfaces are used to define a contract that a class must follow. If a class implements an interface, it must implement all the methods defined in that interface.
Here are some examples of how abstraction is used in Java:
abstract class Animal {
public abstract void makeSound();
public void eat() {
System.out.println("I am eating.");
}
}
class Dog extends Animal {
public void makeSound() {
System.out.println("Woof!");
}
}
class Cat extends Animal {
public void makeSound() {
System.out.println("Meow!");
}
}
public class Main {
public static void main(String[] args) {
Animal dog = new Dog();
Animal cat = new Cat();
dog.makeSound();
cat.makeSound();
dog.eat();
cat.eat();
}
}
In this example, the Animal class is an abstract class that defines the makeSound() method as abstract. The Dog and Cat classes inherit from the Animal class and implement the makeSound() method. The Main class creates objects of the Dog and Cat classes and calls their makeSound() and eat() methods.
interface Shape {
double getArea();
double getPerimeter();
}
class Circle implements Shape {
private double radius;
public Circle(double radius) {
this.radius = radius;
}
public double getArea() {
return Math.PI * radius * radius;
}
public double getPerimeter() {
return 2 * Math.PI * radius;
}
}
class Rectangle implements Shape {
private double length;
private double width;
public Rectangle(double length, double width) {
this.length = length;
this.width = width;
}
public double getArea() {
return length * width;
}
public double getPerimeter() {
return 2 * (length + width);
}
}
public class Main {
public static void main(String[] args) {
Shape circle = new Circle(5);
Shape rectangle = new Rectangle(4, 6);
System.out.println("Circle area: " + circle.getArea());
System.out.println("Circle perimeter: " + circle.getPerimeter());
System.out.println("Rectangle area: " + rectangle.getArea());
System.out.println("Rectangle perimeter: " + rectangle.getPerimeter());
}
}
In this example, the Shape interface defines the getArea() and getPerimeter() methods. The Circle and Rectangle classes implement the Shape interface and provide their own implementations of the methods. The Main class creates objects of the Circle and Rectangle classes and calls their getArea() and getPerimeter() methods.