Abstract class and Interface in Java

1- Introduction

In this document, I will instruct you in Interface and Abstract Class, also analyze the similarities and differences between them.

2- Abstract Class

Abstract class. for example:
// This is an Abstract class.
// It must be declared as abstract because it has an abstract method.
public abstract class ClassA  {

 // This is an abstract method
 // It has no body.
 // Its access modifier is public
 public abstract void doSomething();

 // Access modifier of this method is protected
 protected abstract String doNothing();

 // This method does not declare access modifier
 // It has the default access modifier.
 abstract void todo() ;
}

// This is an abstract class
// It is declared as abstract, though it does not have any abstract methods.
public abstract class ClassB   {

}
  Characteristics of an abstract class is:
  1. It is declared abstract.
  2. It can declare 0, 1 or more of  inside abstract methods.
  3. Unable to initialize one object directly from an abstract class.

3- Abstract class, the examples

See illustration:
AbstractJob.java
package org.o7planning.tutorial.abs;

// An abstract class (Simulating a job).
// There are two methods declared abstract
public abstract class AbstractJob {

    public AbstractJob() {

    }

    // This is an abstract method.
    // return Job Name.
    public abstract String getJobName();

    // This is an abstract method.
    public abstract void doJob();

}
JavaCoding.java
package org.o7planning.tutorial.abs;

// Class implements all abstract methods of the parent class.
public class JavaCoding extends AbstractJob {

    public JavaCoding() {
    }

    // Implements abstract method of parent.
    @Override
    public void doJob() {
        System.out.println("Coding Java...");
    }

    // Implements abstract method of parent.
    // Method with body.
    // Return name of Job.
    @Override
    public String getJobName() {
        return "Coding Java";
    }

}
ManualJob.java
package org.o7planning.tutorial.abs;

// AbstractJob has two abstract methods.
// This class implements only one abstract method of parent class.
// Therefore it must be declared as abstract.
public abstract class ManualJob extends AbstractJob {

    public ManualJob() {

    }

    // Implements abstract method of parent class.
    @Override
    public String getJobName() {
        return "Manual Job";
    }

}
BuildHouse.java
package org.o7planning.tutorial.abs;

// This class inherited from ManualJob
// Build House undeclared abstract,
// so it must implement all remaining abstract methods.
public class BuildHouse extends ManualJob {

    public BuildHouse() {

    }

    // Implements abstract methods of the parent class.
    @Override
    public void doJob() {
        System.out.println("Build a House");
    }

}
Demo
JobDemo.java
package org.o7planning.tutorial.abs;

public class JobDemo {

    public static void main(String[] args) {

        // Create AbstractJob
        // from Constructor of JavaCoding.
        AbstractJob job1 = new JavaCoding();

        // Call doJob() method.
        job1.doJob();

        // getJobName is the abstract method in AbstractJob.
        // But it was implemented in a certain subclass (JavaCoding).
        // So can call it
        String jobName = job1.getJobName();

        System.out.println("Job Name 1= " + jobName);

        // Create AbstractJob object
        // from constructor of BuildHouse.
        AbstractJob job2 = new BuildHouse();

        job2.doJob();

        String jobName2 = job2.getJobName();

        System.out.println("Job Name 2= " + jobName2);

    }
}
Running the example:

4- Interface

We know that a class can extends from only one parent class.
// Class B is subclass of class A, or in other words B is extended  from A.
// Java only allows a class to extend from only one another class.
public class B extends A  {
  // ....
}

// In case your class do not specify that it is extended from which class, Java will self-understood that
// it extends from the Object class.
public class B  {

}

// This way and the above are similar.
public class B extends Object  {

}
But a class can extend from multiple Interfaces.
// a class can extends from only one parent class.
// But can extends from multiple Interfaces.
public class Cat extends Animal implements CanEat, CanDrink {

  // ....
}
The characteristics of the interface
  1. Interface always has modifier: public interface, whether you declare clearly or not.
  2. If there are fields, they are: public static final, whether you declare clearly or not.
  3. Its methods are abstract method, ie without function body, and contains modifier is: public abstract, whether you declare or not.
  4. Interface have no Constructor.

4.1- Structure of an Interface

NoAccessModifierInterface.java
package org.o7planning.tutorial.itf;

// This is an interface not specified access modifier.
// Only classes in the same package can implements this interface.
interface NoAccessModifierInterface {

}
CanMove.java
package org.o7planning.tutorial.itf;
 
public interface CanMove {

 
public abstract void run();

// Method in interface is always public abstract
void back();

// Method in interface is always public abstract
public int getVelocity();

}
CanDrink.java
package org.o7planning.tutorial.itf;

 
public interface CanDrink {

 
public static final String PEPSI = "PEPSI";

// Fields in Interface is always public static final.
final String NUMBER_ONE = "NUMBER ONE";

// Fields in Interface is always public static final.
String SEVENUP = "SEVEN UP";

public void drink();

}
CanEat.java
package org.o7planning.tutorial.itf;

 
public interface CanEat {

 public void eat();
}

4.2- Class implements Interface

Animal.java
package org.o7planning.tutorial.cls;

import org.o7planning.tutorial.itf.CanMove;

// This class extends from Object.
// And implements CanMove interface.
// CanMove has 3 abstract methods.
// This class implements only one abstract method of CanMove.
// Therefore it must be declared as abstract.
// The remaining abstract methods to be implemented in the subclasses.
public abstract class Animal implements CanMove {

    // Implements run() method of CanMove.
    @Override
    public void run() {
        System.out.println("Animal run...");
    }

}
Cat.java
package org.o7planning.tutorial.cls;

import org.o7planning.tutorial.itf.CanDrink;
import org.o7planning.tutorial.itf.CanEat;

// This class extends Animal and implements CanEat, CanDrink interfaces.
// This class not declared as abstract.
// So it must implements all abstract methods of the interfaces.
public class Cat extends Animal implements CanEat, CanDrink {

    private String name;

    public Cat(String name) {
        this.name = name;
    }

    public String getName() {
        return this.name;
    }

    // Implements method of CanMove
    @Override
    public void back() {
        System.out.println(name + " cat back ...");
    }

    // Implements method of CanMove
    @Override
    public int getVelocity() {
        return 110;
    }

    // Implements method of CanEat
    @Override
    public void eat() {
        System.out.println(name + " cat eat ...");
    }

    // Implements method of CanDrink
    @Override
    public void drink() {
        System.out.println(name + " cat drink ...");
    }

}
Mouse.java
package org.o7planning.tutorial.cls;

import org.o7planning.tutorial.itf.CanDrink;
import org.o7planning.tutorial.itf.CanEat;

public class Mouse extends Animal  implements CanEat, CanDrink {

  @Override
  public void back() {
      System.out.println("Mouse back ...");
  }

  @Override
  public int getVelocity() {
      return 85;
  }

  @Override
  public void drink() {
      System.out.println("Mouse drink ...");
  }

  @Override
  public void eat() {
      System.out.println("Mouse eat ...");
  }

}
AnimalDemo.java
package org.o7planning.tutorial.cls;

import org.o7planning.tutorial.itf.CanEat;

public class AnimalDemo {

    public static void main(String[] args) {
        
        // Inherit static field of CanDrink
        System.out.println("Drink " + Cat.SEVENUP);

        // Create CanEat object
        // from constructor of Cat.
        CanEat canEat1 = new Cat("Tom");

        // An object declared as CanEat.
        // But it is a Mouse
        CanEat canEat2 = new Mouse();

        // Polymorphism shown here.
        // Java know the actual types of objects

        // ==> Tom cat eat ...
        canEat1.eat();
        // ==> Mouse eat ...
        canEat2.eat();

        boolean isCat = canEat1 instanceof Cat;

        System.out.println("catEat1 is Cat? " + isCat);

        // Cast
        if (canEat2 instanceof Mouse) {
            Mouse mouse = (Mouse) canEat2;
            
            // Call drink method (Inherited from CanDrink).
            mouse.drink();
        }

    }
}
Running the example: