We can create abstract classes and methods in java. this article explains about abstract class and abstract methods.

Abstract keyword in java

Abstract classes:

  • Abstract class may or may not include any abstract methods.
  • If a class contains a abstract method, the class must be declared as abstract class.
  • If a class is declared as abstract ,  it can not be instantiated (i.e, we can not create object of an abstract class).
  • We can extend an abstract class.
  • If we are extending an abstract class, we must give implementation for abstract methods declared in parent class. If no implementation is provided, then the child class also should be declared as abstract.
  • An abstract class may contain constructorsThis constructors can be called from child class using super keyword.
  • If an abstract class implements an interface, can implement few or no methods of that particular interface. However, a child class of this abstract class should implement all methods, which are not implemented by the parent abstract class.

Following is the general format of an abstract class:

public abstract class MyAbstractClass {
  //variables
  //concrete methods
 abstract void abstractMethodName();
}

Abstract method:

  • Abstract method is a method, which defines only method signature and provides no implementation.
  • If a abstract method throws any exception, any class which is implementing this method may / may not to throw the exception.

Following is an example of an abstract method:

//abstract method example
abstract returnType abstractMethodName() throws [exception..];

Note that abstract method signature ends with semicolon.

Check out few must read Java programming books below:

Also read: