Tutorials

Object Oriented Programming


Abstract Class


Abstract class in java provides partial abstraction.

 

An abstract class is a class that may contain both concrete* method as well as abstract* method.

*Concrete method is the one whose body is defined or implementation is provided.

*Abstract method is one whose body is not defined or implementation is not provided

.

An abstract class is declared with abstract keyword

 

Syntax for abstract class


package abstractClass;

abstract class TutorialsInHand{

      public abstract void javaTutorial();  //abstract method- body of the method not defined

      public void motive(){

            System.out.print("Provide best learning resources");

      }

}

Important points regarding abstract class

  • You are free to declare unlimited methods (both abstract and non-abstract) in your abstract class.
  • If your class contains even single abstract method, it must be declared abstract.
  • Abstract class being a class can be extended like any usual class. Multiple inheritance is restricted for abstract class as well.
  • If your class extends any abstract class then you should either provide implementation to the abstract method of that class or else declare your class abstract as well.
  • Abstract class cannot be instantiated. It means we cannot create object of abstract class.

So in case you don't want your class to be instantiated but only extended then declaring it abstract is a good idea.

Can we declare class abstract even if it has no abstract method?

Yes, we can declare class as abstract even if it does not contain any abstract method.

For instance see below class:

 

package abstractClass;

abstract class NoAbstractMethod {

}

This abstract class without any abstract method will compile fine without any issue. 
But the question is, what is good at all in creating abstract class without abstract method?
The answer is you can create an abstract class without abstract method if:
  • You don't want other classes to create instance of this class. Remember, abstract class cannot be instantiated.
  • You may want in future to declare any abstract method which should be implemented by classes extending this abstract class.
 
Now we will see two different scenarios of extending abstract class with example.
  • Extend abstract class and provide implementation to its abstract methods.
  • Extend abstract class and don't provide implementation to one or all of its abstract methods.

Case 1: Extend abstract class and provide implementation to its abstract methods.

Note: TutorialsInHand class can be found at the beginning of this page


package abstractClass;

public class LocalTuition extends TutorialsInHand {

      @Override
      public void javaTutorial() {  //implementing abstract method

            System.out.println("Java training");

      }

      public void fees(){

            System.out.println("1000 INR for each course");

      }

      public void enroll(){

            System.out.println("To enroll call +99999999999");

      }

}

Case 2: Extend abstract class and don't provide implementation to one or all of its abstract methods.

Note: TutorialsInHand class can be found at the beginning of this page

 

Since you do not provide implementation to the abstract method of abstract class that you extends so you are bound to declare your class abstract,

For example, in below code snippet LocalTuitionA extends abstract class TutorialsInHand but do not provide implementation to its abstract method. For the same reason, LocalTuitionA has to be declared as abstract.  

If you donot do so, you will get compile time error.

package abstractClass;

public abstract class LocalTuitionA extends TutorialsInHand {

      public void fees(){

            System.out.println("1000 INR for each course");

      }
    
      public void enroll(){

            System.out.println("To enroll call +99999999999");

      }

}

 

 


Please Share this page
Views : 97