Page 1 of 1

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

Posted: Wed Feb 08, 2017 12:12 pm
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!

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

Posted: Wed Feb 08, 2017 8:14 pm
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.

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

Posted: Thu Feb 09, 2017 12:31 pm
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 =).

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

Posted: Thu Feb 09, 2017 1:51 pm
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.

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

Posted: Fri Feb 10, 2017 11:05 am
by jamesmccreary
Thank you, that pretty much solidifies my understanding of inheritance now!

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

Posted: Mon Aug 06, 2018 2:50 am
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!

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

Posted: Mon Aug 06, 2018 3:16 am
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.

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

Posted: Wed Aug 25, 2021 7:45 pm
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?

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

Posted: Thu Aug 26, 2021 1:49 am
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.