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

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
ETS User

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

Post 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?

:?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

javanaut

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

Post 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

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

ETS User

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

Post 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.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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?
If you like our products and services, please help us by posting your review here.

fasty23
Posts: 37
Joined: Thu Feb 13, 2014 12:58 am
Contact:

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

Post 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();
}

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

anjuvnr
Posts: 1
Joined: Sat Apr 18, 2015 7:07 pm
Contact:

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

Post 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.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Deleted User 3513

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

Post 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"?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

What happened when you tried it out?
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 »

What si the difference between hidden and shadowed variable?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post by flex567 »

I read the word press article.
What is the difference between hidden variables and overriden method?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

flex567
Posts: 202
Joined: Mon Apr 02, 2018 8:40 am
Contact:

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

Post 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) .
Last edited by flex567 on Wed Dec 26, 2018 8:11 am, edited 1 time in total.

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

gaborj
Posts: 3
Joined: Sat Mar 16, 2019 6:28 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10043
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 48 guests