What does abstract modifier in Java do?

Java 8Object Oriented ProgrammingProgramming

An abstract keyword is used to declare methods abstract methods and abstract classes. Once a method is declared abstract we should not specify body for those.

Example

public abstract class Sample{
   public abstract demo(){
   }
}

Error

Sample.java:2: error: invalid method declaration; return type required
   public abstract demo(){
                   ^
1 error

And once a class is declared abstract it cannot be instantiated.

Example

public abstract class Sample{
   public abstract void demo();
   public static void main(String args[]){
      new Sample();
   }
}

Error

C:\Sample>javac Sample.java
Sample.java:4: error: Sample is abstract; cannot be instantiated
   new Sample();
   ^
1 error
raja
Published on 10-Jan-2018 07:16:43
Advertisements