• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Two conclusions about overridden that I'm not sure to be right

 
Ranch Hand
Posts: 84
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys. I hope you're doing well. I'd like to see if you share my conclusion about the following cases:

* CASE 1
If we have an interface declaring a method and then a class that overrides that method in a wrong way, then, the subclass of this last class is obligated to override the method in the right way in order to compile. If it is not done, this class will inherit the wrong implementation.



-> Lines 2 and 4 do not compile.

* CASE 2
Unlike case 1, if we have a class (instead of an interface) that implement a method and then a subclass that override it in a wrong way, then if there is a third class that inherit from the last one and it does not properly override the method, there won't be any issue and anyway it will be compile.

 1 class HasTail{ protected int make(){ return 2; } }
 2 class TestExt extends HasTail{ int make(){return 4;} }
 3
 4 class Test2 extends TestExt{
 5     //public int make(){ return 0; }    
 6     public static void main(String[] args){
 7
 8     }
 9 }

-> Only line 2 does not compile while Test2 class compiles.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't add your own line numbers; the code tags add line numbers automatically. Please also tell us where the material comes from, to avoid copyright problems and to allow us to assess the original. I have seen that code before.
In line 2 you have a method with protected access, which cannot implement that inherited from the HasTail interface. Remember that interface methods are public by default. If the superclass won't compile, the subclass cannot compile either.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pablo Napoli wrote:. . .

Again you have a protected method which you are trying to override with package‑private access.

-> Only line 2 does not compile while Test2 class compiles.

The reason it compiles may be that you are reusing the name. You might have a TestExt class from elsewhere. Please check your directory to see whether there is a different TestExt.class file anywhere. Since yoiur second Test2 class doesn't declare any instance methods, it can be a subclass of anything with an accessible constructor and not final status.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't edit posts after a reply
 
Pablo Napoli
Ranch Hand
Posts: 84
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ritchie. Thanks for your answer and sorry for so many mistakes, I will have to read again some rules. I just wanted to edit the original post by deleting the numbers and adding the name of the book.

Regarding to the exercise, i rechecked it and i got the same result. So what i want to say is:

A (INTERFACE THAT DECLARES A METHOD)
B (CLASS THAT IMPLEMENTS A AND MAKES A WRONG OVERRIDEN)
C (CLASS THAT EXTENDS B)
====
DOES NOT COMPILE CLASSES B AND C

A (CLASS THAT DECLARES A METHOD)
B (CLASS THAT EXTENDS A AND MAKES A WRONG OVERRIDEN)
C (CLASS THAT EXTENDS B)
====
ONLY COMPILER SHOWS ERROR ON CLASS B

 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Pablo Napoli wrote:. . . sorry . . .

Apology accepted adding the name of the book.. . . .Please add the name of the book to your next post.

I hve had another look at your code and tried to run it. In the second case, Test2 produces no error messages because on its own that class would be correct. Because TestExp fails to compile, none of the three classes compiles and no new *.class file is created. The only class to compile on its own is HasTail with javac HasTail.java.
reply
    Bookmark Topic Watch Topic
  • New Topic