Can we define an abstract class with no abstract methods in Java?

JavaObject Oriented ProgrammingProgramming

Yes, we can declare an abstract class with no abstract methods in Java.

  • An abstract class means that hiding the implementation and showing the function definition to the user.
  • An abstract class having both abstract methods and non-abstract methods.
  • For an abstract class, we are not able to create an object directly. But Indirectly we can create an object using the subclass object.
  • A Java abstract class can have instance methods that implement a default behavior.
  • An abstract class can extend only one class or one abstract class at a time.
  • Declaring a class as abstract with no abstract methods means that we don't allow it to be instantiated on its own.
  • An abstract class used in Java signifies that we can't create an object of the class directly.

Example

abstract class AbstractDemo { // Abstract class
   private int i = 0;
   public void display() { // non-abstract method
      System.out.print("Welcome to Tutorials Point");
   }
}
public class InheritedClassDemo extends AbstractDemo {
   public static void main(String args[]) {
      AbstractDemo demo = new InheritedClassDemo();
      demo.display();
   }
}

In the above example, we have not defined an abstract method in AbstractDemo class. The compiler doesn't throw any compile-time error.

Output

Welcome to Tutorials Point
raja
Published on 06-Jun-2019 13:22:58
Advertisements