java 中关键字和保留字_Java中的超级关键字

java 中关键字和保留字

Super refer to parent class. It holds the reference id of
parent section of child class object. Observe below example.
class A
{
                int
x=10;
}
class B extends A
{
                int
x=20;
}
Here you can see that in child object, two separate sections
are created namely parent section and child section. So super actually refer to
parent section.

Use of Super Keyword in Java

There are three usage of super keyword in java.
  • Access parent class instance variable.
  • Invoke parent class method.
Below program will show all the three usage of super
keyword.
class parent
{
                int x=20;              
                parent()
                {
                                System.out.println("Parent");
                }
 
                void show()
                {
                                System.out.println("Parent Show");
                }
}
 
class child extends parent
{
                int x=30;
 
                child()
                {
                                super();                                //invoke parent constructor
                }
 
                void show()
                {
                                System.out.println("Child Show");
                }
 
                void display()
                {
                                System.out.println(x);                   //print child x
                                System.out.println(super.x);      //print parent x
 
                                show();                                //invoke child show()
                                super.show();   //invoke parent show()
                }
 
                public static void main(String...s)
                {
                                child c=new child();
                                c.display();
                }
}
  • Here both the classes have variable x and method show(). To access variable of child class we simply write x and to access variable of parent class we write super.x.
  • Similarly, to invoke method of child we simple write

    show() and to invoke method of

    parent we write super.show(). In this way super is also used to remove the problem

    of

    method overriding.

  • super() must be first line of the constructor. In above

    example we have written super() in first line of child class constructor to

    call parent class constructor.

  • Super can’t be used in static method.
  • By default super is first line of constructor. Super keyword

    and

    this keyword can’t be used together in constructor.

Super refer to parent class. It holds the reference id of
parent section of child class object. Observe below example.
class A
{
                int
x=10;
}
class B extends A
{
                int
x=20;
}
Here you can see that in child object, two separate sections
are created namely parent section and child section. So super actually refer to
parent section.

Use of Super Keyword in Java

There are three usage of super keyword in java.
  • Access parent class instance variable.
  • Invoke parent class method.
Below program will show all the three usage of super
keyword.
class parent
 {
                 int x=20;              
                 parent()
                 {
                                 System.out.println("Parent");
                 }
  
                 void show()
                 {
                                 System.out.println("Parent Show");
                 }
 }
  
 class child extends parent
 {
                 int x=30;
  
                 child()
                 {
                                 super();                                //invoke parent constructor
                 }
  
                 void show()
                 {
                                 System.out.println("Child Show");
                 }
  
                 void display()
                 {
                                 System.out.println(x);                   //print child x
                                 System.out.println(super.x);      //print parent x
  
                                 show();                                //invoke child show()
                                 super.show();   //invoke parent show()
                 }
  
                 public static void main(String...s)
                 {
                                 child c=new child();
                                 c.display();
                 }
 }
  • Here both the classes have variable x and method show(). To access variable of child class we simply write x and to access variable of parent class we write super.x.
  • Similarly, to invoke method of child we simple write

    show() and to invoke method of

    parent we write super.show(). In this way super is also used to remove the problem

    of

    method overriding .

  • super() must be first line of the constructor. In above

    example we have written super() in first line of child class constructor to

    call parent class constructor.

  • Super can’t be used in static method.
  • By default super is first line of constructor. Super keyword

    and

    this keyword can’t be used together in constructor.

翻译自: https://www.thecrazyprogrammer.com/2014/10/super-keyword-in-java.html

java 中关键字和保留字

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值