• 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Jj Roberts
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Himai Minh
  • Carey Brown
  • salvin francis

Accessing an overridden method

 
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good Day everyone and congratulations again for this very nice platform/tool that you set up.
I am a Newbie in Java and I am going now through the process of studying and learning "Working with Inheritance".
My goal today is to understand how to acess an overridden method.
If is not an issue I will copy and paste here a portion of code from OCA/OCP JAVA SE 8 Practice Test (Selikoff-Boarsky )-Chapter 7/Question 7.
The question works fine , I understood it completely.
Congratulations to the Authors, I am really getting benefit from using the combined set Study Guide/Practice Test.


Furthermore, the same question, it triggers me "an additional topic/doubt " about Java features that I would really like to understand.






The code outputs  Driving Electric Car.
Everything fine and clear!

I , ON MY OWN, would like to acess to the drive() method defined in   Car  class.
My question : How could I do it?

I "naively" solved in this way:



The point is that I would like to use the car reference variable initialized with the code fragment:


I, as newbie, have the impression , that this is not possible since drive() instance method  in ElectricCar class overrides drive() instance method  in parent class Car.

Does anyone has idea how to access the overridden method through car reference variable?
Is there any other way how to get to the "overridden" method keeping drive() method non static?

thank you,

regards







 
Saloon Keeper
Posts: 12481
269
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ruggero Montesi wrote:Does anyone has idea how to access the overridden method through car reference variable?


No, it's not possible, as long as the actual type of the object that is referenced by the variable is of type ElectricCar. This is a good thing: if you could somehow get the overridden method definition, Java would be a very confusing place.

Is there any other way how to get to the "overridden" method keeping drive() method non static?


The only way to access an overridden method definition is from a direct subclass, through the super keyword:
 
Ruggero Montesi
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now clear!
Thank you very much Stephan van Hulst !!!
 
Marshal
Posts: 70998
291
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...and the super keyword only works once. Yoiu cannot writeIf you could get super.super.xyz to compile, you would now create a situation where an ElectricCar “ISN'T‑A” Car.
 
Ruggero Montesi
Greenhorn
Posts: 14
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, sorry for my late!
I wished to thank Campbell Ritchie for the additional and very interesting insight of the (hypothetical  ) use of "super.super".

If I understood correctly , the use of ""super.super" would "get" us into the superclass of  Car, i.e. = Automobile so not anymore something within Car class, like clearly stated by Campbell Ritchie.

Thanks again
 
Marshal
Posts: 26092
71
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Indeed. The superclass of Car could even be Object, in which case it's clear that super.super.drive() is nonsensical. There's no drive() method in Object.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic