• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

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!
 
Don't play dumb with me! But you can try this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic