Java Object-Oriented Programming Tutorial on Interfaces & ‘implements’ Keyword with Stone River eLearning

Since we have already discussed about inheritance and abstract classes in JAVA, there are no prizes for guessing that the next frontier of this series of blog posts is interface. Interface is very similar to the abstract classes with more purpose and direction to its functionality. It would not be wrong to say that whatever abstract classes do, interface does better. However; whether to use an abstract class or an interface depends solely on the choice of the programmer and the requirements of the program.

 

What is an interface?

An interface can be termed as a group of related methods without any specified functionality initialized to them. In other terms, interface is a blueprint of a class with all its methods as abstract methods.

Interfaces in JAVA are aimed at attaining full abstraction and multiple inheritance; something which cannot be achieved by abstract classes and “extends” keyword alone. Let us look at an example of interface in JAVA.

Eg:


public interface first

{

public void get();

public void show();

}

public interface second

{

public void calculate();

}

public class cube implements first,second

{

int x, y, result;

public void get()

{

x=3;

}

public void calculate()

{

result = x*x*x;

}

public void show()

{

System.out.println(“Cube of “ +x ”is :” +result);

}

public class square implements first,second

{

int x, y, result;

public void get()

{

x=5;

}

public void calculate()

{

result = x*x;

}

public void show()

{

System.out.println(“Cube of “ +x ”is :” +result);

}

public class Test

{

public static void main(String[] args)

{

cube ob1 = new cube();

square ob2 = new square();

ob1.get();

ob1.calculate();

ob1.show();

ob2.get();

ob2.calculate();

ob2.show();

}}

In the above example, we can see that the interface first and second have abstract methods in them and then these interfaces are used by the classes cube and square to perform different functionalities. The keyword for using interfaces in the code is “implements” and the syntax to be followed is, class name followed by the keyword “implements” and then the name of the interfaces in a comma separated order.

Points to Ponder:

  1. All methods under interfaces are/must be public and abstract.
  2. Interface cannot have constructor or an object.
  3. We can extend/inherit interfaces, i.e, one interface can inherit another interface.
  4. Data members of an interface are always public, final and static.
  5. It is necessary to mention access specifier while writing the methods of the interface in a child class.
  6. Once interfaces have been inherited, the methods inside those interfaces cannot be left blank inside the child class. Therefore, the interface data members must implement some functionality inside the child class.
  7. Interfaces are useful in performing multiple inheritance in JAVA since a class can implement more than one interface at a time.

 

How is an interface different from an abstract class?

It was mentioned at the starting of this blog that there are not many differences between an abstract class and an interface with the latter a better and more useful version of the former. However; understanding of the differences between an abstract class and an interface works wonders for a beginner in realizing the overall concepts of JAVA programming language.

  1. All methods inside an interface are by default abstract, however; it is not necessary that all methods inside an abstract class are abstract.
  2. The data members of an interface are all final while an abstract class may have non-final data members as well.
  3. Members of an interface are by-default public, however; members of an abstract class can be public, private or protected.
  4. The methods inside an interface cannot have an implementation in the interface however; they must have an implementation in the child class. On the other hand, the methods inside an abstract class may or may not have an implementation inside itself or the child classes, however; they need to be mentioned in every inherited class.

 

(Visited 210 times, 1 visits today)