• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Interfaces (basically speaking)

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All-
Am I correct in thinking (basically speaking) that interfaces are just a bunch of abstract classes that are just used for multiple inheritance? So you could have a class that implements 10 or so interfaces as long as the class provides definitions for all members of the interfaces?
Is it this simple? Or is there something complex to it?
Thanks
Jimmy-
-----------------
yah, yah I'm a newbie
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes..
generally interfaces are used for implementing multiple inheritance. you can implement 10,20 or more interfaces in ur class
But interfaces classes are not abstract classes...
bye for now
Rajesh Purohit
INDIA
----------------------------------------------
visit my site http://www.geocities.com/rajesh_purohit
 
Ranch Hand
Posts: 388
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
interfaces are here to assure methods are present in the implementing class (eg. ActionListener interface needs public void actionPerformed(ActionEvent ae) ).
you cannot inherit implementation through an interface. so it's up to you to code the methods from the interface.
karl
 
"The Hood"
Posts: 8521
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is that simple. But of course that does not give you the true feeling for the POWER of interfaces.
Lets say that I want to create a class or a method that takes a parameter. Only I don't know WHAT exactly the thing that is going to be passed is going to BE. You can't put a variable in the method definition
public void myMethod(IDontKnowWhat x){ //do stuff};
However you do know that in the do stuff area that you need the thing that is passed to be able to have specific behaviors. For instance, let's say that whatever is passed has to be able to execute a play() method.
Well if you have an interface that includes a play() method (let's call it BabyStuff) then you know that you will be able to handle any class that implements BabyStuff. Now you can code your method using the interface name as the "variable parameter" and be safe.
public void myMethod(BabyStuff x){ //do stuff that includes play()};
The same theory holds true for Fields (variables). You can create a variable that can hold a variety of objects by
BabyStuff b = (an instance of any class that implements BabyStuff);
Now you can use the "b" variable freely, even though you do not know what EXACT class that the object it refers to is. Of course while it is referenced through the "b" variable you can only expect it to do BabyStuff things even though as an instance of some other class it can probably do much more, but it is limited by the type of the variable that is holding it. On the other hand you have gained the ability to be able to hold a variety of different class objects this way, and be able to manipulate them in a consistant manner.
It is for these reasons that there is a basic principle of Object Oriented design that says:


Program to an interface, not an implementation


("Design Patterns" - Gamma, Helm, Johnson & Vlissides)

[This message has been edited by Cindy Glass (edited June 21, 2001).]
 
Jimmy Bonds
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Cindy for the great explanation
Rajesh and Karl Thanks to you guys too!
Jimmy-
--------------
yah, yah I'm a newbie
 
Heroic work plunger man. Please allow me to introduce you to this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic