DEV Community

Cover image for Java abstract classes and interfaces
Shahed Abdulwahab
Shahed Abdulwahab

Posted on

Java abstract classes and interfaces

It is for us the living, rather, to be dedicated here to the unfinished work which they who fought here have thus far so nobly advanced.
Abraham Lincoln.

Alt Text
Abstract class: Class that has some methods without complete definition and has the modifier abstract.

  • You can not create an object using an abstract class constructor.
  • You can use an abstract class as a base class to define a derived class.
  • The abstract class has at least one abstract method.
  • Abstract method: has heading just like an ordinary method, but without body, and it requires the modifier abstract and a semicolon.
  • An abstract method can not be private.
  • An abstract class can be a type. Ex:
public abstract class Example{
  private String data1;
  private double data2;

public abstract double getPay();
}
Enter fullscreen mode Exit fullscreen mode

Interface: specifies a set of methods that any class implements that interface must have.

  • An interface is a type.
  • It contains method headings without definition, and no instance variables:
public interface Interface1{
public void method1();
public int method2();
}
Enter fullscreen mode Exit fullscreen mode
  • To implement an interface, the class must do two things:
    1. Include implements InterfaceName.
    2. The class must implements all the method headings listed in the interface.
public class Implementer implements Interface1 {

    @Override
    public void method1() {
    //definition   
    }

    @Override
    public int method2() {
    //definition   
    }

}
Enter fullscreen mode Exit fullscreen mode
  • The method headings declared to be public
  • An abstract class can implement an interface as well, this class gives definitions for some of the method headings in the interface.
  • Java interfaces can holds constants as well, ex:
public interface Constant {

    public static final int JANUARY = 1, FEBRUARY = 2, MARCH = 3;
}
Enter fullscreen mode Exit fullscreen mode
  • Any class that implements the Constant interface will automatically have these constants, ex:
public class Constants implements Constant {

    public static void main(String[] args) {
        System.out.println(JANUARY);
    }

}
Enter fullscreen mode Exit fullscreen mode
  • You can mix the uses of interfaces by including both constants and method headings in a single interface.
  • Multiple inheritance is not supported in Java, so that a class can extends only one base class. however, using interfaces, a class can implements several interfaces:
public class Implementer implements Interface1, Interface2, .. InterfaceN{

} 
Enter fullscreen mode Exit fullscreen mode
  • The reason that a Java class can extends only one base class is if Java allowed two base classes, the two classes may have the same method heading with different definition and that will cause inconsistency.
  • Rare phenomena, two interface can be inconsistent by defining two constants with the same name and different values:
public interface Interface1{
public static final int ANSWEAR = 0;
}
Enter fullscreen mode Exit fullscreen mode
public interface Interface1{
public static final int ANSWEAR = 3;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)