博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
default_Keyword
阅读量:3926 次
发布时间:2019-05-23

本文共 6218 字,大约阅读时间需要 20 分钟。

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.

//save by A.java

package com.java;    public class A {     String msg="Try to access default variable outside the package";         }

//save by DefaultExample1.java

package com.javatpoint;  import com.java.A;    public class DefaultExample1 {  public static void main(String[] args) {      A a=new A();     System.out.println(a.msg);        }  }

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.

class A {     String msg="Try to access the default variable outside the class within the package";         }    public class DefaultExample2 {  public static void main(String[] args) {      A a=new A();     System.out.println(a.msg);        }  }

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.

//save by A.java    package com.java;    public class A {    void msg()   {       System.out.println("Try to access default method outside the package ");   }         }
//save by DefaultExample2.java  package com.javatpoint;  import com.java.A;    public class DefaultExample2 {  public static void main(String[] args) {      A a=new A();     a.msg();        }  }

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.

//save by A.java    package com.java;    public class A {      void msg()   {       System.out.println("Try to access default method outside the package using inheritance");   }         }
//save by DefaultExample4.java  package com.javatpoint;  import com.java.A;    public class DefaultExample4 extends A {  public static void main(String[] args) {      DefaultExample4 a=new DefaultExample4();     a.msg();        }  }

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.

class DefaultExample5 {  void display()  {      System.out.println("Try to access outer default class");  }  public static void main(String[] args) {      DefaultExample5 p=new DefaultExample5();      p.display();        }  }

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.

package com.java;    public class A  {   String msg;  A(String msg)   {      this.msg=msg;   }  public void display()  {      System.out.println(msg);  }         }
//save by DefaultExample6.java  package com.javatpoint;  import com.java.A;        public class DefaultExample6 {  public static void main(String[] args) {      A a=new A("Try to create the instance of default constructor outside the package");       a.display();          }  }

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.

class A  {      void msg()      {          System.out.println("Try it");      }  }  //save by DefaultExample7.java  class DefaultExample1 extends A {      void msg()  {      System.out.println("Try to access the overridden method");  }  public static void main(String[] args) {      DefaultExample7 p=new DefaultExample7();      p.msg();        }  }

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.

class A  {      void msg()      {          System.out.println("Try it");      }  }    class DefaultExample8 extends A {      private void msg()  {      System.out.println("Try to access the overridden method");  }  public static void main(String[] args) {      DefaultExample8 p=new DefaultExample8();      p.msg();        }  }

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.

class A  {      void msg()      {          System.out.println("Try it");      }  }    class DefaultExample9 extends A {      protected void msg()  {      System.out.println("Try to access the overridden method");  }  public static void main(String[] args) {      DefaultExample9 p=new DefaultExample9();      p.msg();        }  }

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.

class A  {      void msg()      {          System.out.println("Try it");      }  }    class DefaultExample10 extends A {      public void msg()  {      System.out.println("Try to access the overridden method");  }  public static void main(String[] args) {      DefaultExample10 p=new DefaultExample10();      p.msg();        }  }

Output:

Try to access the overridden method

转载地址:http://dbgrn.baihongyu.com/

你可能感兴趣的文章
zoj 2107 Quoit Design(最近点对问题,好好思考,分治)
查看>>
zoj 2111 Starship Troopers(树形DP)
查看>>
vector 容器的使用方法
查看>>
hdu 1520 Anniversary party(基本树形DP)
查看>>
poj 1463 Strategic game(树形DP)
查看>>
poj 3342 Party at Hali-Bula(树形DP+判断方式是不是唯一)
查看>>
Problem 2129 子序列个数 (动态规划题目,注意模余的问题)
查看>>
fzu Problem 2138 久违的月赛之一
查看>>
poj 1947 Rebuilding Roads(树形DP)
查看>>
ural 1056 Computer Net(树形DP)需要用到两遍dfs
查看>>
poj 1848 Tree(树形DP,太难了,三种状态,四种状态转换)
查看>>
zoj 3626 Treasure Hunt I(树形DP+分组背包)
查看>>
hdu 1011 Starship Troopers(树形DP+背包问题)
查看>>
poj 3107 Godfather(树形DP,点的个数较多, 删点使得剩余部分结点最多的最小值)
查看>>
poj 2378 Tree Cutting(树形DP,删点使得独立的部分结点数不超过n/2)
查看>>
poj 1655 Balancing Act(树形DP,删点)
查看>>
hdu 4276 The Ghost Blows Light(树形DP+最短路+分组背包)好题。。。
查看>>
zoj 3537 Cake(区间DP+最优三角形剖分)待续
查看>>
poj 2955 Brackets(区间DP,经典问题)求有规律的括号的最大长度
查看>>
hdu 1754 I Hate It(线段树,单点替换,求区间最值)
查看>>