Java Interfaces and Abstract Classes with Examples

Interfaces and abstract classes in Java:

In this Java training series, we learned about OOPS concept in detail in our previous tutorial.

In this tutorial, we will explore about,

  • What are Java Interfaces?
  • Rules to be followed for the interface
  • Example interface program
  • What are Java abstract classes?
  • Rules to be followed for the abstract class
  • Example abstract class program

Watch the below video tutorial on:

Introduction to Interfaces and Abstract Classes in Java – Part 1:

Overview of Interfaces and Abstract Classes in Java – Part 2:

Abstraction and Inheritance in Java:

What are the Interfaces?

Interfaces are blueprint like class, but it will have only the method declaration. It won’t have any method of implementation. All the methods in the interface are public abstract by default. Java 1.8 interface can have static and default methods.

Interfaces are mainly used in API’s.

For Example: Consider you are designing vehicles engine.

When you are done with the hardware part, you want some of the software functionalities to be implemented by a client who is using your engine. So, in that case, you can define your engine functionalities in an interface.

 Interface Engine
{

void changeGear(int a);
void speedUp(int a);
} 

Rules to be followed for Interface

  • The class which is implementing the interface should implement all the methods in the interface.
  • An interface can contain final variables.
 public class Vehicle implements Engine {

int speed;
int gear;

@Override
public void speedUp(int a) {

this.speed=a;

System.out.println("speed"+speed);
}

@Override
public void changeGear(int a) {

this.gear=a;
System.out.println("gear"+gear);
}

public static void main(String[] args) {
// TODO Auto-generated method

stub

Vehicle objv=new Vehicle();

objv.changeGear(3);
objv.speedUp(70);
}
} 

Rules to be followed for interface

Here the Vehicle class is the subclass which is implementing the engine interface.

What are Abstract Classes?

An abstract class is like a class but it will have abstract methods and concrete methods. Abstract methods don’t have an implementation. It will only have the method declaration.

Rules to be followed for Abstract Class

  • The abstract class cannot be instantiated.
  • Child class which extends the abstract class should implement all the abstract methods of the parent class or the Child class should be declared as an abstract class.

When you want to design partial implementation, you can go for abstract class.

Example abstract class program:

EmployeeDetails.java

 public abstract class EmployeeDetails {
private String name;
private int emp_ID;

public void commonEmpDetaills()
{
System.out.println("Name"+name);
System.out.println("emp_ID"+emp_ID);
}
public abstract void confidentialDetails(int s,String p);

} 

EmployeeDetails

The class which is going to extend the abstract class.

HR.java

 public class HR extends EmployeeDetails {
private int salary;
private String performance;

@Override
public void confidentialDetails(int s,String p) {
this.salary=s;
this.performance=p;
System.out.println("salary=="+salary);
System.out.println("performance=="+performance);
}

public static void main(String[] args) {
HR hr =new HR();
hr.confidentialDetails(5000,"good");
}

} 

hr-java

Key points to be noted:

  • In Interfaces, all the methods will not have method implementation.
  • The class which is implementing the interface should implement all the methods in that particular interface.
  • Abstract classes can have abstract methods as well as normal concrete methods. Abstract methods don’t have an implementation.
  • The class which is extending the abstract class should have the implementation for all the abstract methods in the abstract class.
  • If the subclass doesn’t have enough information to implement the abstract methods, then the subclass should be declared as an abstract class.

Conclusion

In this tutorial, we explored all about interface and abstract classes. We also learned about the rules and syntax to use interface and abstract in the programs.

 Our upcoming tutorial will educate you more on Exceptions in Java!!

PREV Tutorial | NEXT Tutorial