Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

super keyword

 
shruthi gowdas
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I wanted to know how to access the methods of grandparent class from the child class (when those methods in child class are overridden) using super keyword. Please help.
 
Eduardo Lomonaco
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



This code produces the output This is FatherClass.
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you can only go "up" one level.
It's impossible for class Child to call the foo() implementation of class GrandFather.
 
Eduardo Lomonaco
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right
PS: Father class extends GrandFather class
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, of course
 
shruthi gowdas
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eduardo Lomonaco wrote:Right
PS: Father class extends GrandFather class


Thanks guys, But are you sure I cant manipulate anything with super to get the methods of class? Sorry, but i wish there was some answer to that.
 
Eduardo Lomonaco
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Sruthi,

I'm no so sure what do you mean by "are you sure I cant manipulate anything with super to get the methods of class"

But using Java reflection API you do can get the methods of the superclass or even instantiate the superclass.

This API can be a lil complicated for starters so I would suggest you to check what this returns and then do some research on Reflection API:

 
shruthi gowdas
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Eduardo Lomonaco wrote:Hey Sruthi,

But using Java reflection API you do can get the methods of the superclass or even instantiate the superclass.

This API can be a lil complicated for starters so I would suggest you to check what this returns and then do some research on Reflection API:



Thank you
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch Eduardo Lomonaco, but it’s not “lil” please.

Using names like Father for classes is misleading because biological inheritance and computing inheritance are very different.
If you try to use super-to-superclass code, you are violating the rule that a subclass IS-A superclass.
 
Eduardo Lomonaco
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, sorry! And thank you for the tips.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic