Java Abstract Class Tutorial with Rules and Examples

Java Abstract Class Tutorial Dining Hall Example Infographic

A Java Abstract Class is nothing but a class with one or more unimplemented methods. These incomplete methods should be implemented by the first Concrete subclass of an abstract class. Let us know more about Abstract Classes in this Last Minute Java Tutorial.

Note: A concrete class is any Java class that defines all methods for readymade use. Objects of a concrete class can be created using the keyword "new".

Java Abstract Class Rules and Examples

You can not instantiate an Abstract Class to create an object. It is not a normal fully working class with methods and variables. Some implementation is left to the subclass. So, it is up to the developer or implementor to complete the method body of an abstract method of an abstract class.

Java Abstract Class Syntax:

abstract class CLASS_NAME
{
  abstract return-type METHOD_NAME(PARAMETERS); //Ending with a Semicolon
}

To create an Abstract Class, you should use the keyword "abstract" before the keyword "class". The keyword "abstract" should precede before each Abstract method of an Abstract Class. You should end the abstract method with a semicolon (;) symbol just after the closing parentheses ")" without any method-body.

Note: An abstract class can only be subclassed or inherited using the keyword "extends". Abstract methods will be converted into concrete methods by a Subclass through inheritance by maintaining the same method signature. You can not say that subclass is overriding an abstract method as the superclass is an abstract class and it does not support the creation of objects in memory.

Check the below example once. An abstract class can contain concrete methods like seating(), ambience() and music() which can be used directly after subclassing the abstract class.

AbstractClassExample1.java

abstract class DiningHall
{
  abstract void food();
  void ambience()
  {
    System.out.println("Ambience Ready");
  }

  void seating()
  {
    System.out.println("Seating Ready");
  }

  void music()
  {
    System.out.println("Music YES");	
  }
}

class DiningHallSubclass extends DiningHall
{
  void food()
  {
    //Writing code is optional. No compiler error.
    System.out.println("Serve Pizza and Burger..");
  }
}

public class AbstractClassExample1
{
  public static void main(String[] args)
  {
    //Superclass reference
    DiningHall dh = new DiningHallSubclass();
    dh.food();
    dh.ambience();

    //Same class reference
    DiningHallSubclass dhs = new DiningHallSubclass();
    dhs.food();
    dhs.music();
  }
}
//OUTPUT
Serve Pizza and Burger..
Ambience Ready
Serve Pizza and Burger..
Music YES

In the above example, DiningHall is an abstract class with an abstract method food(). So the DiningHallSubclass has to define its own version of food() method after inheriting the DiningHall class. The constructor of the abstract class is invoked by the default constructor of the implementing subclass.

Java Abstract Class creation and usage Rules:

Find all the rules to create a compiler friendly Java Abstract Class below. Also, know how to use an Abstract Class.

  1. An abstract class need not contain any abstract methods.
  2. An abstract class with 100% concrete methods compiles and runs without errors or exceptions.
  3. An abstract class or abstract method can be created only by using the keyword "abstract".
  4. An abstract class can contain variables of type primitives and Objects.
  5. An abstract class can contain constants defined using the keyword "final".
  6. An abstract class can contain static variables and methods.
  7. Abstract methods can not be declared with the keyword static.
  8. An abstract method can throw exceptions. The subclass implementing it need not throw any exception. Calling these methods with a subclass reference will not result in compile errors.
  9. An abstract class can define a constructor. It allows overloading of constructors also.
  10. If one method is abstract, the whole class enclosing it should be marked abstract.
  11. An abstract class can not be declared "final" as it stops subclassing and implementing.
  12. An abstract class can inherit or subclass another abstract class using the "extends" keyword.

Look at another example using an Abstract class Juicer. The subclass ABCJuicer implements the abstract method makeJuice().

AbstractClassExample2.java

abstract class Juicer
{
  abstract void makeJuice();

  Juicer() //constructor
  {
    System.out.println("JUICER Constructor.");
  }

  void addSugar()
  {
    System.out.println("1 Tablespoon Sugar added.");
  }
}

class ABCJuicer extends Juicer
{
  void makeJuice()
  {
    //Coldpress juice
    //or Normal juice
    System.out.println("Coldpressed Juice is ready.");
  }
}

public class AbstractClassExample2
{
  public static void main(String[] args)
  {
    ABCJuicer juicer = new ABCJuicer();
    juicer.makeJuice();
    juicer.addSugar();
  }
}
//OUTPUT
JUICER Constructor.
Coldpressed Juice is ready.
1 Tablespoon Sugar added.

Note: Abstract classes are meant to define some code and leave only fewer methods to be implemented by the subclasses. It is the best practice.

We shall discuss a variation of an Abstract class, Interface in the coming chapters.

Share this Last Minute Java Abstract Class tutorial with your friends and colleagues to encourage authors.

 

 

ExamTray App is now Available on Google Play EXAMTRAY android app link