• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Anyhting like super.super?

 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !!!

 
Henry Wong
author
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sahil Kapoor wrote:Animal(d).name would not work

Check the syntax. That's not what Henry wrote.
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry is correct.. It'll work. There is no virtual invocation on variables. Reference variables are invokes on reference type..

 
Prabhakar Reddy Bokka
Ranch Hand
Posts: 197
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Abimaran Kugathasan
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For your case (the Pet class doesn't have getName() method), try this.
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry !!!
You catch my Mind !!! I think i should stop studying today !!!
 
Sahil Kapoor
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot for Excellent Explanation Henry Wong !!!




 
Prabhakar Reddy Bokka
Ranch Hand
Posts: 197
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry. Thats really fantastic.
 
Alpesh Padra
Ranch Hand
Posts: 41
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Lalit Mehra
Ranch Hand
Posts: 384
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
// 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic