Mega Code Archive

 
Categories / Java Book / 002 Class
 

0150 What are Abstract Classes

You can require that certain methods be overridden by subclasses by specifying the abstract type modifier. To declare an abstract method, use this general form: abstract type name(parameter-list); No method body is present for abstract method. Any class that contains one or more abstract methods must also be declared abstract. abstract class MyAbstractClass{ abstract type name(parameter-list); } Here is an abstract class, followed by a class which implements its abstract method. abstract class MyAbstractClass { abstract void callme(); void callmetoo() { System.out.println("This is a concrete method."); } } class B extends MyAbstractClass { void callme() { System.out.println("B's implementation of callme."); } } public class Main { public static void main(String args[]) { B b = new B(); b.callme(); b.callmetoo(); } } The output: B's implementation of callme. This is a concrete method.