Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1359 :

Posted: Tue Sep 25, 2012 11:39 pm
by ETS User
would option 1 work if it was casting a C object to an A object in main() of some hypothetical test class for instance?

:?

Re: About Question enthuware.ocajp.i.v7.2.1359 :

Posted: Wed Sep 26, 2012 4:57 am
by admin
No, that's the thing with polymorphism. If the actual object is of a subclass and if the subclass overrides a method, there is no way to invoke the superclass method from outside that object. For all practical purposes, the base class method has been overridden. It is not merely shadowed (as is the case with static methods) and so casting will not help either.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1359 :

Posted: Wed Sep 26, 2012 12:02 pm
by javanaut
Hi Paul,

Thank-you for the response. Do you mean there is no way to invoke the super class of the super class from inside the lowest subclass in the hierarchy?

Regards,

javanaut

Re: About Question enthuware.ocajp.i.v7.2.1359 :

Posted: Wed Sep 26, 2012 1:33 pm
by admin
javanaut wrote:Hi Paul,

Thank-you for the response. Do you mean there is no way to invoke the super class of the super class from inside the lowest subclass in the hierarchy?

Regards,

javanaut
Yes, that's what I mean but only with respect to overridden methods. Given class A<-- B<-- C, there is no way that you can invoke a method in A from C, if that method is overridden by B.

Re: About Question enthuware.ocajp.i.v7.2.1359 :

Posted: Fri Dec 07, 2012 11:29 am
by ETS User
Hi Paul,
In this eg. if each of the "public void perform_work()" is tweaked to add a "super.perform_work();" call then A's method can be reached from C's.
I do think in the explanation "There is no way to go more than one level up for methods." is true for the presented code but cannot be a generalized stmt.

Re: About Question enthuware.ocajp.i.v7.2.1359 :

Posted: Fri Dec 07, 2012 3:48 pm
by admin
ETS User wrote:Hi Paul,
In this eg. if each of the "public void perform_work()" is tweaked to add a "super.perform_work();" call then A's method can be reached from C's.
I do think in the explanation "There is no way to go more than one level up for methods." is true for the presented code but cannot be a generalized stmt.
Even in this case, it is not C's method that is calling A's method. C's method is only calling B's method. The fact that B's method calls A's method is unknown to C. So it is a correct generalized to say that there is no way to call A's method from C's method.

This is similar to a situation where a package private method (in A) is made accessible outside the package through a public method of another class (say, B) in the same package. Now, if C calls B's public method, which in turn calls A's package private method, would you say that A's method is accessible from C?

Re: About Question enthuware.ocajp.i.v7.2.1359 :

Posted: Thu Mar 06, 2014 11:44 pm
by fasty23
What about if we make an instance of A in class C? like this:
class A { public void perform_work(){} }
class B extends A { public void perform_work(){} }
class C extends B { public void perform_work(){}
A a=new A();
a.public void perform_work();
}

Re: About Question enthuware.ocajp.i.v7.2.1359 :

Posted: Fri Mar 07, 2014 1:17 am
by admin
Please observe that this discussion is about being able to call super class's method if the actual object is of the subclass and the subclass overrides that method. If you create an object of A, the question of B's or C's perform_work() does not arise because the object is not of B or C.

Re: About Question enthuware.ocajp.i.v7.2.1359 :

Posted: Sat Apr 18, 2015 7:14 pm
by anjuvnr
Admin,
Although your explanation makes sense, indicating the last option as the right answer doesn't make any sense. The last option also in this case is incorrect. The verbiage should have said "None of the above" instead of "It is not possible". As it is, all the four choices are incorrect.

Re: About Question enthuware.ocajp.i.v7.2.1359 :

Posted: Sat Apr 18, 2015 8:15 pm
by admin
No, "It is not possible" is correct as explained in the explanation. If there were a way to do that, then "None of the above" would have been valid.

Re: About Question enthuware.ocajp.i.v7.2.1359 :

Posted: Sun Feb 19, 2017 6:39 am
by Deleted User 3513
It is not possible to cast the keywords this and super, right? Just like what was done in option 1? And it would specifically give out an error saying "not a statement"?

Re: About Question enthuware.ocajp.i.v7.2.1359 :

Posted: Sun Feb 19, 2017 11:58 am
by admin
What happened when you tried it out?

Re: About Question enthuware.ocajp.i.v7.2.1359 :

Posted: Mon Dec 24, 2018 7:57 am
by flex567
What si the difference between hidden and shadowed variable?

Re: About Question enthuware.ocajp.i.v7.2.1359 :

Posted: Mon Dec 24, 2018 8:40 am
by admin
Try this: https://techsymphony.wordpress.com/2011 ... g-in-java/
or in Hanumant's book, shadowing is explained in section 3.4.2 under "When is this necessary" heading and hiding is explained in section 11.1.2

Re: About Question enthuware.ocajp.i.v7.2.1359 :

Posted: Wed Dec 26, 2018 6:22 am
by flex567
I read the word press article.
What is the difference between hidden variables and overriden method?

Re: About Question enthuware.ocajp.i.v7.2.1359 :

Posted: Wed Dec 26, 2018 6:32 am
by admin
flex567 wrote:
Wed Dec 26, 2018 6:22 am
I read the word press article.
What is the difference between hidden variables and overriden method?
Not sure what kind of difference are you looking for. One is a variable and one is a method. There is nothing like "overridden variable" if that is what you are asking.

Also, the questions that you are asking are too basic. You really should read a book from first page to the last page very carefully and very patiently. Only that can set your fundamentals straight.

Re: About Question enthuware.ocajp.i.v7.2.1359 :

Posted: Wed Dec 26, 2018 7:01 am
by flex567
I think some fundamental concepts are not well explain in most of the books and then you learn about it only from these exam questions(answers) .

Re: About Question enthuware.ocajp.i.v7.2.1359 :

Posted: Wed Dec 26, 2018 7:41 am
by admin
That is possible.
BTW, overriding and hiding are explained in detail in Section 11.1.2 of Hanumant's book. Let me know which part is not clear so that we can improve it.

Re: About Question enthuware.ocajp.i.v7.2.1359 :

Posted: Wed Mar 27, 2019 6:22 pm
by gaborj
hi Admin,

slightly modified version of fasty23's :
class C extends B {
public void perform_work(){ }
public void instanceMethod() {
A a = new A();
a.perform_work();
}
}

In this context the a's perform_work method is called from C's instance method, isn't it?

Re: About Question enthuware.ocajp.i.v7.2.1359 :

Posted: Wed Mar 27, 2019 7:02 pm
by admin
gaborj wrote:
Wed Mar 27, 2019 6:22 pm
hi Admin,

slightly modified version of fasty23's :
class C extends B {
public void perform_work(){ }
public void instanceMethod() {
A a = new A();
a.perform_work();
}
}

In this context the a's perform_work method is called from C's instance method, isn't it?
Yes.