Default access modifier in Interface

public interface Inter
     protected void print();
}Doesn't compile. Error Message is Modifier protected not allowed here.
public interface Inter
void print();
above code compiles fine. I was thinking since default access is more resctricted than protected it should not have been compiled either.
am i wrong??

Try implementing an interface with an access other than public in a different package.Your wish is my desire! Voila, p1.a is not a public interface and p2.c still implements it circumventing the impossiblity to directly use the "implements" keyword.
package p1;
interface a {
void doit();
package p1;
public class b implements a {
        public void doit() {
package p2;
public class c extends p1.b {
package p1;
public class d {
        public static void main(String a[]) {
                Object o = new p2.c();
                if (o instanceof a) {
                        System.out.println("A miracle! "
                                + o.getClass().getName()
                                + " implements "
                                + a.class.getName()
}$ javac -d . *.java
$ java p1.d
A miracle! p2.c implements p1.a

Similar Messages

  • Usage of default access modifier

    Some programmers don't use the default access modifier (package level). Is there a specific reason? If so, what is it? Or is it a good to use default access modifier?
    Thank you,
    Srikanth

    Some prefer to grant or limit access to an object by the interface they expose. In this case all methods are either public or private. Access is restricted based on the fact that only the appropriate code is given references of certain interface types and if code does not have the right interface, they can not access the method.
    Its a different style, but I use it myself quite a bit. This way I never am concerned over if it should be public,private, protected or default. The choice is simpler. But sometimes it can make you create interfaces for really simple things which I am not uptight enough to do...

  • Default access modifier

    Hello,
    Can someone tell me what the default access modifier is in Java:
    -for methods
    -for member variables
    Thanks in advance,
    Balteo

    Friendly. Accessible to any other classes in the package.

  • Regarding Default access modifier

    Hi All,
    There is a rule saying that...there must be one public class per source file and the file name must match with the class name.
    What about having a class with default access level?I have a file having only one class (same as the file name) with default access level.It is working fine...
    My question is...With default access, a class can be accessed inside the same package, but not from outside.Then..How come JVM is able to run that class?
    What am I thinking is correct? or Am I going wrong?
    Can anybody make it clear?
    Thanks

    What about the rule saying..
    There must be one public class per source file and
    the file name must match with the class name?I think you have misinterpreted. You can have at most one public class per file. If one class is public then all others cannot be public.

  • Doubt on 'default' access modifier

    How JRE access the main method even if the class has 'default' access.
    ie, following run without declaring the class as public.
    As per my understanding only classes in the same package access the method. Is the 'default' package belong to JRE ?
    class a {
    public static void main(String args[]){
    System.out.print("Welcome");
    }

    I am getting following error when I tried to run from command prompt.
    Exception in thread "main" java.lang.UnsupportedClassVersionError: HelloWorldApp
    (Unsupported major.minor version 50.0)
    at java.lang.ClassLoader.defineClass0(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)

  • Default class access modifier

    What is the default access modifier for a class? I can't seem to find it in the tutorials...
    Thanks
    Jim

    The default access is the same for top level classes that do not specify access explicitly as for other identifiers. Package.
    This single source file will create two class files in the same package. Only other classes in the package can see these top level package access classes.
    And, yes, this is a bad idea. In the case of some build tools, errors in a compile can cause the public class to compile, but the package class to fail. After that, the compiler will not be able to determine what source file to compile for the package level class. This is an error that confuses many developers. I do not recommend this practice. Put each top level class in its own source file.
    � {�                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Default/package/none access modifier

    Hi,
    I was hoping for some discussion on the default/package/none access modifier. It's always really bugged me that we have public, private, protected, and then "none", while it seems to me that it would be less confusing, and more consistent to use the keyword "package", or maybe even "default".
    Then, source code would look like
    public class MyClass{
        public int getValue() {}
        private void setValue() {}
        protected void someMethod() {}
        package int justForPackage() {}
    }I know this concept has come up before, but the books I've read which mention this topic haven't offered any actaul justification/explanation for why there isn't some keyword.
    Anyway, are there any insights as to why java is this way, and any reasons why java should or shouldn't be changed to include the package access modifier.

    A good example is within a tightly grouped package (usually should be this way) you may have some cooperative classes that access methods.
    // one .java file
    public class SomeHelper {
       private void method() {
         new ClassForUsers().accessHiddenLogic();
    // next .java file in same package
    public ClassForUsers {
        /* default-access */ void accessHiddenLogic() {
    }But, you may want to allow users to subclass your ClassForUsers, without giving them access to the hidden logic method directly:
    // another .java file in a different package
    public class UsersSubclass extends ClassForUsers {
        public void userMethod() {
            // can't do this
            accessHiddenLogic();
    }This could be for either business logic or security reasons. So, package level access can be very useful. However, I've seen that in practice it is avoided because it isn't obvious what is going on.

  • Non access modifier: default

    Top of the morning.
    Im studying for my SCJP exam and I have 2 text books.
    One of the text books asks the following question:
    (Im only writing the one answer im sceptical about)
    which of the following declarations are illegal?
    A. default String s;The answer at the back says that the above is illegal because defaultis not a keyword. But am I right in saying that unless you explicitly declare a class member
    as having one of the 4 access levels, it is automatically give default access - so in essence
    writing default is redundant if you don't give the member private, public or protected access.
    Nevertheless you could write default String s; couldn't you?
    The above declaration does compile.
    Thanks

    shamrock08 wrote:
    The answer at the back says that the above is illegal because defaultis not a keyword. [It is|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html], it is just not one [related access|http://java.sun.com/docs/books/tutorial/java/nutsandbolts/switch.html].
    The above declaration does compile.It does not in Eclipse. Lets see your compiling code.
    Nevertheless you could write default String s; couldn't you?
    No.

  • Access modifier for Constructors ???

    As constructors are not the so called Members of a class
    (Class's Member declarations include only 4 things : variables , methods, member classes and member interfaces)
    Why do we have access modifiers for constructors also ??
    I know that if a class A 's constructor is declared 'private' , then that class cannot be instantiated outside the class A. .That is, class A can be instantiated only inside class A and provide this newly created reference to the outside world through public getter method.
    I dont understand how the other access modifiers (protected and default ) apply to a constructor.
    Any help from ur side is greatly appreciated !!!!!

    Why do we have access modifiers for constructors also
    ??To prevent anyone from accessing them if they shouldn't.
    I know that if a class A 's constructor is declared
    'private' , then that class cannot be instantiated
    outside the class A. Or you use another c'tor. Or a static getInstance() method provided by A. You you simply shouldn't create an instance yourself anyway.
    That is, class A can be
    instantiated only inside class A and provide this
    newly created reference to the outside world through
    public getter method.Yepp.
    I dont understand how the other access modifiers
    (protected and default ) apply to a constructor. Same as at other places. No difference.

  • Access specifiers for interface methods

    When we implement the interface ,we have to specify the implementing method access specifier to be "PUBLIC" ,but not "PROTECTED" or "PRIVATE".
    Compiler is giving an error -- attempting to assign weaker access privileges ,when we specify protected.
    what is the internal reason for this?Why we shouldnt make the access weaker in the implementing class.
    Can any one help me on this.Your help is highly appreciated.

    When we implement the interface ,we have to specify
    the implementing method access specifier to be
    "PUBLIC" ,but not "PROTECTED" or "PRIVATE".
    Compiler is giving an error -- attempting to assign
    weaker access privileges ,when we specify protected.
    what is the internal reason for this?There is absolutely no point in having a private interface method. The interface represents a visible abstraction and private methods are never visible so it is a contradiction in terms.
    An interface is intended to represent an abstraction that a user (software) uses. Protected via child/parent represents a usage that is restricted to a child from a parent. The child can already see the parent so there is no point in having an abstraction for the child. And it would probably be unwise to limit a child by such an abstraction.
    Protected via the package and interfaces is more contentious as to why it is not allowed. There are those that argue that this should be allowed so that a package can use interfaces but restrict them to the package. To me this seems like a minor point given that most interfaces will probably represent an abstraction to other packages and not within a single package. This applies specifically to default access as well.

  • Private, protected Access Modifiers with a class

    Why cant we use private and protected access modifiers with a class?
    Thanks.

    Matiz wrote:
    >
    Public access allows you to extend a parent class in some other package. If you only want users to extend your class rather than instantiate it directly, make the class abstract and design for extension.Agreed. However, would the same argument be not true for the default access at the class level? No. Default access would only allow you to extend a parent class in the same package (as opposed to some other package).
    Now my confusion is why is a class allowed default access at the top level and not protected?Because protected for a top-level class makes no sense. The protected keyword provides member access to any other class in the same package and extending classes outside the package. A top-level class isn't a member of a class, by definition, so there's nothing that protected would do provide differently than public.
    So, the two access modifiers for a top-level class are public and default. Public allows access to the class outside the package, whereas default restricts access to the class within the package.
    ~

  • Access modifiers ignored !?!

    Hi
    please, have a look at the code below and let me know your opinions
    // ------------- source file 1 ------------------------
    package pckg1;
    public class Dog {
         final public static void main (String[] args) {
              pckg2.Puppy puppy = new pckg2.Puppy("Spot");
              System.out.println(puppy.getName());
    // ------------- source file 2 --------------------
    package pckg2;
    class Puppy {
         String name;
         public Puppy(String name) {
              this.name = name;
         String getName() {return name;}
    Each class belongs to a different package. Obviously if you compile Puppy first and attempt compiling Dog the compiler will complain about several things (class Puppy not being public, method getName() not being public, etc...).
    Now,
    if you change the Puppy class so that the necessary bits are public, re-compile Puppy and then Dog it will work.
    When you subsequently go back to Puppy and remove the added public access modifiers and re-compile JUST the Puppy class and try running Dog IT WILL STILL WORK AS IF EVERYTHING IN THE PUPPY CLASS WERE PUBLIC.
    I don't thing this is right. If the access modifiers cannot be relied on then the whole system is flawed.
    PS: Running on W2K, the behaviour observed on JRE 1.2.2_007, 1.3.0_02 as well as on 1.4.0-beta-b65.
    Cheers
    Ales Krestan

    Thanks for the response, but I hold different views on the matter. Although performance surely is of a great importance, it should not come at the expense of consistency.
    A few more thoughts on the same:
    1/ my understanding is that it is responsibility of the class loader to decide what to do in such cases during the resolution case. Apparently the application class loader of SUN's JVM ignores it and so does the extension class loader of the same JVM.
    2/ surprisingly (or rather expectedly) when attempting to load both classes (with the dodged version of Puppy) using the system class loader the JVM throws java.lang.IllegalAccessError.
    3) the same trick cannot be done with an interface. If you try to dodge an interface using the same principle and try to load it using the application class loader JVM will refuse to run (or load) the class that uses the dodged interface.
    4/ the class loader DOES other things, so why not to check the access modifiers as well. It should check if the class can be instantiated, for example. See the section 2.17.3 Linking: Verification, Preparation, and Resolution of the JVM specification.

  • Access modifiers

    I dint understood the below question properly, please help me out.......
    You want subclasses in any package to have access to members of a superclass. Which is
    the most restrictive access that accomplishes this objective?
    A. public
    B. private
    C. protected
    D. transient
    E. default access
    I want to know whether the question is relating to class access modifiers or methods and variables of the classes...

    I didnt get you, please explain the topic properly.... I think classes can have only public and default as an access modifier, is it rite??? That's right. Look at [this,|http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html] it will give you a better understanding of the topic than you'd get from me answering your question.

  • Access Modifiers, Package declaration

    I created the following two classes in the same folder as two source files:
    A.java
    package abc;
    class A{
    B.java
    package abc;
    class B extends A{
    When I tried to compile, A compiled successfully. But when compiling B, it gives an error Cannot find symbol for class A.... I think it should be visible as both class access modifiers are (default). Why do I get this error????
    By the way if package declarations are removed from both classes then they compile successfully........

    i tried a lot..... does classpath affects compilation??? can u correct dis???If we're going to make the effort to help you, you could show willing and make the effort to spell out all your words and generally make your sentences as easy to understand as possible.
    Yes, the classpath affects compilation as the link provided shows. YOU can correct this.
    >
    e:\one\>javac -classpath e:\one\cert B.java
    >
    The classpath points at directories (or JAR files) not Java source files. Read the link.

  • Public access specifier in interface

    I have a slight problem with my code below. I created a file and named it ClosedCurve.java* and wrote all code into the file.
    My code below wont be compiled. unless I add an access specifier public . and I do not understand why i need to do that. I got an error that say "Cannot reduce the visibility of the inherited method from ClosedCurve"
    package classs.and.objects;
    public interface ClosedCurve {
        double computeArea();
    class Triangle implements ClosedCurve {
        double computeArea() // I need to change to public double computeArea()
            return 0.0;
    }

    The default specifier within the interface (in fact the only specifier within the interface) is public. The default specifier (when none are noted) for classes is package-private which is less visible than public.
    Please have a look here: [Controlling Access to Members of a Class|http://java.sun.com/docs/books/tutorial/java/javaOO/accesscontrol.html]

Maybe you are looking for

  • How to get library stored external drive on new computer with song details

    hi, i currently have my itunes music stored on an external firewire drive connected to my powerbook. i would like to move this library with playlists, ratings and play counts etc intact to my new macbook pro, however i would still like the library st

  • Apple ID retrieval not working

    The wife cannot log into her Apple ID on any Apple products.  When trying to recover the password the e-mail is never received.  When going through to retrieve the login information it claims the information provided (e-mail and birthdate) are unknow

  • Use navigateToURL to open a local pdf...

    I have a problem with the following code. I have been searching since yesterday for solutions to open pdfs in a browser window from within flash. I've found various forum posts about using navigateToURL and it looks like people have success. It seems

  • Library not showing up in Home sharing?

    Both my Mac and PC have Home Sharing enabled, yet my music and playlists are not showing up on my other computer. Both computers are authorized. Help?

  • Storage Manager and Windows 2012 Deduplication

    Does anyone have Storage Manager working with the deduplicated volumes in Windows 2012? It works fine on 2012 itself, but when I attempt to move data to or from a deduplicated volume I get the following error: 182: the requested operation is not supp