Abstract class & Interface in Java

Iroshan Aberathne
2 min readNov 22, 2019

In this article we are going to summaries some identifiable key points on Abstract classes and Interfaces in java.

Abstract Class

  1. Abstract class is a class that contains zero or more (often one or more) abstract methods.
  2. An abstract class cannot instantiated but abstract classes can have constructors.
  3. Another class (Concrete class) has to provide implementation of the abstract class in order to be used for instantiation.
  4. Concrete class uses extends keyword.
abstract class Demo{
public abstract void demoMethod();
public void normalMethod(){
System.out.println("This is a normal method");
}
}

public class DemoApp extends Demo{
@Override
public void demoMethod() {
System.out.println("Abstract method is implemented");
}

public static void main(String[] arg) {
DemoApp demoApp = new DemoApp();
demoApp.demoMethod();
demoApp.normalMethod();
}
}
/****************************** OUT PUT ***********************/Abstract method is implemented
This is a normal method
Process finished with exit code 0

Interface

  1. All methods of an interface are abstract methods.
  2. It defines the signature of a set of methods, without the method body.
  3. A concrete class must implement the interface(all the abstract methods of the interface)
interface Car{
void changeGear();
void applyBreak();
}

public class DemoApp implements Car{

@Override
public void changeGear() {
System.out.println("Change gear !!!");
}

@Override
public void applyBreak() {
System.out.println("Apply breaks !!!");
}

public static void main(String[] arg) {
DemoApp demoApp = new DemoApp();
demoApp.changeGear();
demoApp.applyBreak();
}
}
interface Car{
void changeGear();
void applyBreak();
}
public class DemoApp implements Car{@Override
public void changeGear() {
System.out.println("Change gear !!!");
}
@Override
public void applyBreak() {
System.out.println("Apply breaks !!!");
}
public static void main(String[] arg) {
DemoApp demoApp = new DemoApp();
demoApp.changeGear();
demoApp.applyBreak();
}
}
/****************************** OUT PUT ***********************/
Change gear !!!
Apply breaks !!!
Process finished with exit code 0

Abstract Class vs Interface

The purpose of this article is to give some insight to the Abstract classes & Interfaces because those are key components in object oriented programming.

Happy to see your thought !!!!!

--

--