About Question enthuware.ocajp.i.v7.2.1359 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1359 :
would option 1 work if it was casting a C object to an A object in main() of some hypothetical test class for instance?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1359 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.1359 :
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
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
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1359 :
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.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
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.1359 :
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.
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.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1359 :
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.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.
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?
If you like our products and services, please help us by posting your review here.
-
- Posts: 37
- Joined: Thu Feb 13, 2014 12:58 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1359 :
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();
}
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();
}
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1359 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Sat Apr 18, 2015 7:07 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1359 :
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.
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.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1359 :
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.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.1359 :
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"?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1359 :
What happened when you tried it out?
If you like our products and services, please help us by posting your review here.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1359 :
What si the difference between hidden and shadowed variable?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1359 :
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
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
If you like our products and services, please help us by posting your review here.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1359 :
I read the word press article.
What is the difference between hidden variables and overriden method?
What is the difference between hidden variables and overriden method?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1359 :
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 202
- Joined: Mon Apr 02, 2018 8:40 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1359 :
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) .
Last edited by flex567 on Wed Dec 26, 2018 8:11 am, edited 1 time in total.
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1359 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 3
- Joined: Sat Mar 16, 2019 6:28 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1359 :
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?
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?
-
- Site Admin
- Posts: 10046
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1359 :
Yes.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 73 guests