Posts Tagged: abstract class

So what’s that difference again? Abstract class vs Interface

Everything was nice and pretty clear before beautiful Java 8 was born.  One could define:

(1) Abstract class as being a class declared abstract (usually, but not necessary, contains at least one abstract method).

(2) Abstract method as being one method that is declared, but for which no implementation was provided.

(3) Interface as a 100% abstract class.

Even though, not all the differences are explained in these statements, these would have been acceptable definitions.

And then Java 8 was brought into the world, and the new feature (considered by some the best new enhancement) : default and static methods for interfaces. Non-abstract methods defined directly in the interface. This implies that (3) became obsolete and the magic question arise:

What is the difference between an interface and abstract class? 

Abstract class Interface
Default methods any method that is not abstract can be considered defaultit is not marked with default modifier marked withdefault modifier
Static methods static modifier required see below examples static modifier required
Non-Constants variables can be defined all the variables are implicitly public static final
A Java class can extend just one class (abstract or concrete) implement multiple interfaces
A Java interface cannot extend/implement an abstract class can extend multiple interfaces
The first concrete class extending the abstract class must implement all abstract methods implementing the interface must implement all abstract methods
Implicit modifiers for variables No implicit modifiers public static final
Implicit modifier for methods No implicit modifiers the non-static & non-default methods are implicitly public abstract
When to use … ? when the code is shared amongst related classes when the code is shared among unrelated classes

So, let’s revisit the definition of an interface. I would like to go for this one:

Interfaces are abstract data types that provide a contract between its users and its providers.

Here you have a nice example to start discovering the differences between abstract classes and interfaces!

import java.io.*;
import static java.lang.System.out;
class Main {
 public static void main (String[] args) throws Exception { //Mother mother = new Mother(); DOES NOT COMPILE
 //AnInterface interface = new AnInterface(); DOES NOT COMPILE
 Mother child1 = new Child();
 AnInterface child2 = new Child();
 Child child3 = new Child();
 
 //calling static fields
 out.println(child1.ABSTRACT_CLASS_NAME);
 out.println(Mother.ABSTRACT_CLASS_NAME);
 out.println(child3.ABSTRACT_CLASS_NAME);
 out.println(Child.ABSTRACT_CLASS_NAME);

 out.println(child2.INTERFACE_NAME);
 out.println(AnInterface.INTERFACE_NAME);
 out.println(child3.INTERFACE_NAME);
 out.println(Child.INTERFACE_NAME);

 //caling static methods
 child1.staticMotherMethod();
 Mother.staticMotherMethod();
 child3.staticMotherMethod();
 Child.staticMotherMethod();
 //calling default methods

 //child2.staticInterfaceMethod(); DOES NOT COMPILE
 AnInterface.staticInterfaceMethod();
 //child3.staticInterfaceMethod(); DOES NOT COMPILE
 //Child.staticInterfaceMethod(); DOES NOT COMPILE
 
 //calling default methods
 child1.defaultMotherMethod();
 child1.defaultMotherMethod1();
 child3.defaultMotherMethod();
 child3.defaultMotherMethod1(); 

 child2.defaultInterfaceMethod();
 child2.defaultInterfaceMethod1();
 child3.defaultInterfaceMethod(); 
 child3.defaultInterfaceMethod1();

 }
}

abstract class Mother {

 public static final String ABSTRACT_CLASS_NAME = "Mum name";

 public static void staticMotherMethod() { //the access modifier is not implied
 out.println("static Mother Method");
 }

 public void defaultMotherMethod() { //the access modifier is not implied
 out.println("Mother default");
 }

 public void defaultMotherMethod1() {
 out.println("Mother default 1"); //the access modifier id not implied
 }

 public abstract void abstractMotherMethod() ;
}

interface AnInterface {

 String INTERFACE_NAME = "Interface name"; //implicit public static final

 default void defaultInterfaceMethod() { //implicit public
 out.println("default interface method");
 }

 default void defaultInterfaceMethod1() {
 out.println("default interface 1");
 }

 static void staticInterfaceMethod() { //implicit public
 out.println("static inteface method");
 }

 void abstractInterfaceMethod(); //implicit public abstract
}

class Child extends Mother implements AnInterface {
 
 public void abstractInterfaceMethod() {
 out.println("Child abstractInterfaceMethod");
 }

 public void abstractMotherMethod() {
 out.println("Child abstractMumMethod");
 }

 public void defaultMotherMethod1() {
 out.println("Child default method");
 }

 public void defaultInterfaceMethod() {
 out.println("Child interface method");
 }

} 

Hope you had fun! 🙂 Till the next post hope you enjoy Java!