ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am getting my feet wet in OO Programming, and have run into the idea of an Interface in both Php and Java. I get the basic idea about Inheritance:
"Inheritance" a subclass has exactly one superclass in an "is-a-relationship".
example: VW Bug is a Car.
I get that, and you use extends to implement it, like Class VwBug extends Car and you get all the state and method goodies from Car without duplicating, and then provide custom state and method goodies for the specific sub-class VwBug to make it a unique type of Car.
I'm struggling with the idea of an Interface though, I understand the idea is that unlike in Inheritance, a class can fulfill more than one "is-a-relationships".
I'm just having trouble thinking of an example of how to describe this, or why you would use this instead of Inheritance?
yeh you have the right idea with that example you posted there. an interface will allow a group of unrelated classes to be seen as related based on their behavior and not on their inheritance heirarchy.. good thing to pick up right away in java, since you are restriced from using multiple inheritance, you have to use interfaces. i would rather works with interfaces 90% of the time anyhow..
here is an example but ill warn you that this seemed clearer in my head before i put in on paper. see if you follow.
Code:
class GameObject{}
class Player
{
void punch(GameObject o)
{
if(o instanceof Smashable)
o.smash();
}
}
interface Smashable
{
void smash();
}
class Window extends GameObject implements Smashable
{
void smash()
{
System.out.println("smashed window");
}
}
class Brick extends GameObject{}
main()
{
Window w;
Brick b;
Player p;
p.punch(w); // window smashed
p.punch(b); // hand broke :)
}
(btw this may not be syntactically correct. i didnt test it i just typed it in here)
actually i just noticed in my example, you are going to need to cast the GameObject to the appropriate interface object before calling the interface functions.
Both Door and Window implement the Smashable interface which calls the method smash(), meaning that both the Door and Window Classes must include a method smash(), however the actual smash() method itself within the Door and Window classes can vary as seen in:
It would seem the purpose of the interface is simply to tell the class to execute methods found within it? As in your example, class Door extends GameObject implements Smashable if you removed the implements Smashable, the class wouldn't know to run the smash() method.
Is there something more to interfaces that I am missing?
yeh my example is lacking a bit. ill try another way for a bit
so with an interface you can also make completely different objects seem like they are the same by using an interface. they can come from completely unrelated object heirachies and still be treated as the same. a good example would be java's Printable interface.
you can take any object and have it implement Printable, and then it is possible to pass it to the print methods.. so you could take the Door class that was in the earlier example, or some other class such as a custom container you make and if you implement printable, you can treat that object as a Printable object and pass it to the printer. and the printer object will call the Print that is defined because you implemented Printable and generate what you want printed for that object.
interfaces just make polymorphic objects bend a little further. one of those things that until you see a really great use for it and you go "Ahaa!" then it is really hard to try to explain. but it would probably be helpful to lookup Printable interface and mess with it a while.
that says that if o is a Smashable object, then call o's smash method. the
((Smashable) o)
part is to cast o from a GameObject to a Smashable (because a GameObject knows nothing about smash())
Yeah cause right now interfaces seem like a pretty big waste of time. Web resources are pretty limited as well, been browsing for a few hours now. For instance the java tutorial explanation:
interface Bicycle {
void changeCadence(int newValue);
void changeGear(int newValue);
void speedUp(int increment);
void applyBrakes(int decrement);
}
class ACMEBicycle implements Bicycle {
// remainder of this class implemented as before
}
And that's literally all they say about it, except on the last section of this tutorial, it has an exercise to create interfaces, although it never specified how.
I'll be checking that out, it's part of the same series of tutorials I'm on. I'm just surprised they would introduce a concept so early in the tutorial and request an exercise without even explaining it, their page on interfaces is seriously lacking. Although so is the rest of the web, I can't seem to get a straight answer from any site about the actual advantage, or purpose of using "implements"
I keep hearing that: Interfaces provide a form of multiple inheritance. But I am unable to find any examples of any class doing this.
That sounds a lot like the microsoft doc I just finished reading for interfaces in VB:
When to Use Interfaces
Interfaces are a powerful programming tool because they allow you to separate the definition of objects from their implementation. Interfaces and class inheritance each have advantages and disadvantages, and you may end up using a combination of both in your projects.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.