Abstraction in Java

Abstraction is a fundamental concept in object-oriented programming that refers to the process of hiding implementation details while showing only the necessary information to the user. Abstraction in Java can be achieved through interfaces and abstract classes.

Interfaces

An interface is a collection of abstract methods that define a set of behaviors that a class must implement. An interface does not contain any implementation of the methods; it only specifies the methods that a class should implement. In Java, an interface is declared using the interface keyword.

Here is an example of an interface in Java:

interface Animal {
  public void eat();
  public void sleep();
}
class Dog implements Animal {
  public void eat() {
    System.out.println("The dog is eating");
  }
  public void sleep() {
    System.out.println("The dog is sleeping");
  }
}
public class TestDog {
  public static void main(String args[]) {
    Animal d = new Dog();
    d.eat();
    d.sleep();
  }
}

In the above example, we have defined an interface Animal with two methods eat() and sleep(). The Dog class implements the Animal interface and provides its own implementation of the eat() and sleep() methods.

In the main() method, we create an object of the Dog class and assign it to a variable of type Animal. We then call the eat() and sleep() methods on the Animal object. Since the Dog class implements the Animal interface, the appropriate implementations of the methods are called.

The output of the program will be:

The dog is eating
The dog is sleeping

Abstract Classes

An abstract class is a class that cannot be instantiated but can be subclassed. An abstract class can contain both abstract and non-abstract methods. Abstract methods are declared without any implementation, and they must be implemented by the subclass. In Java, an abstract class is declared using the abstract keyword.

Here is an example of an abstract class in Java:

abstract class Animal {
  public void eat() {
    System.out.println("The animal is eating");
  }
  public abstract void sleep();
}
class Dog extends Animal {
  public void sleep() {
    System.out.println("The dog is sleeping");
  }
}
public class TestDog {
  public static void main(String args[]) {
    Animal d = new Dog();
    d.eat();
    d.sleep();
  }
}

In the above example, we have defined an abstract class Animal with two methods eat() and sleep(). The eat() method is a non-abstract method that has a default implementation in the Animal class. The sleep() method is an abstract method that does not have any implementation in the Animal class.

The Dog class extends the Animal class and provides its own implementation of the sleep() method.

In the main() method, we create an object of the Dog class and assign it to a variable of type Animal. We then call the eat() and sleep() methods on the Animal object. Since the Dog class extends the Animal class, it provides the implementation for the sleep() method.

The output of the program will be:

The animal is eating
The dog is sleeping

Thus, we have seen how abstraction can be achieved in Java through interfaces and abstract classes.