首页 > 编程语言 > 详细

Java Keyword -- super

时间:2015-09-24 09:22:11      阅读:179      评论:0      收藏:0      [点我收藏+]

Reference: super

When we override superclass‘s methods, but still want to invoke them, we can use keyword super in child classes. We can also use super to refer to a hidden field (although hiding fields is discouraged).

// Superclass is parent class
public class Superclass {

    public void printMethod() {
        System.out.println("Printed in Superclass.");
    }
}

// Subclass is child class
public class Subclass extends Superclass {

    // overrides printMethod in Superclass
    public void printMethod() {
        super.printMethod();
        System.out.println("Printed in Subclass");
    }
    public static void main(String[] args) {
        Subclass s = new Subclass();
        s.printMethod();    
    }
}

 Compiling and executing Subclass prints the following:

Printed in Superclass.
Printed in Subclass

We can also use super() or super(parameter list) to invoke superclass‘s constructor.

 

Java Keyword -- super

原文:http://www.cnblogs.com/ireneyanglan/p/4834157.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!