OOP: Abstraction in Java

  1. Abstract class.
  2. Interface.

Abstract class

  • An abstract class is a special class in Java that can have abstract methods.
  • We cannot initialize abstract classes directly.
  • The abstract class can have regular methods and properties as well.
  • If an abstract class extends another abstract class but the implementation of abstract methods is not required.
  • The first non-abstract class that extends the abstract class must implement all its abstract methods.
  • The class can extend only one abstract class by the rules of inheritance to avoid the diamond problem.
public abstract class Vehicle{
public String name;

public abstract void move();
public abstract void stop();
}
public class Car extends Vehicle {

@Override
public void move() {
System.out.println("Car is driving");
}

@Override
public void stop() {
System.out.println("Car is stopped");
}
}
public class Motorcycle extends Vehicle {

@Override
public void move() {
System.out.println("Motorcycle is driving");
}

@Override
public void stop() {
System.out.println("Motorcycle is stopped");
}
}
public class Main {
public static void main(String[] args) {
// we cannot assign abstract class directly
// Vehicle vehicle = new Vehicle();

Vehicle vehicle = new Car();
vehicle.drive();
// a lot of code here ...

vehicle.stop();
}
}
public class Main {
public static void main(String[] args) {
Vehicle vehicle = new Motorcycle();
vehicle.drive();
// a lot of code here ...

vehicle.stop();
}
}

Interface

  • The interface can have only abstract methods except for static and default methods.
  • We cannot initialize the interface directly.
  • All interface properties are public static final. Basically, they are all constants by default.
  • public static final is optional to type for properties. It will be injected by default.
  • public abstract is optional to type for methods in the interface. It will be injected by default.
  • We need to use implements keyword to implement our interface. One class can implement multiple interfaces.
  • static and default methods can have bodies in the interface.
public interface Vehicle {
public static final String str;
// private String str1; does not compile
void drive(); // public abstract will be injected by default
void stop(); // public abstract will be injected by default
// static method can have a body
public static String setStr(String strInput) {
str = strInput;
}
// default method can have a body
public default void printStr() {
System.out.println(str);
}
}
public class Car implements Vehicle {

@Override
public void move() {
System.out.println("Car is driving");
}

@Override
public void stop() {
System.out.println("Car is stopped");
}
}
public class Motorcycle implements Vehicle {

@Override
public void move() {
System.out.println("Motorcycle is driving");
}

@Override
public void stop() {
System.out.println("Motorcycle is stopped");
}
}
public class Main {
public static void main(String[] args) {
// we cannot assign interface directly
// Vehicle vehicle = new Vehicle();

Vehicle vehicle = new Car();
vehicle.drive();
// a lot of code here ...

vehicle.stop();
}
}

--

--

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