This page contains an archived post to the Design Forum (formerly called the Flexible Java Forum) made prior to February 25, 2002.
If you wish to participate in discussions, please visit the new
Artima Forums.
Message:
Interface is Language
Posted by Yau Phong on 02 Apr 1999, 8:17 PM
I pefer to think interface as "language". Say a class named ABC implements three interfaces, then we can think that ABC speaks three kinds of language. For any object that would like to talk to ABC's objects, the object must at least know one of the three "language". And which language(s) the object needs to know depends on what the object want to do. Interface acts like this in Jini too. (pls correct me if I am wrong) Another point be may consider is that all methods defined in an interface must be public. So if I want to define a method as private, for example, then I will need abstract class.
Both abstract class and interface are needed, right? Yau ^_^
> I understood the article as far as why polymorphism goes. I > guess I didn't understand why the interface is needed. > Couldn't we implement the example 4 as: > abstract class Talkative { > void talk(); > } > abstract class Animal extends Talkative { > abstract public void talk(); > } > class Dog extends Animal { > public void talk() { > System.out.println("Woof!"); > } > } > class Cat extends Animal { > public void talk() { > System.out.println("Meow."); > } > } > class Interrogator { > static void makeItTalk(Talkative subject) { > subject.talk(); > } > } > class CuckooClock extends Talkative { > public void talk() { > System.out.println("Cuckoo, cuckoo!"); > } > } > class Example4 { > public static void main(String[] args) { > CuckooClock cc = new CuckooClock(); > Interrogator.makeItTalk(cc); > } > } > I'm experienced in OO, but admittedly new in Java, so maybe > this is a dumb question.
Replies:
|