• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interface questions and group programming  RSS feed

 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All

I have a simple question, but I just can't seem to get it :( ...

Suppose that we are two programmers in the group... and that we have agreed on the below interface... how can I benefit from his implementation if his class is hidden from me? I can't construct an object of type interface because it isn't a class... does that mean I always need to know his class name in order to use his implementation? And also the constructors?

What if the class is not yet implemented?



Thanks for your help :D
 
Rancher
Posts: 989
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The idea is that you (being also a programmer) can continue to write your part of the program without waiting for the other guy to complete the full implementation of that interface. When you need to call that guy's code in your code you just do so using a reference to the interface type and your code will compile so you can complete your programs independently of each other. When he is done you then decide how (and where) to instantiate the completed implementation of that interface and glue your code together.
 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that you don't necessarily have to construct an object of a particular type in order to use it. If it's part of a larger API, other objects may have methods to return an object of that interface type. Something like

Or your code might acquire it through injection.
 
Marshal
Posts: 56600
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
E Armitage wrote: . . . complete the full implementation of that interface. . . .
That interface is complete. Everybody using it can produce their own implementation without needing to know what the others are doing.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I generally understand "implementation of an interface" to mean a class that implements the interface - which does not need to be complete in order to start using the interface, just like E Armitage says.
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
H Melua wrote:I can't construct an object of type interface because it isn't a class...

Why not? Even if you don't use all the tools you've been told about, there's nothing to stop you from writing your own test/dummy implementation class, even if every method of it simply throws UnsupportedOperationException.

And if you design your program/app properly, there will only be one place that you actually create a someInterface, and thus only one place to change when you have a working implementation.

HIH

Winston
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
another advantage that often gets overlooked comes down the road. Your partner may discover a new way to do something on his side. It may be faster, smaller, cleaner, and have fewer bugs. He can write a brand new class - something you have never heard of - and as long as it implements the interface, YOU DON'T HAVE TO TOUCH YOUR CODE.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:And if you design your program/app properly, there will only be one place that you actually create a someInterface, and thus only one place to change when you have a working implementation.

Or possibly none, if the object is obtained via injection, or gotten from some API.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf Dittmer wrote:Or possibly none, if the object is obtained via injection, or gotten from some API.

Absolutely.

Winston
 
H Melua
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@E Armitage

I see! The issue was confusing to me because I kept reading that interfaces "hide the implementation" (which is true ), then I thought, as a result the constructors will also not appear, since the interfaces don't include this detail...! But from your answer I understood that, there will be another step for gluing things together :D ... so thanks for that...

@Ulf Dittmer

:D I still remember you, one of the ever so helpful member, <3

Thanks for your help, that answer is a spot on! Because I was wondering, in the real world, how do people benefit from interfaces? If for example someone puts up a code on his website that people can use through the interface, without knowing the constructors! Now it makes all the sense that there should be a source for that...
At least for my simple use, I can request a method in the interface that returns an object of type someInterface, and just use that to check my syntax for the mean time...

Thanks again <3

@Campbell Ritchie
I remember you too :D ... I feel home again haha!!

His terminology makes full sense to me, because implementation in my view reflects the class that implements the interface... (I understand it may confuse others...)

@Winston Gutkowski
Thanks for your answer...
I meant I can't construct something like "someInterface s = new someInterface()".... because it doesn't have a constructor... I understand that you can still have s being of type someInterface, but the constructor has to be of the implementing class...

Yes I completely understand that I can create my own dummy class.... but the confusion is really on "does the interface hide all the details of the implementation given the fact interfaces do not list constructors"? Now I understand that it is possible using the method Ulf had mentioned...

Thanks again

@fred rosenberger

Thanks for your help... I understand that, I was also looking into how interfaces allow us to have common types to compare different objects... which I find very interesting :D ...


Thanks all... I really appreciate your help and the discussion that had taken place... please continue I'm learning new stuff <3

HannaH
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
H Melua wrote:I meant I can't construct something like "someInterface s = new someInterface()".... because it doesn't have a constructor...

Actually, you can, by creating an anonymous class, which is yet another possible way to go until you have a proper working implementation.

Just make sure that you only do it in one place, so you have the minimum of changes to make when you do.

Winston
 
H Melua
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
^^^ ooh I see, thanks very much for that :D ...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!