• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

A question about "interface"

 
jeff yashero
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
/***************************/
abstract interface Car {
public void Wheel();
^^^^^^
}
class BigCar implements Car {
public void Wheel() {
^^^^^^ System.out.println("The bigcar have 4 wheels");
}
}
public class Test {
public static void main(String[] args) {
BigCar bc=new BigCar();
bc.Wheel();
}
}
/***************************/
I delete the keyword "public"(class BigCar & interface Car)...
then compile it
but...it compile error why??
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24212
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All interfaces are abstract and all interface methods are public. Java lets you include these keywords in the interface definition, but they're redundant. The following is identical to yours in every respect:

Now, when you implement an interface, since the interface methods are public, your implementations must be, too. When you change BigCar's Wheel() method to be non-public, you break that rule, so you get an error.
 
nandu pc
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by jeff yashero:

I delete the keyword "public"(class BigCar & interface Car)...
then compile it
but...it compile error why??

Moreover, you cannot access non-public members of a class from another class. so you shud get a comiple error at bc.Wheel() too.
NaNdU
 
jeff yashero
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic