About Question enthuware.ocajp.i.v7.2.854 : 22

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

Moderator: admin

Post Reply
nmarino
Posts: 3
Joined: Tue May 12, 2015 10:06 pm
Contact:

About Question enthuware.ocajp.i.v7.2.854 : 22

Post by nmarino »

Your comment,

"When the return type of the overridden method (i.e. the method in the base/super class) is a primitive, the return type of the overriding method (i.e. the method in the sub class) must match the return type of the overridden method."

Can't the return type be a covariant/subtype of the declared return type in the super class?

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

Re: About Question enthuware.ocajp.i.v7.2.854 : 22

Post by admin »

The concept of covariant return types doesn't apply to primitives. The statement is explicitly talking about primitives.
If you like our products and services, please help us by posting your review here.

nmarino
Posts: 3
Joined: Tue May 12, 2015 10:06 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.854 : 22

Post by nmarino »

ok I guess I misread some where.

So if I have a method in the super class that has a return type of animal, in the subclass if I override that method I could return a dog object as long as it extends the Animal class.

In addition if the super class return type was an Object, in the subclass I could return a String since all objects derive from Object?

Is that correct?
Last edited by nmarino on Wed May 13, 2015 7:32 pm, edited 1 time in total.

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

Re: About Question enthuware.ocajp.i.v7.2.854 : 22

Post by admin »

Yes, that is correct.
If you like our products and services, please help us by posting your review here.

nmarino
Posts: 3
Joined: Tue May 12, 2015 10:06 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.854 : 22

Post by nmarino »

Are you saying yes to both my questions? Sorry I added the second question to my post apparently while you were already replying when it only had the first question.

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

Re: About Question enthuware.ocajp.i.v7.2.854 : 22

Post by admin »

nmarino wrote:ok I guess I misread some where.

So if I have a method in the super class that has a return type of animal, in the subclass if I override that method I could return a dog object as long as it extends the Animal class.
Yes.
In addition if the super class return type was an Object, in the subclass I could return a String since all objects derive from Object?

Is that correct?
Yes.
If you like our products and services, please help us by posting your review here.

Mark7777
Posts: 32
Joined: Tue Apr 12, 2016 9:19 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.854 : 22

Post by Mark7777 »

And just to be clear, neither can the subclass inherit the superclass method since it would be an illegal overload because both have no-arg parameters which is not allowed?

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

Re: About Question enthuware.ocajp.i.v7.2.854 : 22

Post by admin »

Not sure what you mean. Please post sample code that illustrates the situation you are talking about. Also, please post the result of your compilation effort.
-Paul.
If you like our products and services, please help us by posting your review here.

Mark7777
Posts: 32
Joined: Tue Apr 12, 2016 9:19 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.854 : 22

Post by Mark7777 »

Okay. I didn't compile it. It's more of a theoretical query I have been having with several of these types of questions. Take this snippet from #22.

class A {
public int getCode(){ return 2;}
}

class AA extends A {
public long getCode(){ return 3;}
}

The answer includes the fact that AA's getCode() cannot override A's getCode() because of different return types, but when I see a question like this I also ask if it is a valid overload, or, whether AA inherits A's method getCode(). I feel as though that second part of the answer is left out of the analysis. Here, both methods have no-arg parameters so how can AA inherit A's method? It can't. So not only is it an invalid override, but it can't be a legal overload (by which I mean it can't be inherited by AA). Does that make sense? So my question could be: why isn't the issue of overloading mentioned in the answer in addition to the issue of overriding?

Mark

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

Re: About Question enthuware.ocajp.i.v7.2.854 : 22

Post by admin »

1. Overloading requires the parameter lists of the two methods to be different (in terms of type and order of the parameters. Not merely parameter names). So right off, no overloading is happening here.

2. Overloading or parameter list have nothing to do with inheritance. So I am not sure what you mean, "both methods have no-arg parameters so how can AA inherit A's method?".
If there is a non-private instance method in a super class, it will always, most certainly be inherited by the subclass. This inheritance is really reason why you cannot override the method in subclass with an incompatible return type. In other words, since this method is being inherited by the subclass, if you try to define another method with the same signature (signature means method name and parameter list. It does not include return type and throws clause), you are overriding it and so it must follow the rules for an override i.e. the return type and exception list must be compatible to the super class's method.

3. You should get into the habit of compiling your test programs from command line and observe the error messages given by the compiler. It will help you learn the concepts a lot quicker.

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

Mark7777
Posts: 32
Joined: Tue Apr 12, 2016 9:19 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.854 : 22

Post by Mark7777 »

Overloading or parameter list have nothing to do with inheritance.
I understand the overriding aspect of the question. I'm focused on overloading which according to Mala's book (p.300) is related to inheritance: a subclass "inherit[s] the variables and methods defined in the [superclass] and use them directly, as if they were defined in their own classes."

Accordingly, the superclass method of A is inherited and becomes part of the subclass AA. So it would look like this, although it would fail to compile because of identical argument lists:

class A {
public int getCode(){ return 2;}
}

class AA extends A {
public int getCode(){ return 2;} // Invisible, but inherited, though it has the same argument list.
public long getCode(){ return 3;}
}

The reason it looks like an illegal overload, (in addition to an illegal override) is because the following code would be a legal overload, right? As in the following:

class A {
public int getCode(){ return 2;}
}

class AA extends A {
public int getCode(){ return 2;} // Invisible, but inherited, though it has the same argument list.
public long getCode(long aLong){ return 3;} // Now we have an overload, right?
}

So if the getCode() of A is impliedly inserted into AA via inheritance, aren't we also dealing with an overload?

All I'm saying is that the exam's example fails for two reasons, not just one. A) illegal override, and B) illegal overload. Compilers are handy, but only to a point. They usually don't give all the reasons why something doesn't compile, eh?

m.

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

Re: About Question enthuware.ocajp.i.v7.2.854 : 22

Post by admin »

OK, I see what you mean. Yes, you could call it an illegal overload if your intention was to overload the getCode method but you would be going against the conventional analysis of the code.

Conventionally, since the signatures of the two methods are same, right away, it is a case of override (not overload). The message from the compiler implies the same. We don't even get into the domain of overload until the signatures are different.

Regarding using the compiler, it is a suggestion from our experience. You are free to ignore :) But we appreciate if the poster shows effort by actually trying to compile their code, include the error message in the post, and specify what they did not understand. Otherwise, it would just be spoon feeding.

-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], Google [Bot] and 56 guests