A method that has no body is known as an abstract method.
Syntax-
access-modifier abstract return-type methodName(parameters);
Example-
public abstract void eat();
abstract class Shape { public abstract int area(int r); public void display() { System.out.println("I am a shape") } } class Square { public void perimeter(int s) { System.out.println("Perimeter of square = " +4*s); } public static void main(String args[]) { Square s = new Square(); s.perimeter(2); } }
Output
compile time error.
The above code will give compile time error as the derived class Square has not implemented abstract method area() of Shape class.
abstract class Animal { String name; public Animal(String name) { this.name = name; } public abstract void sound(); } class Dog extends Animal { public Dog(String name) { super(name); } public void sound() { System.out.println("Bark"); } public static void main(String args[]) { Animal a = new Dog("German Shepherd"); System.out.println("Name = "+a.name); a.sound(); } }
Output
Name = German Shepherd Bark
As you can see we can call abstrac class constructor using super() keyword.