• 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
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Interfaces

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Todd !!
 
Bartender
Posts: 783
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Politics n. Poly "many" + ticks "blood sucking insects". 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