英文:
Whats difference between output of any function whose return type is int and is used with super() and without super() keyword?
问题
I am confused on the functioning of super( ) keyword in java .
If I donnot use super keyword then its not giving any "return value of function".
While i am using super kayword then its shows the "return value of function".
Can anybody Explain me about that reason ?
英文:
I am confused on the functioning of super( ) keyword in java .
If I donnot use super keyword then its not giving any "return value of function".
While i am using super kayword then its shows the "return value of function".
Can anybody Explain me about that reason ?
答案1
得分: 1
super关键字指的是超类(父类)对象。它用于调用超类方法和访问超类构造函数。
我将逐行解释您的输出:
第一行
圆的面积将为314.0,来自以下代码:
由于您正在创建Child_Class的实例并调用其方法
Child_Class cc = new Child_Class();
cc.area();
第一行
圆的面积将为314.0,来自以下代码,当您调用父类的area方法时:
super.area();
第三行:
10
来自打印super.area()返回的值,因为super.area()返回了硬编码为10的半径。在这里,您正在调用父类的area方法。
System.out.println(super.area());
第四行 正方形的面积将为100,来自以下代码:
System.out.println("正方形的面积将为" + side * side);
当您不使用super关键字时,这意味着您未调用父类方法。
英文:
The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to access the superclass constructor.
I will explain your output line by line :
First line
Area of circle will be 314.0 comes from the code:
Since you are creating instance of Child_Class and calling its method
Child_Class cc= new Child_Class()
cc.area();
First line
Area of circle will be 314.0 comes from the code when u are calling the parent class area method
super.area();
Third line:
10
comes from printing the value returned by super.area() since super.area() returns the radius which is hardcoded as 10. Here u are calling the parent class area method.
System.out.println(super.area());
Fourth line Area of square will be 100 comes from
System.out.println("Area of square will be " + side*side);
When u dont use super it simply means that you are not invoking the parent class method.
答案2
得分: 0
super()方法指的是父类。在这个示例中,Child_Class是父类(或超类),Shapes是子类。你可以看出shapes扩展了Child_Class。
super.area()指的是父类中的area()方法,也就是Child_Class中的方法。这是将被返回的值。
英文:
The super() method i referring to the parent class. In this example, Child_Class is the parent class (or super class) and Shapes is the child class. You can tell because shapes extends Child_Class.
super.area() is referring to the area() method in the parent class, so the one in Child_Class. The is the value that will be returned.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论