It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anyhting like super.super? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anyhting like super.super?" Watch "Anyhting like super.super?" New topic
Author

Anyhting like super.super?

Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Consider the following code.

Problem :- How to access the Top class version instance variable name ? In the below code , how to refer Animal class variable name in Dog class.

In dog class , if we say name then it would imply name in Dog class.
In dog class , if we say super.name then it would imply name in Pets class.

<<How to say name variable in Animal class ??? >> super.super.name does not work !!! : (

Thanks !!!



SCJP 6.0 96%

(Connecting the Dots ....)
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

Try.... System.out.println(((Animal) d).name);

As for the getName() method of the Animal class, that's not accessible externally via a Dog reference.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Henry Wong wrote:Try.... System.out.println(((Animal) d).name);

As for the getName() method of the Animal class, that's not accessible externally via a Dog reference.

Henry


Animal(d).name would not work, How come Dog class knows anything about its references until we pass it.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

Sahil Kapoor wrote:
Animal(d).name would not work, How come Dog class knows anything about its references until we pass it.


Why won't it work? That is how you get to a hidden (by the subclass) variable.

Henry
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4341
    
    7

Sahil Kapoor wrote:Animal(d).name would not work

Check the syntax. That's not what Henry wrote.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Henry is correct.. It'll work. There is no virtual invocation on variables. Reference variables are invokes on reference type..



|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 189

I think the question is ...
Accessing Animal class variable in Dog class without using instances. Only with using super.

In the Dog class super.name returns the variable from Pets.
If i want variable from Animal in the Dog class? Anything like super.super.name??


SCJP 5, SCWCD 5
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Sahil Kapoor wrote:Consider the following code.
Problem :- How to access the Top class version instance variable name ? In the below code , how to refer Animal class variable name in Dog class.


I think, he asked how to call a variable in Animal class, not about using super.super stuff. And, I think, super.super won't work.....
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Hey guys

What ''Prabhakar Bokka '' is telling is right !!!
I am asking exactly what he thinks , Okay let me re-write the cede and comment out the place where i want something to be done.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

It actually very easy to figure it out from the answers provided in this topic... but since you seem to be stuck on using "super", you may not see it...



Henry
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

For your case (the Pet class doesn't have getName() method), try this.
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Thanks Henry !!!
You catch my Mind !!! I think i should stop studying today !!!
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
@ Abimaran Kugathasan Dear you program made me to think more and now i am confused in the output !!!

Consider the following :-





The above code prints Animal, Dont you think it should print Dog.

Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18507
    
  40

Sahil Kapoor wrote:
The above code prints Animal, Dont you think it should print Dog.



Basically, while the Dog class inherits the Animal class' method. It is still a method of the Animal class -- meaning the code doesn't change. The Dog class simply calls the method of the Animal class, it doesn't get a copy of it.

Instance variable do not support polymorphism. So, the method of the Animal class will determine how to access instance variables at compile time. And at compile time, while compiling the Animal class, it is determined that you are accessing the variable of the Animal class.

Henry
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

Sahil Kapoor wrote:@ Abimaran Kugathasan Dear you program made me to think more and now i am confused in the output !!!


Henry explained it clearly....
Sahil Kapoor
Ranch Hand

Joined: Sep 12, 2009
Posts: 316
Thanks a lot for Excellent Explanation Henry Wong !!!




Prabhakar Reddy Bokka
Ranch Hand

Joined: Jul 26, 2005
Posts: 189

Thanks Henry. Thats really fantastic.
Alpesh Padra
Ranch Hand

Joined: Jan 10, 2010
Posts: 41
package com.text;
class TestInheritance {


public static void main(String... args) {

Dog d = new Dog();

System.out.println(d.name);
System.out.println(d.getName());

}
}

class Animal { public String name="Animal"; public String getName(){return name;}}

class Pets extends Animal{ String name="Pets";}

class Dog extends Pets{
String name="Dog";
public String getName(){return super.getName();}
}


your pets class does not have getName() method. Its going to search method name in parent of pets means Animal.

Try this code it works.
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Folks,

If I understood you right, you are kinda getting confused with the term 'Inheritance'. If you look at the term 'Inheritance' and its features of an OOP language, yes it facitates a sub/child class to have access to the mebers of a super/parent class. Remember: It is to the immediate or direct parent class only and NOT any of them up in the hierarchy.

If still you want to a way, you can follow the other bartenders suggestion by typecasting the reference variable to that of the parent class which you are interested in!

Hope this helps!


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Lalit Mehra
Ranch Hand

Joined: Jun 08, 2010
Posts: 384

// Inheritance Test ...



// as the JVM checks for compatibility at the compile time only
// a superclass reference can only access its own instance variables , as it does not know anything about the instance variables of its subclasses ... i mean they aren't defined to him .. compile time polymorphism
// but a superclass reference uses the methods of the subclass it is referring to (obviosly if they are overridden) using the runtime polymorphism
// remember that the instace variables do not take part in run time polymorphism


http://plainoldjavaobject.blogspot.in
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Anyhting like super.super?
 
Similar Threads
Polymorphism
Casting reference types.
instanceOf operator
Shadowed static variable
Inherited methods works on hidden instance variable while ....