Interfaces in Java (Basics of Java Interface)

We would cover the following:

  1. What are Interfaces in Java
  2. What are Differences between a Class and an Interface
  3. How to Declare an Interface
  4. Why Interfaces?
  5. Implementing an Interface
  6. Extending an Interface
  7. Implementing an Extended Interface
  8. Some Quiz on Java Interfaces

 

1. What are Interfaces in Java?

An interface in Java is similar to a class. It is created the same way as as a class. However, an interface is a reference type(this means that it is not a primitive type). An interface can be seen as a collection of abstract methods. An abstract method is a method that is declared with no implementation.

An interface must be inherited(implemented) for it to be used. So a class that implements an interface, would inherit all the abstract methods and must then provide definition for these methods.

 

2. Difference Between a Class and an Interface

And interface in Java is similar to a class but take note of the following differences:

  • An interface cannot be instantiated. This means that you cannot create an object of an interface type.
  • An interface cannot contain any constructors
  • All the methods of an interface are implicitly abstract(declared with no implementation)
  • Any property(field) in an interface must be declared as both static and final. No instance fields in an interface.
  • A class implements an interface (not extends it)
  • An interface can extend more than one interfaces. This is a way to achieve multiple inheritance in Java.

 

3. How to Declare and Interface in Java

The interface keyword is used to declare an interface in java. In the code below, an interface Shape is declared with two abstract methods

/*
 * Written by: Kindson The Genius
 * Date: 19th Dec. 2018
 */

public interface Shape {
	public void Draw();
	public double CalculateArea();
}

Listing 1.0: Interface shape

 

4. Why Interfaces?

Why do we need to have an interface with only definitions with no implementations? Take a look at the program of Listing 1.0. We have a class Shape which represents shape objects. How to draw a shape would depend on the type of shape object. For example, how to draw a triangle would be different from how to draw a circle. In the same way, the calculation of the area of a triangle would be different from the formula for area of a circle. Therefore it makes sense to put the implementations of this method in the particular shape class that implements shape.

 

5. Implementing an Interface

An interface is implemented by a class using the implements keyword followed by the name of the interface. Listing 1.1 shows the classes triangle and circle that inherits from the class shape.

/*
 * Written by: Kindson The Genius
 */

public class Triangle implements Shape {

	double base;
	double height;
	
	public void Draw() {
	     //implementation of the draw method
		
	}

	public double CalculateArea() {
		double area = 1/2 * base * height;
		return area;
	}	
}

Listing 1.1: Class Triangle implements Shape

We can see that the class Triangle, implements that interface Shape. This means that Triangle must provide the implementation for all the methods defined in Shape. In this case, Draw() and CalculateArea().

If a class that implements an interface fails the provide an implementation for the methods in the interface, then there would be compile time  error.

 

6. Extending Interfaces

An interface in Java can extend another interface. This is the same way a class in java can inherit another class using the extends keyword. An interface extends another interface. But remember that a class cannot extent an interface.

An interface extends another interface using the extends keyword.  This is illustrated in Listing 1.2 where the two classes Truck and Bike extends the interface Vehicle

//Filename: Vehicle.java
public interface Vehicle {
	public void setLocation(String location);
	public String getOwner();
}

Listing 1.2: Class Vehicle

(Remember that the name of the class or interface in java is also the same as the filename)

//Filename : Truck
public interface Truck extends Vehicle {
	public void setDriver(String DriverName);
	public void Status();
	public void getNumberOfWheels();
}

Listing 1.3: Truck interface extends Vehicle

 

7. Implementing Extended Interfaces

What would happen when a class is created that implements an interface that is extended from another interface? For example the Truck interface.

So if a class is created that implements Truck, then it has to provide definition for all the three abstract methods setDriver, Status() and getNumberOfWheels(). In addition, it also has to implement the methods from parent interface. In this case vehicle. This is given in Listing 1.4 where we create a new class Mack that implements Truck.

/* A class that implements an extended interface
 * By Kindson The Genius
 */
public class Mack implements Truck {

	public void setLocation(String location) { //From parent Interface
		// TODO Auto-generated method stub

	}

	public String getOwner() { 		//From parent Interface
		// TODO Auto-generated method stub
		return null;
	}

	public void setDriver(String DriverName) {//From child Interface
		// TODO Auto-generated method stub

	}

	public void Status() {			//From child Interface
		// TODO Auto-generated method stub

	}

	public void getNumberOfWheels() {	//From child Interface
		// TODO Auto-generated method stub

	}
}

Listing 1.4: A class that implements an extended interface
8. Questions and Answers on Interfaces

Try the following questions and answers on Interfaces:

Q1:   Can an Interface implement another Interface?

A1: No. An interface only extends another interface

Q2:  Can an class extend an Interface?

A2: No. A class can only implement a Interface

Q3: Can you create an object from an interface(can you instantiate an interface)?

A3: No. Interfaces cannot be instantiated. They are meant only to be implemented by another class.

Q4: Can you have other fields aside from methods in an interface?

A4: Yes, but they must be static and final

 

Other Java Topics

Admin bar avatar

kindsonthegenius

Kindson Munonye is currently completing his doctoral program in Software Engineering in Budapest University of Technology and Economics

View all posts by kindsonthegenius →

4 thoughts on “Interfaces in Java (Basics of Java Interface)

Leave a Reply

Your email address will not be published. Required fields are marked *