super用于在 派生类中调用父类的重名方法,或者引用重名的变量。
super被用在派生类中,就是为了明确调用父类的方法。
示例(很浅显易懂):
FROM: http://stackoverflow.com/questions/3767365/super-in-java
-------------------------------------------------
1.Super keyword is used to call immediate parent.
2.Super keyword can be used with instance members i.e., instance variables and instance methods.
3.Super keyword can be used within constructor to call the constructor of parent class.
OK now let’s practically implement this points of super keyword.
Check out the difference between program 1 and 2. Here program 2 proofs our first statement of Super keyword in Java.
Program 1
class base
{
int a = 100;
}
class sup1 extends base
{
int a = 200;
void show()
{
System.out.println(a);
System.out.println(a);
}
public static void main(String[] args)
{
new sup1().show();
}
}
Output : -
200
200
Now check out the program 2 and try to figure out the main difference.
Program 2
class base
{
int a = 100;
}
class sup2 extends base
{
int a = 200;
void show()
{
System.out.println(super.a);
System.out.println(a);
}
public static void main(String[] args)
{
new sup2().show();
}
}
Output
100
200
In the program 1, the output was only of the derived class. It couldn’t print the variable of base class or parent class. But in the program 2, we used a super keyword with the variable a while printing its output and instead of printing the value of variable a of derived, it printed the value of variable a of base class. So it proofs that Super keyword is used to call immediate parent.
OK now check out the difference between program 3 and program 4
Program 3
class base
{
int a = 100;
void show()
{
System.out.println(a);
}
}
class sup3 extends base
{
int a = 200;
void show()
{
System.out.println(a);
}
public static void main(String[] args)
{
new sup3().show();
}
}
Output
200
Here the output is 200. When we called the show function, the show function of derived class was called. But what should we do if we want to call the show function of the parent class. Check out the program 4 for solution.
Program 4
class base
{
int a = 100;
void show()
{
System.out.println(a);
}
}
class sup4 extends base
{
int a = 200;
void show()
{
super.show();
System.out.println(a);
}
public static void main(String[] args)
{
new sup4().show();
}
}
Output
100
200
Here we are getting two output 100 and 200. When the show function of derived class is invoke, it first calls the show function of parent class because inside the show function of derived class we called the show function of parent class by putting the super keyword before the function name.
相关推荐
java中super关键字的三种用法
Java super关键字使用demo
java中super关键字的三种用法.pdf
javathis和super用法 this和 super 关键字的区别
主要介绍了java之super关键字用法实例解析,较为详细的分析了super关键字的用法及内存分布,需要的朋友可以参考下
主要介绍了Java super关键字用法,结合具体案例形式分析了java super关键字调用父类构造方法、属性及方法等相关操作技巧与注意事项,需要的朋友可以参考下
因为子类中的成员变量或方法名优先级高,所以子类中的同名成员变量或方法隐藏了父类的成员变量或方法,但是我们如果想要使用父类中的这个成员变量或方法,需要用到super。 3.super(参数):调用
主要介绍了Java的super关键字与instanceof运算符使用方法,是Java入门学习中的基础知识,需要的朋友可以参考下
this和super ,这两个变量在使用前都是不需要声明的.this变量使用在一个成员函数的内部,指向当前对象,当前对象指的是调用当前正在执行方法的...因此它们都是非常有用的变量,下面我想介绍一下this和super的使用方法......
主要介绍了Java super关键字的使用方法详解的相关资料,希望通过本文能帮助到大家,让大家对super关键字彻底掌握,需要的朋友可以参考下
主要介绍了Java编程中this关键字与super关键字的用法示例,super是this的父辈,在继承过程中两个关键字经常被用到,需要的朋友可以参考下
主要介绍了Java编程中this与super关键字的用法,包括从内存结构方面分析super的作用域,需要的朋友可以参考下
总结了Java学习中的52个关键字(包括一些总结),如果觉得自己Java基础知识需要总结一下的,可以下载下来瞧瞧 2.this和super 1>在你的方法中的某个刑参名与当前对象的某个成员有相同的名字,这 时为了不至于混淆,...
System.out.println("抽象方法中,Name = "+super.NAME); } } public class Demo01 { public static void main(String args[]) { Student s = new Student(); s.fun(); s.print(); } } abstract ...
主要介绍了java中this与super关键字的使用方法的相关资料,希望通过本文能帮助到大家,让大家彻底理解应用java中this与super,需要的朋友可以参考下
主要介绍了java 关键字super详解及用法的相关资料,需要的朋友可以参考下
主要介绍了Java中一些关键字的使用技巧总结,其中重点讲述了this和super两个关键字的用法,需要的朋友可以参考下
静态方法,也就是类方法中不能有this和super关键字 1. 子类的构造函数如果要引用super的话,必须把super放在函数的首位. 复制代码 class Base { Base() { System.out.println("Base"); } } public ...
static、this、super、final的JAVA用法
熟悉使用@Override注解重写父类方法和使用super关键字调用父类方法。 阅读建议: 建议读者在阅读前具备Java的基础知识,包括类、对象、方法和成员变量等概念。 在阅读过程中,尝试编写简单的Java程序,创建并使用...