• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

public modifier

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all ranchers,

The code below is saved using the file named "ATest.java".

public interface AnInterface {
public void methodOne() throws Exception;
}

class AnInterfaceImpl implements AnInterface {
public void methodOne(){
System.out.println("I will never throw an exception");
}
}

public class ATest {
public static void main(String args[]) {
AnInterfaceImpl ai = new AnInterfaceImpl();
ai.methodOne();
}
}

If I try to compile this code, I got this error:
"class AnInterface is public, should be declared in file named AnInterface.java"

Why does JVM complains about the public access modifier? And besides, AnInterface was declared as an interface and not a class. The code also contains ATest.java declared as public. So why is this complaining?
 
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interface is a special class.

Bottom line: interface IS A class. At least the compiler thinks so.
 
Richard Rex
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jon,

You mean we are allowed only to have one public access modifier for all top-level classes, interfaces & abstract classes in a single file?

Correct me if I'm wrong.

Thanks!
 
Jon Lee
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is correct, chad.
 
Richard Rex
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi jon,

Thanks a lot!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic