Abstract class in Java

Creating an abstract class as simple as creating a simple Java class but it contains abstract keyword.

Like, you may have a class called A and it is abstract class. Then it should be like the following


abstract class A(){}

Why should we need abstract class in java?
Sometimes we don't want to reveal the internal details to our users than only show the essential functionality, in this case we need to create abstract class.
Such as, we send a message someone and he/she receives the message but both parties don't need to know the internal message transmission policy.
Another example may be, we buy something from a shop and pay the bill through credit or debit card. User's don't need to know what's going on behind the scene.( means, the internal functionality of credit or debit cards)

The abstract class may contain fields (variables), abstract or non abstract methods as well. Even constructor or static methods too. But the method body will be provided by the implemented class.

Points to note about Abstract class: 1. An abstract class must declare with the abstract keyword. 2. It may contain abstract or non abstract methods, fields, constructors etc. (class members) 3. Even, an abstract class may contain final data members like final methods.

# Declaring Abstract class:


abstract class A(){
//abstract or non abstract members
//But abstract methods don't have body
}

But, note that the method body must be provided by the sub class or inherited class.(The inherited class can't be abstract then). It means, the abstract class can't have main method.

# Look at the example below:
In this example, we have an abstract class Car and it has an abstract method drive(); this method doesn't have any body, as the method body will be provided in the derived class.

Here is the first example:


abstract class Car{
    //abstract method: doesn't have body
    abstract void drive();
  }
  
  //now create a main class file
  public class Main extends Car{
    //override the abstract method here
    @Override 
    public void drive(){
      System.out.println("Drive slowly!")
    }
    
    public static void main(String [] arg){
      
      //create an object of the Main class now
      Main obj = new Main();
      obj.drive();
      
    }
  }
  /**
   * Output:
   * Drive Slowly!
   */

If you already know about inheritance basics I mean, parent child class in java, then the concept of abstract can be very easy to understand. The implementation (body) of the abstract class data members (methods or fields) will be provided by subclass. In the above example, the Main class is the subclass.

Let's look at another example.

In this example we have an abstract class called Car, and it has an abstract method called showPrice(); the method implementation or method body will be provided by our three more sub-classes or inherited classes.

So, create a class file called TestClass into your favourite ide. Here, the TestClass contains the main method and it should be public class. Now outside the TestClass create Car abstract class and an abstract method called showPrice(); It returns int (numeric value).

Note: Abstract class may contain non abstract methods (methods which have body) as well.

Look over full example now:


abstract class Car{
  //create an abstract method
  //the method doesn't have body
  abstract int showPrice();
}

//now create three more public class
//that extends Car class
public class Volvo extends Car{
  //override the showPrice() method here
  @Override
  public int showPrice(){
    return 8500;
  }
}
//Create a clasd called Maruti
public class Maruti extends Car{
  //override the showPrice() method here
  @Override
  public int showPrice(){
    return 12300;
  }
}
//create a subclass called Mustang
public class Mustang extends Car{
  //override the showPrice() method here
  @Override
  public int showPrice(){
    return 15000;
  }
}

//now create the main class file.
//it should be public and contains
//main method
public class TestClass{
  public static void main(String[] args){
    
    //Create objects of the above three 
    //child class to access the
    //showPrice() method
    
    Volvo volvo = new Volvo();
    Maruti maruti = new Maruti();
    Mustang mustang = new Mustang();
    
    System.out.println("The price of Volvo is "+ volvo.showPrice() +"$");
    System.out.println("The price of Maruti is "+ mariti.showPrice() +"$");
    System.out.println("The price of Mustang is "+ mustang.showPrice() +"$");
  }
}
/**
 * Output:
 * The price of Vovo is 8500$
 * The price of Maruri is 12300$
 * The price of Mustang is 15000$
 */

//This is an ongoing article...