Ok, I really dont understand the point of interfaces, other than maybe to enforce a structure or something of mandatory methods or variables, sort of like extending an abstract class. I dont see how implementing interfaces is really multiple inheritance. Multiple inheritance as i envision it would be like having methods and stuff written for you. like this. would to me seem to be multiple inheritance
So how would you do that with interfaces? inherit precoded methods from different classes. thanks, -Andre
Andre, Interfaces helps you to have a class with one implementation i.e. from the super class, with the added benefit that you get a predefined interfaces(set of methods)with interface only and no implementation, so that you are able to give your own definitions regarding the class in which it isimplemented.So when you go up the heirarchy you find a fair, simple path.This is not the case in multiple inheritance in c++ where you have to branch while upcasting. THANKS
Joined: Jun 20, 2001
So basicly its a design aid, cause there is no code, just abstract suggestions to be enforced on the implementing class?
I fail to see how that compares to multiple inheritance.
Well, if you have a bunch of unrelated classes, yet they all use the same interface implementation, you can use that interface for calls and such instead of each individual class. Also, if a single class implements several interfaces, instead of specifying that class each time, it can be used where ever the interfaces can be used. That's quite handy I would say. Jason [This message has been edited by jason adam (edited September 05, 2001).]
Joined: Aug 21, 2001
Jason could you please explain it with an example.
Chicken Farmer ()
Joined: May 08, 2001
Ok, I'll give it a shot, though the best examples I think are Listeners. If you know anything about Applets, you've used interfaces, and can learn from those. Let's say you have an interface called Sleeper, which has a method isSleeping(). You also have classes Person, Fish, and Owl. I would not associate these classes normally, because besides being Animals, they don't have much in common (ok, I used to be a zoology major in undergrad and that statement just sounds so wrong, but for this example we'll use it). However, an object of each one of these classes has to sleep, but each has its own implementation of isSleeping()(people are somewhat fixed in their patterns, fish sleep if the light isn't on in the tank, cats sleep whenever they darn well feel like it). So each class will implement Sleeper, and use it's own implementation of isSleeping(). Now, if we have a program that has these objects doing something, and at some point in the program we want to call a method called isAsleep( Sleeper s ). We can pass each object of the classes to this method, regardless of which class it is, and call the isSleeping() method, and each object will run its own implementation. But the nice thing is we don't have to specify Cat, or Person, or Fish. We just use Sleeper, and where ever Sleeper is, we can use those classes. I would write the code out, but I have to run, have class tonight. This should give you a gist of what I was talking about earlier. Clear as mud? For an excellent explaination of Interfaces, read Michael Matola's post at http://www.javaranch.com/ubb/Forum19/HTML/000727.html Jason