• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interface Concept in Java  RSS feed

 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,CodeRanch

I am having a hard time trying to understand interface concept.

what is an interface?
What the use of an interface?
what does implement or to implement means?
What does implementation means?

Thanks in advance
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66304
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please read the tutorial on interfaces. A forum is not a suitable replacement for a tutorial. Please post back with any specific questions you may have.
 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Please read the tutorial on interfaces. A forum is not a suitable replacement for a tutorial. Please post back with any specific questions you may have.


Actually I do not want to understand syntax.what I want to understand is what is an interface?Can someone give me a simple analogy for example?Also what does it mean to implement an interface?That's what I am trying to understand.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you notice that there were several more pages to the tutorial than just the first one Bear linked to? Did you read those as well? If so, what did you understand from what?
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raghav singh wrote:
Actually I do not want to understand syntax.what I want to understand is what is an interface?Can someone give me a simple analogy for example?Also what does it mean to implement an interface? That's what I am trying to understand.


It would help if you explain your confusion. To me, a concrete class that implements an interface, should have implementations for methods defined in the interface. Kinda straightforward actually.

Henry
 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
raghav singh wrote:
Actually I do not want to understand syntax.what I want to understand is what is an interface?Can someone give me a simple analogy for example?Also what does it mean to implement an interface? That's what I am trying to understand.


It would help if you explain your confusion. To me, a concrete class that implements an interface, should have implementations for methods defined in the interface. Kinda straightforward actually.

Henry


I am actually a beginner .Its been 2 weeks I started learning java.

Yeah right now I am in a state of confusion and the problem is I don't know how to express it in words.Maybe I should take a break from java.

I am going to re read the link on oracle documentation given to me and try to understand.

Anyway thank you everyone.
 
W Akram
Greenhorn
Posts: 4
Eclipse IDE Java Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

New to Java as well so forgive if technicalities are not quite correct but I'll try and give the gist of it. An interface is abstract, it does not do anything in itself. It is relying on others to do the actual implementation. It lists a set of empty methods which it expects you to implement in your concrete class ( hence people talking about an interface being a contract ).

So for an analogy;

Assume you have an interface BankAccount. In it you have a method called calcInterest in which you return the interest calcualated having being passed the balance. something like;
public double calcInterest(double bankbalance ).

Now when you write the concrete class that implements the interface, you have to write the method that takes the bankbalance and returns the interest rate. That is the contract. How you calculate the interrest rate is entirely up you. All the interface knows and cares about is that is that for method calcInterest it's given you the bankbalance and wants the interest amound back.

How is this useful? Well you could have NormalSaver account which implements the BankAccount interface. Three months down the line you decide to set up a new account, SuperSaver with interest calculated in a different manner. Well now you can implement BankAccount interface again but for SuperSaver, implement calcInterest in with your new way and pass back the calculated amount. The interface has not changed and you did not need to change the code for NormalSaver. This means you did not need to adjust the existing code, which in turn tends to mean less risk and less testing.

Hope that helps.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interface is simply a common boundary between two entities that allow communication...

The interface between you and turning on your computer is the power button... All you are required to do is push the button and you expect a specific function/action to be carried out which is for your computer to turn on... You are not required to know the inner workings of what happens after you push the button, the concerns of the inner working is left for the implementer...

This is the same concept that is transferred to code... An interface in code is simply how you are going to use (interact with) a piece of code which is specified as function/method signatures...

As with the power button... it expects for you to push it for it to carry out the action of turning on your computer...

In code, a function/method expects you to pass in arguments with specific types and in a specific order for it to carry out its duty which has to be implemented...

Going from here with the information gathered thus far... Lets say you are creating an animation system that flashes images on the screen... You can set up the interface for the system as follows:


What this does is set up a common boundary between the users and implementers of the system to allow code such as the following:


The interface states what must be done in order to carry out its duty which is to call draw and pass it an image and call erase and pass it an image

To the user all he needs to do it pass an image to draw and pass an image to erase to use the system without concerns of how it was implemented...

To the implementer all he needs to do it let draw accept and image and let erase accept an image which can be illustrated as follows:


That is basically the general idea of an interface with regards to setting up one, implementing it and using it...
 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
W Akram wrote:Hi,

New to Java as well so forgive if technicalities are not quite correct but I'll try and give the gist of it. An interface is abstract, it does not do anything in itself. It is relying on others to do the actual implementation. It lists a set of empty methods which it expects you to implement in your concrete class ( hence people talking about an interface being a contract ).

So for an analogy;

Assume you have an interface BankAccount. In it you have a method called calcInterest in which you return the interest calcualated having being passed the balance. something like;
public double calcInterest(double bankbalance ).

Now when you write the concrete class that implements the interface, you have to write the method that takes the bankbalance and returns the interest rate. That is the contract. How you calculate the interrest rate is entirely up you. All the interface knows and cares about is that is that for method calcInterest it's given you the bankbalance and wants the interest amound back.

How is this useful? Well you could have NormalSaver account which implements the BankAccount interface. Three months down the line you decide to set up a new account, SuperSaver with interest calculated in a different manner. Well now you can implement BankAccount interface again but for SuperSaver, implement calcInterest in with your new way and pass back the calculated amount. The interface has not changed and you did not need to change the code for NormalSaver. This means you did not need to adjust the existing code, which in turn tends to mean less risk and less testing.

Hope that helps.


Thanks that help me a lot.
 
raghav singh
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rico Felix wrote:An interface is simply a common boundary between two entities that allow communication...

The interface between you and turning on your computer is the power button... All you are required to do is push the button and you expect a specific function/action to be carried out which is for your computer to turn on... You are not required to know the inner workings of what happens after you push the button, the concerns of the inner working is left for the implementer...

This is the same concept that is transferred to code... An interface in code is simply how you are going to use (interact with) a piece of code which is specified as function/method signatures...

As with the power button... it expects for you to push it for it to carry out the action of turning on your computer...

In code, a function/method expects you to pass in arguments with specific types and in a specific order for it to carry out its duty which has to be implemented...

Going from here with the information gathered thus far... Lets say you are creating an animation system that flashes images on the screen... You can set up the interface for the system as follows:


What this does is set up a common boundary between the users and implementers of the system to allow code such as the following:


The interface states what must be done in order to carry out its duty which is to call draw and pass it an image and call erase and pass it an image

To the user all he needs to do it pass an image to draw and pass an image to erase to use the system without concerns of how it was implemented...

To the implementer all he needs to do it let draw accept and image and let erase accept an image which can be illustrated as follows:


That is basically the general idea of an interface with regards to setting up one, implementing it and using it...


Yeah that's it... That was what I was looking for.Greatly help me.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
raghav singh wrote:what is an interface? What the use of an interface? ...

These questions crop up a lot - for example, here - and you may find the answer useful.

At the risk of overloading you too soon, it might be worth pointing out that there are TWO distinct phases to creating a program/class:
1. Design.
2. Construction (ie, coding).

And, of the two, the first is MUCH more important than the second - even if it doesn't seem like it at the moment .
You can almost always change code; it's much more difficult to change a design.

Interfaces are design components. Classes are for coding - ie: when you know exactly what you want them to look like.

Have a look at the WhatNotHow (←click) page for more details.

HIH

Winston

PS: When replying, you don't have to include the entire post in your quote, because it tend to make threads very long. Just include enough to make it obvious what/who you're replying to. Thanks.
 
Cj Walker
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just put it interface and everything will run smoothly.
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch C J Walker
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!