Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Interfaces, Inheritance, and Implementation

 
Ranch Hand
Posts: 107
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,

Please could someone kindly explain why generates a compilation error? I do not think it is because it is NOT overriding the methods of X3. The IDE is not giving me any helpful info to determine the root cause. Thanks in advance for any help that you can give.

The source of this question comes from the Online Udemy Course "Pass the Oracle Certified Associate(OCA): Java SE 8 Programmer I EXAM"
Instructor: Udayan Khattry




 
Saloon Keeper
Posts: 12811
278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What error message are you getting? To me, that code looks valid and it should print "X3".
 
Clemonte Johnstone
Ranch Hand
Posts: 107
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:What error message are you getting? To me, that code looks valid and it should print "X3".


The code does not execute. It gives a compilation error on line 17
 
Clemonte Johnstone
Ranch Hand
Posts: 107
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Clemonte Johnstone wrote:

Stephan van Hulst wrote:What error message are you getting? To me, that code looks valid and it should print "X3".


The code does not execute. It gives a compilation error on line 17



Its polymorphism as well right as X1 is instantiating X?
 
Marshal
Posts: 26463
81
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:What error message are you getting?



Come on. If your goal is to become a Java programmer, you should know by now that the error messages are there to tell you something about the problem.
 
Clemonte Johnstone
Ranch Hand
Posts: 107
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Stephan van Hulst wrote:What error message are you getting?



Come on. If your goal is to become a Java programmer, you should know by now that the error messages are there to tell you something about the problem.



Apologies the code compiles. It was because of a duplicate class in my test suite. Its because I work very hard and have overlooked this yesterday. Sorry.
 
Clemonte Johnstone
Ranch Hand
Posts: 107
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Stephan van Hulst wrote:What error message are you getting?



Come on. If your goal is to become a Java programmer, you should know by now that the error messages are there to tell you something about the problem.



Its polymorphism as well right as X1 is instantiating X?
 
Stephan van Hulst
Saloon Keeper
Posts: 12811
278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Clemonte Johnstone wrote:Its polymorphism as well right as X1 is instantiating X?


Can you elaborate on your question? I don't know what you mean.
 
Clemonte Johnstone
Ranch Hand
Posts: 107
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:

Clemonte Johnstone wrote:Its polymorphism as well right as X1 is instantiating X?


Can you elaborate on your question? I don't know what you mean.



I have nothing more to add. What I have stated should be enough information.
 
Stephan van Hulst
Saloon Keeper
Posts: 12811
278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then I'm afraid I can't help you further.
 
Clemonte Johnstone
Ranch Hand
Posts: 107
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Then I'm afraid I can't help you further.



This is polymorphism right?


 
Master Rancher
Posts: 3829
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Clemonte Johnstone wrote:Its polymorphism as well right


Yes

Clemonte Johnstone wrote:as X1 is instantiating X?


No.  I suspect this is the part that was confusing Stephan.  (And me.)  We would not say that X1 instantiates X.  We would say that X implements X1 (and X2 and X3).  Instantiation is being done by the _39_Question_Intefaces_Inheritance_And_Implementation class in the main() method.

But yes, there's polymorphism here, since the declared type of obj is X1, and yet the actual specific type it references at runtime is an X.  And when you call print(), there are multiple implementations to choose from, but because the actual runtime type is X the call resolves to the most specific override, which is found in X3's print() method.
 
Clemonte Johnstone
Ranch Hand
Posts: 107
2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can we also not say that X is an instance of X1?
 
Marshal
Posts: 72423
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No. Types are not instances of other types. They are supertypes and subtypes.
 
It's a beautiful day in this neighborhood - Fred Rogers. Tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic