java method abstract 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: how to create an abstract method in java

abstract class Scratch{
  // cannot be static
    abstract public void hello();
    abstract String randNum();
}

interface Other{
  //all methods in an interface are abstract
    public void bye();
    String randDouble();
}

Tags:

Java Example