Not sure which constructor are you talking about. I don't see any constructor here. Syntax of super dot only allows you to go one level up. You can put a print statement in the methods and try it out.
If you like our products and services, please help us by posting your review here.
Again, I have no idea what you are trying to say by, "super constructor will allow only one level up from the class its been called". What exactly are you trying to do?
If you like our products and services, please help us by posting your review here.
class A{
public void m1() { }
}
class B extends A{
public void m1() { }
}
class C extends B{
public void m1(){
/* //1
... lot of code.
*/
}
}
ans-You can access class B's m1() using super.m1() from class C.
according to above answer , doesn't using super.m1() allows the constructor to use the class A's method from C ?
here what is super.m1().
let for example there are three classes with same method class A,Band C
i am asking If we call a method using Super().method from class C which method super will invoke, class A's or B's ?
The explanation is talking about the method m1. It is not talking about a constructor. super.m1() is a way to invoke the immediate superclass's version of m1 instead of the current class's. Thus, as the explanation says, if you call super.m1 from class C, it will invoke B's m1 (and not A's). If you invokes super.m1() from class B, then it will invoke A's m1.
Why the program tells me: "There is no construct like super.super. So, there is no way you can access m1() of A from C."? What does it means? So if a sub-class haven't got a constructor that invocates a super constructor i cant' use its methods?
Can someone help me to chiarify this question please?
It means that you can't access A's member from C using some thing like super.super.methodInA() even though you can access B's member from C using super.methodInB();.
So while super.something is valid, super.super.something is not valid.
If you like our products and services, please help us by posting your review here.
I want to understand two things pertaining to this question.
1) The first correct answer says - "You cannot access class A's m1() from class C for the same object ( i.e. this)."
Does this mean the following: class C extends B{
public void m1(){
this.m1();
}
But this will mean calling C's m1() itself. The meaning of the statement is quite confusing.
2) The second correct answer says - "You can access class B's m1() using super.m1() from class C."
Does it mean the following: class C extends B{
public void m1(){
super.m1(); //This is supposed to call class B's m1() method. }
Regards
Raj
I want to understand two things pertaining to this question.
1) The first correct answer says - "You cannot access class A's m1() from class C for the same object ( i.e. this)."
Does this mean the following: class C extends B{
public void m1(){
this.m1();
}
But this will mean calling C's m1() itself. The meaning of the statement is quite confusing.
The statement is talking about accessing A's implementation of m1 in an object of class C. If you try to call this.m1, as you have said, it will be calling its own m1, which proves it is not possible to invoke A's m1.
2) The second correct answer says - "You can access class B's m1() using super.m1() from class C."
Does it mean the following: class C extends B{
public void m1(){
super.m1(); //This is supposed to call class B's m1() method. }
Regards
Raj
Yes, when you do super.m1(), you are able to invoke B's m1.
If you like our products and services, please help us by posting your review here.
class A{
public void m1() { System.out.println("xyz"); }
}
class B extends A{
public void m1() { }
}
class C extends B{
public void m1(){
new A().m1();
}
}
class TestClass{
public static void main(String[] args){
new C().m1();
}
}
That statement is about that particular option. The option says, "You can access class A's m1() using ( (A) this ).m1() from class C." and the first sentence of the explanation, which you have ignored, says, "Note that selection of method to be executed depends upon the actual object class.".
You are taking the statement out of context. Your code shows that you are accessing some other object's m1, not of "this". The actual class of object in your code is not C but A.
If you like our products and services, please help us by posting your review here.
option B,C,D says "from class c" but they did not mention object is created for class C only . So if we create object for class A in class C then A's m1() is called and your explanation is little confusing as it says "no matter what you do u cannot call A's m() from Class C". And option A is clearly talking about C's object as it says "this" but remaining options are not clear on that. So here do we need to assume that "from class C" means object is created for class C ?
There is no need to assume anything in options b, c, and because complete line of code is given. B says, "using super.m1() ", C says, " using ( (A) this ).m1()", and D says, " using super.super.m1()", so, I am not sure where you think there is a confusion of using a reference to another object. If the code is using "super" and exists in class C, then it is quite clear it is talking about the current object which is an object of class C.
If you like our products and services, please help us by posting your review here.
i am clear with options but what i actually meant was the sentence in the explaination "super.super is an invalid construct. So, there is no way you can access m1() of A from C." if we create a object of class A in class C we can access A's m1() . so can u please remove that second part of that explanation which says no matter what u do u cannot access A's m1() from C.
And general question- So incase if only code under main method is given ,do we need to assume that it is written in a class or select option as "compile error" if there is such option given?
i am clear with options but what i actually meant was the sentence in the explaination "super.super is an invalid construct. So, there is no way you can access m1() of A from C." if we create a object of class A in class C we can access A's m1() . so can u please remove that second part of that explanation which says no matter what u do u cannot access A's m1() from C.
Sure, your feedback is taken into consideration and it will be updated if deemed necessary.
And general question- So incase if only code under main method is given ,do we need to assume that it is written in a class or select option as "compile error" if there is such option given?
Yes, you have to assume that the main method exists in a class. It can't run on its own. Also, you should assume that anything that is not given or shown in the question, is valid and will not affect the answer.
If you like our products and services, please help us by posting your review here.