An abstract class cannot have non-abstract methods code example

Example 1: abstract class java

// abstract class
abstract class Shape
{
   // abstract method
   abstract void sides();
}
class Triangle extends Shape
{
   void sides()
   {
      System.out.println("Triangle shape has three sides.");
   }
}
class Pentagon extends Shape
{
   void sides()
   {
      System.out.println("Pentagon shape has five sides.");
   }
   public static void main(String[] args)
   {
      Triangle obj1 = new Triangle();
      obj1.sides();
      Pentagon obj2 = new Pentagon();
      obj2.sides();
   }
}

Example 2: Can we add a non-abstract method into abstract class?

Yes, we can. An abstract class can have both abstract and non-abstract methods

Example 3: can you declare an abstract method in a non abstract class

No. A normal class(non-abstract class) cannot have abstract methods.

Tags:

Misc Example