• Post Reply Bookmark Topic Watch Topic
  • New Topic

Abstract Class-Inheritance - Interface question  RSS feed

 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
During my studying I came up with this question in  SYBEX Certified Associate Java® SE 8 Programmer I, Study Guide, Exam 1Z0-808 page xxxii


Because I could not understand it, I typed the code in Eclipse and created the above classes.
I understand that there is a compiler error in line 5.
When an abstract class implements an interface, it inherits all its abstract methods.
In this case the astract class chooses to implement the method, so it overrides it. But when overriding the access modifier should be the same as the parent, or it should have more access than the parent.
In this case that does not apply here so it creates a compiler error.
I undesrtood this case.

The other thing that I cannot understand is that there is one more compiler error on line 9. In Eclipse it says
"The inherited method puma.getTailLength() cannot hide the public abstract method in HasTail"
I cannot understand what it means
Can you help me??
Thanks in advance.
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ioanna Katsanou wrote:But when overriding the access modifier should be the same as the parent, or it should have more access than the parent.
Correct. So it can be public only in this case, because there are no other less strict access modifier than public, hence you get compiler error within Puma class.

=Ioanna Katsanou wrote:there is one more compiler error on line 9.
In the code you posted there is not. Check if you copied code correctly.
 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:In the code you posted there is not. Check if you copied code correctly.


The compiler error appears only when I have also the compiler error in line 5. If I change the access modifier of the getTailLength method to public then the second compiler error in line 9 disappears.

I just cannot understand what causes the second compiler error
Here is a print screen:

screen.jpg
[Thumbnail for screen.jpg]
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ioanna Katsanou wrote:
The compiler error appears only when I have also the compiler error in line 5. If I change the access modifier of the getTailLength method to public then the second compiler error in line 9 disappears.

I just cannot understand what causes the second compiler error


Remember that the compiler may get confused after a compile error. A compile error may cause another compile error. A compile error may actually hide another compile error. Etc.

It is probably not an efficient use of time, to try to figure out how a compile error confuses the compiler, that it triggers another compile error (that may or may not be correct).

Henry
 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
Ioanna Katsanou wrote:
The compiler error appears only when I have also the compiler error in line 5. If I change the access modifier of the getTailLength method to public then the second compiler error in line 9 disappears.

I just cannot understand what causes the second compiler error


Remember that the compiler may get confused after a compile error. A compile error may cause another compile error. A compile error may actually hide another compile error. Etc.

It is probably not an efficient use of time, to try to figure out how a compile error confuses the compiler, that it triggers another compile error (that may or may not be correct).

Henry


I agree with that, but I have to know why the second compiler error is caused, because the exam question was "how many compiler errors does this code have and in which lines?"
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ioanna Katsanou wrote:
I agree with that, but I have to know why the second compiler error is caused, because the exam question was "how many compiler errors does this code have and in which lines?"


... Well ... Good luck with the investigation then.

Henry
 
Dave Tolls
Ranch Foreman
Posts: 3065
37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not the compiler.
I'm pretty sure that's Eclipse.
It's code scanning has probably got confused somewhere.

If you want to know actual compiler errors I would recommend double checking by compiling on the command line.
 
Ioanna Katsanou
Ranch Hand
Posts: 131
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dave Tolls wrote:That's not the compiler.
I'm pretty sure that's Eclipse.
It's code scanning has probably got confused somewhere.

If you want to know actual compiler errors I would recommend double checking by compiling on the command line.


Thank I will try that.
In the book it says that there is a compiler error on this line with the following explanation:

the class Cougar implements an overloaded version
of getTailLength(), but since the declaration in the parent class Puma is invalid,
it needs to implement a public version of the method. Since it does not, the declaration
of Puma is invalid, so option D is correct (option D is compiler error on this line)
 
Liutauras Vilda
Sheriff
Posts: 4923
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ioanna Katsanou wrote:In the book it says that there is a compiler error on this line with the following explanation:

the class Cougar implements an overloaded version
of getTailLength(), but since the declaration in the parent class Puma is invalid,
it needs to implement a public version of the method. Since it does not, the declaration
of Puma is invalid, so option D is correct (option D is compiler error on this line)

So it is correct, exactly what we all said. There is only 1 compiler error in the Puma class. Dave suggested to use command line compiler, in fact book suggests the same - so follow that, will be more beneficial for exam and for your future.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!