Home » Java Default Keyword

Java Default Keyword

by Online Tutorials Library

Java default keyword

A Java default keyword is an access modifier. If you didn’t assign any access modifier to variables, methods, constructors and, classes, by default, it is considered as default access modifier.

Points to remember

  • The default access modifier is accessible within the package only.
  • Unlike private and protected, we can create a default outer class by not assigning any access modifier to it. In such a case, it not restricted to take class name similar to a program name.
  • If you are overriding any method, overridden method (i.e., declared in the subclass) must not be more restrictive. So, the default method or variable can’t be allowed to use private access modifier.

Examples of default keyword

Example 1

Let’s see an example to determine whether the default variable is accessible or not outside the package.

Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:   The field A.msg is not visible  

Example 2

Let’s see an example to determine whether the default variable is accessible or not outside the class within the package.

Output:

Try to access the default variable outside the class within the package  

Example 3

Let’s see an example to determine whether the default method is accessible or not outside the package.

Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:   The method msg() from the type A is not visible  

Example 4

Let’s see an example to determine whether the default method is accessible or not outside the package using inheritance.

Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:   The method msg() from the type A is not visible  

Example 5

Let’s see an example to determine whether we use default outer class.

Output:

Try to access outer default classs  

Example 6

Let’s see an example to determine whether we create the instance of default constructor from outside the class.

Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:   The constructor A(String) is not visible  

Example 7

Let’s see an example to determine whether the default method is overridden to sub-class using default access modifier.

Output:

Try to access the overridden method  

Example 8

Let’s see an example to determine whether the default method is overridden to sub-class using private access modifier.

Output:

Exception in thread "main" java.lang.Error: Unresolved compilation problem:   Cannot reduce the visibility of the inherited method from A  

Example 9

Let’s see an example to determine whether the default method is overridden to sub-class using default access modifier.

Output:

Try to access the overridden method  

Example 10

Let’s see an example to determine whether the default method is overridden to sub-class using public access modifier.

Output:

Try to access the overridden method  
Next TopicJava Keywords

You may also like