• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

When should interfaces be used?

 
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So I'm a bit confused on the whole point on Interfaces the creation of a new class is supposed to be *Class* *name* = new *Interface();* right? From an example in class it seemed like all the interface did was list the method calls and such and in the normal class instead of "public void *name*" you could just do *name*? So what purpose does this serve, is there special instances we should use Interfaces?

Thanks,


~JO
 
Saloon Keeper
Posts: 15729
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The point of interfaces is that you may want to declare useful behavior, but don't have the means to implement that behavior yet, or that the specific implementation doesn't matter.

For instance, take a look at the collections framework. Collections.sort() takes a List, and sorts it right? However, there's various ways you can implement a List of elements. For instance, you can have a list that stores its elements in a contiguous block of random access memory (ArrayList) or you could link between the elements with pointers (LinkedList). To the sort() method it doesn't matter what kind of list you have, as long as it's clear that it can perform particular actions on it. These actions are detailed in the contract specified by the List interface.

An interface provides you with the benefit of specifying what you need, without the constraint of actually having to provide it.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jay Orsaw wrote:So I'm a bit confused on the whole point on Interfaces the creation of a new class is supposed to be *Class* *name* = new *Interface();* right?



Other way around.


List is an interface. ArrayList is a concrete class that implements List. Every ArrayList IS-A List, but the reverse is not true.

The left side says, "I need this 'list' variable to be of type List." Whether we use an interface, abstract class, concrete class that's a leaf of the hierarchy, or concrete class that has subclasses on the LHS, we're saying "I need something that IS-A 'this type', but it can also be any subtype."

From an example in class it seemed like all the interface did was list the method calls and such and in the normal class



An interface is a way to define a pure abstract type. We're saying what one of "these things" must do, without saying anything about how its' actually done. For example, the java.util.List interface says that "If you call yourself a List, you must provide methods add(), iterator(), etc., and somebody who uses a List is assured that those methods will be there."

If I declare that my method returns a List, all you, the caller know and care about is that it meets the requirements of a List. You don't know or care if it's an ArrayList or a LinkedList, and I, the implementor, can use whichever one I deem appropriate.

If I declare that my method accepts a List parameter, call I care about is that it meets the contract set out by the List interface. I don't care if you give me an ArrayList or a LinkedList. I just need something I can call add(), iterate(), etc., and that maintains insertion order.

instead of "public void *name*" you could just do *name*?



No, you still have to specify a return type, but every method declared in an interface is always public and abstract, so Java allows us to leave those two keywords off.

So what purpose does this serve, is there special instances we should use Interfaces?



See above comments.
 
Stephan van Hulst
Saloon Keeper
Posts: 15729
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jeff Verdegan wrote:



Tsk tsk ;)
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Jeff Verdegan wrote:



Tsk tsk ;)



What, the lack of generics? I thought about it, but decided to leave them out for simplicity's sake.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks I was just able to find the code we did, and I think I understand it now. Basically the interface is like a set of things to do "add() remove()" etc that depending on the class could do different things, but all use the same interface? The add for one class could be different from the add for another class, but if they implement the same interface it will have the same parameters and such that it works with?






and yeah I see that it's not the other way around... Thanks for the help!
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jay Orsaw wrote:Basically the interface is like a set of things to do "add() remove()" etc that depending on the class could do different things, but all use the same interface? The add for one class could be different from the add for another class, but if they implement the same interface it will have the same parameters and such that it works with?



All the implementations of add() should do the same thing at a high level--add an element to the list. It's how they do it that's different for the various implementations. An ArrayList just assigns it to an array slot and increments a pointer, while a LinkedList adjusts previous and next links.

For many operations, we don't care about those details. As somebody else mentioned, for example, for sorting a List, all that matters is that we are able to to perform the various operations, not how they're implemented. Or for simply processing every item in a List, all we care about is that we can iterate over it.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So after reading this https://coderanch.com/t/387137/java/java/Extends-vs-Implement to get a better understanding I think I've got it. Interfaces seem to just be a skeleton for your classes to use, in which all of the methods of an interface must be used. When you extend a class you can use some methods from the super class, or override methods of the super class to do what you want. It seems that both could possibly do the same thing though, it just depends on what exactly you need? All or some? or having a skeleton vs having something that's already coded, just need some tweeks?


Also I have a class called "Face" and I was trying out to Extends the Rectangle Class. I was able in my "Draw" Class to say so you can also throw a superclass in front? The only issue I see is that it only uses the methods in the Rectangle2D class which is an issue and it seems like no point to really do it this way....... I also realized that since Rectangle is a Subclass of Rectangle2D that since I am using the a subclass of a subclass I can call any Superclasses that proceed i.e., Rectangle -> Rectangle2D -> RectangularShape ? Also what exactly does the = "class();" do? From my extension of the Rectangle class the Face class has 0 usage from what I see.

One last thing, what exactly would the difference be in saying compared to if DList has the same methods what is different?

As Stephan said it's declaring useful behavior before actually needing it, so it seems like a documenting technique for what exactly you want to do, before actually doing it? Is there anything else to it?

Thanks for the help, much appreciated,

~JO

Edit: Going to make a new topic for my other questions.

 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jay Orsaw wrote:Thanks I was just able to find the code we did...


I shortened a few very long lines in your code (67, 81 and 260(split in two)) because it screws up the windowing here.

BTW, that's an awful lot of code to go through; in future, you might want to just post the relevant parts. Also, the indentation isn't too great.

Winston
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Winston Gutkowski wrote:

Jay Orsaw wrote:Thanks I was just able to find the code we did...


I shortened a few very long lines in your code (67, 81 and 260(split in two)) because it screws up the windowing here.

BTW, that's an awful lot of code to go through; in future, you might want to just post the relevant parts. Also, the indentation isn't too great.

Winston



Was just an example we did in class, no need to edit it, was just posting as a reference. I understand what's going on now. I'll mark this as resolved besides the last question I had.

EDIT: i cannot even remove it, for some reason I cannot edit my own work? Is there a time limit, or since you edited it did it mess up my editing capabilities?
 
Winston Gutkowski
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jay Orsaw wrote:EDIT: i cannot even remove it, for some reason I cannot edit my own work? Is there a time limit, or since you edited it did it mess up my editing capabilities?


Shouldn't do as far I know, but I'm fairly new to this Bartending lark. If you're going to mark the Thread as resolved, I wouldn't worry about it; just remember next time that very long lines in code sections do cause display problems.

Winston
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
without reading all the previous answers, you seem confused
*Class* *name* = new *Interface();* right?
no it is more like class Name implements Interface

The point of interfaces is that you may want to declare useful behavior, but don't have the means to implement that behavior yet, or that the specific implementation doesn't matter.


that is probably the gist of it. if you are going to have several classes that behave in a very similar way you write an Interface
here is an example

Problem choice = map.get(problems.getSelectedItem());

i can have hundreds of classes that implement Problem but i only need that one line of code that treats them all the same
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jay Orsaw wrote:So after reading this https://coderanch.com/t/387137/java/java/Extends-vs-Implement to get a better understanding I think I've got it. Interfaces seem to just be a skeleton for your classes to use, in which all of the methods of an interface must be used.



Not really. They're a definition of what a given type does. Nobody has to use any of the methods (though if you're not planning to use the methods, why obtain the object in the first place?) but any implementing non-abstract class has to implement all the interface's methods.

When you extend a class you can use some methods from the super class, or override methods of the super class to do what you want.



Again, using the methods has noting to do with it. Extending a class is no different from implementing an interface. They're both IS-A relationships; in both cases you inherit the parent types non-private members; and, in both cases, you have to either provide implementations for all the abstract methods. It so happens that all of an interface's methods are always public and abstract, but you could do the same thing with an abstract class.

Also I have a class called "Face" and I was trying out to Extends the Rectangle Class. I was able in my "Draw" Class to say so you can also throw a superclass in front?



In my first reply, with regard to List list = new ArrayList(); I said the following:

me wrote:The left side says, "I need this 'list' variable to be of type List." Whether we use an interface, abstract class, concrete class that's a leaf of the hierarchy, or concrete class that has subclasses on the LHS, we're saying "I need something that IS-A 'this type', but it can also be any subtype."



This is exactly the same thing.

The only issue I see is that it only uses the methods in the Rectangle2D class which is an issue and it seems like no point to really do it this way



You're declaring it a Rectagle2D because all you'll care about when you call its methods is that it's some kind of Rectangle2D. You don't care if it's a plain old Rectangle2D that implements its methods in this one particular way, or a Face that implements them some other way. And if the actual object does override some methods, it will be those methods that are called, not the parent class's versions.

If Face adds methods that aren't present in the parent class, and you need to use those methods, then you shouldn't be declaring it as Rectangle2D, because you're already saying "I need a Face, not just a Rectangle2D".

One last thing, what exactly would the difference be in saying compared to if DList has the same methods what is different?



I explained that in my first reply and repeated it above. When you declare TypeName variableName you're saying, "I will use this variable as this particular type. It can't be some supertype (just plain old Object won't do, nor will Collection), but in terms of what features I need from it, I don't need it to be anhy more specific than just Type (for instance, in my use of this variable, I wouldn't care if it's an ArrayList or a LinkedList)."

 
Sheriff
Posts: 22815
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jay Orsaw wrote:EDIT: i cannot even remove it, for some reason I cannot edit my own work? Is there a time limit, or since you edited it did it mess up my editing capabilities?


There's a time limit indeed, but I can't remember what limit that was. Winston's editing had nothing to do with it.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Long bump, but I was curious if by using an interface that I can sort classes that implement that interface? I have a few types of objects I want to sort; however I'm not sure if they can all be sorted in multiple classes. We wanted to create 1 big class that had all of the information for those 4 object types, but I figured if I could make seperate classes that all were implemented by 1 thing and then I could use the same sorting for all of them from the interface, that would be better than using 1 giant class full of methods that one might use and the others might not....


I.E., can I create a linked list, array, and arraylist of integers and throw them all into another array(or something like that) and sort them all since they all implement the same sort of the List Interface? Or could I only sort each list by themselves?
 
Marshal
Posts: 79969
396
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jay Orsaw wrote:. . . by using an interface that I can sort classes that implement that interface? . . .

The short answer is yes.

The long answer is y-e-e-e-e-e-s-s-s-s, or if you prefer to avoid my silly jokes, look at this section in the Java Tutorials, which I think will answer your question.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Jay Orsaw wrote:. . . by using an interface that I can sort classes that implement that interface? . . .

The short answer is yes.

The long answer is y-e-e-e-e-e-s-s-s-s, or if you prefer to avoid my silly jokes, look at this section in the Java Tutorials, which I think will answer your question.



Thanks .
 
Campbell Ritchie
Marshal
Posts: 79969
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You’re welcome and I hope that link answered your question.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
youre wrong when you code:
It must be
and also, you use interface if you think your module would want polymorphism in the future.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Jay Orsaw wrote:. . . by using an interface that I can sort classes that implement that interface? . . .

The short answer is yes.

The long answer is y-e-e-e-e-e-s-s-s-s, or if you prefer to avoid my silly jokes, look at this section in the Java Tutorials, which I think will answer your question.



So I haven't done anything really with the sorting of classes and making the interface... I was curious if I should use an interface for what I want to do.... I am going to have about 4 classes in which each class would be similar, but not the same... I figured I'd make an interface with all of the methods needed(some shared like x,y,w,h but some different)... I figured I'd do that, or just make 4 seperate classes, but from what I got from this thread if some things are similar I should make it all implement the same interface(I also look at the Array, Arraylist, and Linkedlist, though their implementing the same stuff, just differently; whereas I have some similar features that will most likely act the same, and some additional that only some or 1 class will use)...
 
Campbell Ritchie
Marshal
Posts: 79969
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is difficult to read, but I think it looks correct.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That is difficult to read, but I think it looks correct.



Ok basically I have about 4 classes that have a few variables/methods that might be the same(x,y,w,h), and some that are different....

Should I put all of the methods into the an interface, or just make 4 separate classes?
 
Campbell Ritchie
Marshal
Posts: 79969
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don’t know.
 
Jay Orsaw
Ranch Hand
Posts: 356
Android Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Don’t know.



What should I DOOOOOOOOOOOOOOOOOO!!!
 
Sheriff
Posts: 28328
96
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First of all don't call your variables things like x, y, w, and h. Give them proper names so that when you ask people skill-testing questions about your variables, they at least have some clue what you're talking about.

Then ask a specific question. "Some classes which might have some methods the same and some different" could describe pretty much every application ever written.

So here's what you should do. Come up with a proposed design. And by "design" I mean a description of the data that the application will work with and how it is grouped together. Do not write any code, just describe things. Then come back and ask the question.
 
I wish to win the lottery. I wish for a lovely piece of pie. And I wish for a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic