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: 10036
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: 10036
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 (or inherited from parent) 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: 10036
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.

richardsmw0
Posts: 2
Joined: Wed Aug 25, 2021 7:37 pm
Contact:

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

Post by richardsmw0 »

Ok I'm a bit confused about this question vs another question. Having to do with what exactly is the class of the reference.

This question has this code and this explanation
Flyer f = new Eagle("American Bald Eagle");
When you try to use f.name, the class of the reference f is Flyer and Flyer has no field named "name",

question 6 has this code and explanation
Movable m = new Donkey();
the reference m refers to a Donkey at run time

Why is f a Flyer and in question 6 m is a Donkey? Shouldn't either f be and Eagle or m be a Movable?

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

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

Post by admin »

>Flyer f = new Eagle("American Bald Eagle");
Here the class of the reference (or more precisely, the declared type of the reference) f is Flyer. The type of the object to which f is pointing, is Eagle.

>Movable m = new Donkey();
Again, the declared type of the reference m is Movable and the type of the object to which m is pointing is Donkey.

During compilation, the compiler verifies that the declared type of the reference has the field or the method that you are trying to invoke on that reference. So, f.name will not compile because Flyer doesn't have a field named f.

During runtime, the JVM checks the actual class of the object to which the reference is pointing, and accesses the field or the method of that class.

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: Bing [Bot] and 44 guests