About Question enthuware.ocajp.i.v8.2.910 :

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

Moderator: admin

Post Reply
jamesmccreary
Posts: 22
Joined: Sun Jan 15, 2017 10:51 pm
Contact:

About Question enthuware.ocajp.i.v8.2.910 :

Post by jamesmccreary »

Two questions:

1. How is "System.out.println(((Eagle)f).name)" valid if class Eagle does not have a name variable? Variables are never inherited, so Eagle is unaware of class Bird's name variable. Not that it matters, but interface Flyer (reference type of f) does not have a name variable either.

2. The explanation states "f.getName() will work because Flyer does have a getName() method". Since the actual object reference variable f is pointing to is of type Eagle, Eagle's getName() method (which is inherited from class Bird) is called, yes? Does it then matter whether or not the Flyer interface has a getName() method? I believe the description should say that "... will work because Bird (which Eagle implements) does have a getName() method". I may be incorrect.

Thank you so much for all your help, I am awed by your knowledge of Java!

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

Re: About Question enthuware.ocajp.i.v8.2.910 :

Post by admin »

jamesmccreary wrote:Two questions:

1. How is "System.out.println(((Eagle)f).name)" valid if class Eagle does not have a name variable? Variables are never inherited, ...
Where did you read that? Either you should change your source or read it again thoroughly. All instance members (methods as well as as fields) except private ones are inherited.
2. The explanation states "f.getName() will work because Flyer does have a getName() method". Since the actual object reference variable f is pointing to is of type Eagle, Eagle's getName() method (which is inherited from class Bird) is called, yes? Does it then matter whether or not the Flyer interface has a getName() method?
Yes, it does matter. The declared type of f is Flyer and if Flyer doesn't declare getName(), f.getName() will not compile.
If you like our products and services, please help us by posting your review here.

jamesmccreary
Posts: 22
Joined: Sun Jan 15, 2017 10:51 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.910 :

Post by jamesmccreary »

Hi Paul, thank you for your response.

My First Question
I created a simple program and indeed variables are inherited. I believe I was confusing variable inheritance with calling a variable directly on a reference variable. From my program, I determined that if the subclass declares the same variable name and type as its parent, the subclass will "hide the parent variable" with its own variable, and when this variable is called by itself (e.g. variableName instead of Object.variableName)it will become the new variable when this variable name is accessed.

My Second Question
Let me ask if I understand this correctly:

f is of type Flyer pointing to an object of type Eagle. The compiler checks whether any method called on f indeed exists in Flyer, but at run-time the JVM invokes the method of the actual object (Eagle). Thus, if the compiler did not exist, the JVM would be OK with you calling "f.getName();" even if Flyer did not have a getName() method, yes?

Thus, a method can be called on the actual object only if it exists in the super class (Flyer), due to the compiler check.

Is that correct?

Thank you Paul, I will contribute to this forum as much as I can in gratitude for your patience =).

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

Re: About Question enthuware.ocajp.i.v8.2.910 :

Post by admin »

jamesmccreary wrote: My Second Question
Let me ask if I understand this correctly:

f is of type Flyer pointing to an object of type Eagle. The compiler checks whether any method called on f indeed exists in Flyer, but at run-time the JVM invokes the method of the actual object (Eagle).
Correct.
Thus, if the compiler did not exist, the JVM would be OK with you calling "f.getName();" even if Flyer did not have a getName() method, yes?
Correct.
Thus, a method can be called on the actual object only if it exists in the super class (Flyer), due to the compiler check.
Is that correct?
No, a method can be called on the actual object only if it is declared or defined in the class of the reference variable on which it is being called.

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

jamesmccreary
Posts: 22
Joined: Sun Jan 15, 2017 10:51 pm
Contact:

Re: About Question enthuware.ocajp.i.v8.2.910 :

Post by jamesmccreary »

Thank you, that pretty much solidifies my understanding of inheritance now!

Ricc123
Posts: 1
Joined: Mon Aug 06, 2018 2:44 am
Contact:

Re: About Question enthuware.ocajp.i.v8.2.910 :

Post by Ricc123 »

Hi there,

I have a quick question about the statement at the end of the explanation:
f.getName() will work because Flyer does have a getName() method.
When f.getName() is called, does it return the actual name created by the Eagle-class constructor? Since getName doesn't have an implementation in the Flyer interface, how does it return the correct name?

Thanks in advance!

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

Re: About Question enthuware.ocajp.i.v8.2.910 :

Post by admin »

Unfortunately, your question raises quite a few red flags in my mind. Nothing is being created in Eagle constructor and that Flyer doesn't have an implementation for getName, is not important because it always the class of the object pointed to by a f that is required to have an implementation (directly or indirectly through inheritance) of an instance method for that method to be invoked.

Red flags are raised because these are very basic fundamental OOP concepts that you should be aware of before attempting the mock exams. So I will sincerely suggest you to stop attempting the mocks and focus on getting your basics right by reading a good book.



HTH,
Paul.
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 3 guests