example of abstract base class java

Example 1: java abstract class

// 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: write a program in which an abstract class is being defined containg an abstract method omputer(int a, int b) and a non abstract method as well

abstract class Sum{
   
   public abstract int compute(int a, int b);
   public void disp(){
	System.out.println("Method of class Sum");
   }
}
class Demo extends Sum{

   public int compute(int a, int b){
	return a+b;
   }
   public static void main(String args[]){
	Sum obj = new Demo();
	System.out.println(obj.compute(3, 7));
	obj.disp();
   }
}

Example 3: how to create an abstract class in java

abstract class scratch{
  
}

Tags:

Java Example