Abstract Classes v.s. Interfaces in Java

Photo by James Harrison on Unsplash

A MUST-KNOW CONCEPT FOR JAVA INTERVIEW!!!

Look at both Abstract Classes and Interfaces as a set of rules that defines behavior that a class must support, like a contract when inheritance.

Whereas, abstract classes generalize behavior (from general to specific) while interfaces standardize behavior.

Abstract Classes - it cares where we take the behaviors from, the parent class.

When an abstract class is subclassed, the subclass usually provides the implementation for all of the abstract methods in its parent class — however, if it does not, then the subclass must also be declared abstract.

If a class has an abstract method, it is an abstract class.

(they do not have a body, must be “void”)

public abstract class Person {
private String name;
public Person(String name){
this.name = name
}
public abstract void walk();
public abstract void talk();
public abstract void sleep();
}
public class Student extends Person {
private int studentId;
public Student(String name, int id){
super(String name)
this.studentId = id
}
@Override
public void walk() {
// walk
}
@Override
public void talk() {
// talk
}
@Override
public void sleep() {
// sleep
}
}

Interfaces - standalone files- are implemented rather than extended

Used in the inheritance chain to enforce behavior in subclasses, methods marked as abstract in an abstract superclass must be implemented in child subclasses.

In Java, multiple inheritances are only available by implementing several interfaces. For example, a person can be a student, a parent, a child, an employee, and more.

(when you implement the interface, you have to implement everything)

public interface Left {
public abstract void goLeft();
}
public interface Right {
public abstract void goRight();
}
public interface Up {
public abstract void goUp();
}
public interface Down {
public abstract void goDown();
}
public class Position implements Left, Right, Up, Down {
//do something
}

Similarities

Abstract Classes and Interfaces

  • CAN NOT be instantiated

Differences

Abstract Classes:

  • CAN add fields and methods
  • CAN have constructors
  • CAN implement
  • extends ONLY one parent abstract class
  • AT LEAST one abstract method
  • methods/fields CAN NOT be private or final

Interfaces:

  • ONLY abstract methods
  • NO fields
  • NO constructors
  • NO implementation
  • CAN implement multiple Interfaces
  • ALL fields should be defined as static and final
  • ALL methods should be public and abstract

When To Use What?

  • Standard Class — if a parent defines methods and has some data (state)
  • Abstract — if a parent defines methods, has some data (state), but also must leave some methods undefined
  • Interface — if a parent defines methods, but contains no data (state)
Photo by Ben Kolde on Unsplash

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Hanwen Zhang

Hanwen Zhang

21 Followers

Full-Stack Software Engineer at a Healthcare Tech Company | Document My Coding Journey | Improve My Knowledge | Share Coding Concepts in a Simple Way