Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does an Interface in Java work?

Tags:

java

interface

I'm self learning Java, and I'm stuck on a chapter about Interfaces. I simply cannot understand how they work in Java.

I believe I understand perfectly what Interface means and how they apply in everyday situations and technology.

But when it comes to Java, code-wise and logic-wise, I'm stuck. I don't get it. How does the concept work?

Say I have 3 objects, and 1 interface object. 2 Objects are ObjectCalculatingA, ObjectCalculatingB, ObjectMathFunctions, and ObjectInterface.

Supposedly in ObjectInterface there must be some sort of reference to the ObjectMathFunctions, so that ObjectCalculatingA and B can just access the math functions in ObjectMathFunctions without writting them all again in A and B.

Am I right?

like image 898
Grumpy ol' Bear Avatar asked Feb 17 '26 04:02

Grumpy ol' Bear


2 Answers

An interface exists to facilitate polymorphism. It allows declaring a contract that any class that implements the interface must honor. And so it is a way to achieve abstraction and model complexity by looking for commonality between things.

An example? How about shapes? All shapes have an area, right? So you could have the following classes:

  • Square
  • Circle

Then let's say you have another class that allows you to collect shapes, and return the total area:

for (Shape shape in shapes)
{
    area += shape.area()  //This is polymorphism
}

In the example above, we don't care whether the shape is a square or a circle. We can accept either. We only care that it implements the Shape interface. Each object will provide their own custom implementation of area - these internal details aren't important, only that it honors the area contract . See now how we're managing complexity? We can use the class without having to worry about all of the things that go on inside. At this point what it does is important to us, not how it does it, which lets us focus on the problem at hand not getting distracted by complex details.

This polymorphism is one of the reasons why object oriented programming was considered such a powerful evolutionary step in programming. The other key foundation concepts in Object Oriented Programming are:

  • Encapsulation
  • Inheritance

. . . you'll also need to learn these.

Abstract Base Class vs Interface

As a comment said, another way to achieve polymorphism is to use an Abstract Base Class. Which should you choose?

  • Use an interface class implementing it will have its own hierarchy and dependencies. For example a media player. A Movie Player and a Sound Player might have totally different base classes, so use an interface.

  • Use an Abstract base class when you have some commonality between things, but the specifics vary. For example a message parsing framework.

like image 128
Jasper Blues Avatar answered Feb 18 '26 16:02

Jasper Blues


In simple lay mans language. Interface is a contract and classes implementing the interface need to adhere to the contract. There can be many implementations for same interface and users can select which implementation they wish to use. For more detailed information I suggest you read book like HeadFirst JAVA.

Once you begin software development you will understand that many a times you would come across an already implemented piece of code which you feel is not properly implemented. But at the same time a colleague of yours feels its correctly implemented and serves his purpose. This is where interfaces come into play. Your colleague who feels this implementation works for him can continue using the current one whereas you can implement your new implementation but you need to make sure that it adheres to the interface so that in future if your implementation is better, your colleague will have an oion to switch over.

List<String> myList = new ArrayList<String>();

In above example arraylist is on of the implementations of the List interface. Consider this example, ArrayList is not suiting your requirments so you can do the following.

myList = new LinkedList<String>();

This is the power of 'Coding to interface'

like image 41
Pratik Shelar Avatar answered Feb 18 '26 16:02

Pratik Shelar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!