Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default access level for methods in a public abstract class in Java?

Tags:

java

Normally the default access level of methods is package local. But it seems to me that it is different for public abstract classes. In those classes the default seems to be public. Is this correct?

Update

@EJP

It was a bug in my code. It is possible to shadow the package local method with a public method, which confuses me. This makes me think that a public abstract could be similar to an interface where the methods are public. See the example:

a/A.java:

package a;

public abstract class A
{
    String a () { return "a"; }
}

test_a.java:

class test_a
{
    static class NewA extends a.A
    {
        public String a () { return "new a"; }
    }

    public static void main (String[] args)
    {
        NewA a = new NewA();
        System.out.println(a.a());
    }
}
like image 740
ceving Avatar asked Jun 21 '13 09:06

ceving


People also ask

What is the default access level in Java?

The default access is also referred to as package-private. Think of a package as your home, classes as rooms, and things in rooms as variables with default access. These things aren't limited to one room—they can be accessed across all the rooms in your home.

What is the default access modifier for abstract class?

Abstract Access Modifier is a modifier applicable only for classes and methods but not for variables. If we declare any method as abstract then that method must have its implementation in the child class of the respective class because abstract methods never talk about implementation.

Are abstract methods public by default in Java?

By default, all methods are public and abstract until we do not declare it as default and properties are static and final.

What is the default access specifier for a method of a class?

Default: When no access modifier is specified for a class, method, or data member – It is said to be having the default access modifier by default. The data members, class or methods which are not declared using any access modifiers i.e. having default access modifier are accessible only within the same package.


1 Answers

The default visibility is known as “package” (though you can't use this keyword), which means the field will be accessible from inside the same package to which the class belongs.

uf you declare as public than it will be public for all no matter its abstract or not

like image 120
shreyansh jogi Avatar answered Sep 18 '22 18:09

shreyansh jogi