The Artima Developer Community
Sponsored Link

Legacy Design Forum
Designing 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: Why we have to declare public to abstract talk method

Posted by js on January 31, 2001 at 6:48 PM

... because an interface is just a special kind of abstract class with only public methods. If you wanted a package friendly version of talk(), you must define it in the abstract class Animal, not the interface. You can choose to not type 'public' in front of the methods in an interface, but that's what java enforces, and javac should puke if you type anything *but* public (or leaving security keyword out altogether). Somewhat confusing maybe when lack of public, private, or protected in a class means package level security...

> Why we couldn't compile abstract class Animal if we don't
> declare the method as public ?

> D:\Training\Animal.java:3: The method void talk() declared in class Animal cannot override the method of the same signature declared in interface Talkative. The access modifier is made more restrictive.
> abstract void talk();
> ^
> 1 error

>
> interface Talkative {

> void talk();
> }

> abstract class Animal implements 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();
> }

> Thanks.

> J.






Replies:

Sponsored Links



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