Win a copy of Event Streams in Action this week in the Java in General forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Interfaces

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
Could someone please explain interfaces like you explained how I could explain polymorphism to my dog or how to get in touch with my inner class. Those were great. Yea, i know an interface is a class with methods without an implementation and it's a way to allow mulitple inheritance, but WHY is it used I would love it if you could provide an SMALL EASY to read application example. Also if you could explain something like this:
InterfaceRef ir = someObj.someMethod();
Thanks so Much,
Happy Holidays at the Ranch,
Tom
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess it took me a while to figure out the pupose of interfaces. An interface is just a basic agreement of the methods and signatures any class must contain by implementing an interface. Here's an example....
public interface Animal {
public String makesNoise();
}
public class Dog implements Animal {
public String makesNoise() { return "bark"; }
}
public class Cow implements Animal {
pulbic String makesNoise() { return "moo"; }
}
public class DogCow implements Animal {
public String makesNoise() { return "mork"; }
}
so is any class uses an Animal they could just say....

Animal myAnimal = getAnimal() // return the current animal
String myNoise = myAnimal.makesNoise();
System.out.println(myNoise);

 
Tom Graziose
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Todd !!
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom,
See the discussion regarding interfaces in the following link from another forum: http://www.javaranch.com/ubb/Forum33/HTML/001190.html
-Peter
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!