• Post Reply Bookmark Topic Watch Topic
  • New Topic

A question about interface methed.  RSS feed

 
meng-shire tsai
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,everyone~
Those are my programs. Test, Tank and Weapon are in the same directory.

================Test.java================================================
public class Test{
public static void main(String[] argv){
Tank tank = new Tank();
tank.shoot();
}
}
===============Tank.java=================================================
public class Tank implements Weapon{
void shoot(){
System.out.println("Tank shoot");
}
}
==============Weapon.java=================================================
public interface Weapon{
void shoot();
}
=============================End===========================================
When i compile Test.java, i got some erorrs.

C:\>javac Test.java
.\Tank.java:3: shoot() in Tank cannot implement shoot() in Weapon; attempting to
assign weaker access privileges; was public
void shoot(){
^
1 error

So...this tell me. A methed in a interface.Whether you declared it

public or not,it is still public? If it was, why java must let interface
method be public?
--
This is my first post,so I got a little nervous.
Because my English is poor...
Anyway,thx for you read this .
[ December 30, 2003: Message edited by: meng-shire tsai ]
 
Kathy Sierra
Cowgirl and Author
Rancher
Posts: 1589
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy and welcome to the ranch! -- Yes, ALL interface methods are *always* public (and abstract) implicitly (in other words, whether you actually type it that way or not).
So, following the normal Java rules for implementing a method (which are the same as the rules for overriding a method), you MUST not make the implementation/overriding method MORE restrictive than the method you're implemeneting. So that means you MUST make the implementation method from an interface PUBLIC, always.
An interface itself can be either public or default, but the methods must still be public. If you make the interface have a default access level instead of public, though, then the methods aren't really going to be public, since no class outside the package will be able to implement the interface!
But in general, when you think of the word "interface" think of "public contract" or "publicly-exposed behavior". Part of the point of an interface is so that you can use the interface to expose things to the "outside world" while still keeping what you REALLY do (i.e. your own implementations) hidden from the world. All the client knows or cares about is that you have agreed that you can DO these things in the interface, in the way declared by that interface ("this is what I take as an argument, this is what I return, and these are the exceptions that I might throw...")
cheers,
Kathy
p.s. just keep asking questions! Nobody else learns if nobody ever asks questions, and the only "dumb" questions are the ones that you don't ask because you think they're "dumb".
 
meng-shire tsai
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This really help~
Thank you ,Kathy Sierra .
Thank for your advice ,too.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!