The Artima Developer Community
Sponsored Link

Re: help with interfaces

Advertisement

Advertisement

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:

Re: help with interfaces

Posted by Lois Goldthwaite on 04 Feb 1999, 3:53 PM

Making Talkative an abstract class instead of an interface\
is OK if that's the only base class you need, but if
the classes are part of some other hierarchy, you can't
inherit from multiple base classes in Java. But you can
implement as many interfaces as you want.

Lois

> 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:

Sponsored Links



Google
  Web Artima.com   
Copyright © 1996-2009 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use - Advertise with Us