java abstract int method code example

Example 1: What are abstract methods in java

An abstract method is the method which does’nt have any body. 
Abstract method is declared with
keyword abstract and semicolon in place of method body.

  public abstract void <method name>();
Ex : public abstract void getDetails();
It is the responsibility of subclass to provide implementation to 
abstract method defined in abstract class

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 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:

Misc Example