Abstraction in Java


JavaViews 5188

What is Abstraction

Abstraction in Java, which is one of the OOPs concepts, is the process of showing only the required information to the user by hiding other details. For example, consider a mobile phone, we just know that pressing on the call button, will dial the required number. But we actually don’t know the actual implementation of how a call works. This is because the internal mechanism is not visible to us. This is a real example of abstraction. Similarly, abstraction works in the same way in Object-oriented programming. In this tutorial, we will learn how to achieve abstraction using Java abstract classes and methods.

Ways to achieve Java Abstraction

We can achieve abstraction in java by below 2 ways.

Abstraction in Java

You can refer to Interface in Java tutorial to understand more about achieving abstraction using an interface.

Use of Abstraction

  • Provides security by hiding implementation details.
  • Provides flexibility for the subclass to implement the functionality of the inherited abstract method of the superclass.

Let’s discuss in detail about Java abstract class and methods.

Abstract class in Java

When we declare a class with an abstract keyword, we call it an abstract class. abstract is a non-access modifier keyword that we can use along with a class and method. An abstract class is a special class that is a superclass that contains unimplemented methods. Hence the subclass that inherits the parent abstract class should implement all the abstract methods.

Features of abstract class

  • The class must have an abstract keyword in the class definition
  • The abstract class can have abstract methods and non-abstract methods.
  • If a class has an abstract method, then it must be defined as an abstract class.
  • It can have constructors either default or parameterize.
  • Does not allow object creation for an abstract class.
  • It allows defining final and static methods as well
  • We can access the abstract class only through inheritance by using the extends keyword.
  • The class that extends the abstract class must implement all the abstract methods.
  • An abstract class can include variables as well.

Syntax of abstract class

abstract class class_name {
       abstract return_type methodname(); //abstract method
       return_type methodname() { //non-abstract method
       }
}

Example of Abstract class

Below is a simple example of an abstract class named Fruits that has taste as an abstract method. Since different fruits have different tastes, the class that extends the abstract class can have its own taste implementation. To implement the abstract class method, the class must use the extends keyword in the class definition.

abstract class Fruits
{
  abstract void taste();
}

class Apple extends Fruits {

  @Override
  void taste() {
    System.out.println("Sweet taste");
    
  }
  
}
public class AbstractDemo {

  public static void main(String[] args) {
    Apple a = new Apple();
    a.taste();

  }

}
Sweet taste

Let’s see another example where another class extends the same abstract class. Here we have another class Pineapple that extends Fruits. You can see that its implementation is different from that of the method in the Apple class. So now you can understand the main use of abstraction. Each subclass that inherits the parent abstract class has its own flexibility to implement the abstract method.

abstract class Fruits
{
  abstract void taste();
}

class Apple extends Fruits {

  @Override
  void taste() {
    System.out.println("Sweet taste");
    
  }
  
}

class Pineapple extends Fruits {

  @Override
  void taste() {
    System.out.println("Sour taste");
    
  }
  
}
public class AbstractDemo {

  public static void main(String[] args) {
    Apple a = new Apple();
    Pineapple p = new Pineapple();
    a.taste();
    p.taste();
  }

}
Sweet taste
Sour taste

Abstract Methods in Java

When we use the keyword abstract while declaring a method, we call it an abstract method. Abstract methods have only a function declaration and do not have method implementation. This means it contains only an empty body and there is no code inside the method. The class that inherits the abstract class implements the abstract method.

Features of the abstract method

  • We can use abstract methods only inside an abstract class.
  • The abstract method does not have any code within the method body.
  • We must use the keyword abstract to declare an abstract method.

Syntax of an abstract method

abstract returntype methodname();

Example of an abstract method

Let’s consider the same example we saw in the abstract class example above. Here taste is an abstract method that does not have any implementation.

abstract class Fruits
{
  abstract void taste(); //abstract method
}

class Apple extends Fruits {

  @Override
  void taste() {
    System.out.println("Sweet taste");
    
  }
  
}

public class AbstractDemo {

  public static void main(String[] args) {
    Apple a = new Apple();
    a.taste();
  }

}

Abstract Class with abstract and non-abstract methods

As we discussed earlier, an abstract class can have both abstract and non-abstract methods. This provides flexibility for the superclass to decide what method it owns and what methods the subclass can implement.

In the below example, taste is an abstract method, and getFruitName is a non-abstract method. Hence the subclass can directly use this method as part of the inheritance.

abstract class Fruits
{
  String fruitname;
  //abstract method
  abstract void taste();
  
  //non-abstract method
  public void getFruitName(String fruit ) {
    this.fruitname = fruit;
  }
}

class Apple extends Fruits {

  @Override
  void taste() {
    System.out.println("Sweet taste");
    
  }
  
}

public class AbstractDemo {

  public static void main(String[] args) {
    Apple a = new Apple();
    a.getFruitName("Apple");
    System.out.println("Fruit name: " + a.fruitname);
    a.taste();
  
  }

}
Fruit name: Apple
Sweet taste

Abstract class constructors

The abstract class also supports constructors. We can use either parameterized or default constructors. JVM implicitly calls default constructors as we have seen in inheritance.

In the below example, we have used parameterized constructor in the abstract class. We call this constructor from the subclass constructor using the super keyword. When the subclass constructor is called during object creation, it calls the Fruits constructor and executes that first. After which it executes the remaining statements in the Apple constructor.

abstract class Fruits
{
  String fruitname;
  String color;
  
  Fruits(String color) {
    this.color = color;
    System.out.println("Fruits constructor");
  }
  //abstract method
  abstract void taste();
  
  //non-abstract method
  public void getFruitName(String fruit ) {
    this.fruitname = fruit;
  }
}

class Apple extends Fruits {

  Apple(String color) {
    super(color);
    System.out.println("Apple constructor");
  }

  @Override
  void taste() {
    System.out.println("Sweet taste");
    
  }
  
}

public class AbstractDemo {

  public static void main(String[] args) {
    Apple a = new Apple("red");
    a.getFruitName("Apple");
    System.out.println("Fruit name: " + a.fruitname);
    System.out.println("Fruit color: " + a.color);
    a.taste();
  
  }

}
Fruits constructor
Apple constructor
Fruit name: Apple
Fruit color: red
Sweet taste

Difference between abstract class and interface

Now that we have understood about abstract class, let us see the difference between abstract class and interface.

Abstract classInterface
Abstract class can have both abstract and non-abstract methodsInterface can have only abstract methods. From Java 8, supports default methods
Does not support multiple inheritanceSupports multiple inheritance
Uses abstract keywordUses interface keyword
Uses extends keyword to inherit the abstract classUses implements keyword to implement the interface
It can extend another Java class and also implement interfaceIt can only extend another interface
Members can have access modifiers like private, protected, etcMembers can have only public
It can have static, non-static, final or non-final variables.It can have only static and final variables

 

Reference

Translate »