• Post Reply Bookmark Topic Watch Topic
  • New Topic

what are the different ways of calling super class members  RSS feed

 
Sumalatha Durugappa
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I want to know what are the other ways of printing Tree class' x value in the below code example. Please provide the solution.





 
Louis Bros
Ranch Hand
Posts: 54
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, you can call a parent's method with 'super.method()' but you can't call a parent's parent's method directly. You could implement a super call in the parent class?
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you declaring x again in the subclasses?
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sumalatha Durugappa wrote:I want to know what are the other ways of printing Tree class' x value in the below code example. Please provide the solution.

1. I don't understand the question.
2. We don't 'provide solutions'. Please read the HowToAskQuestionsOnJavaRanch (←click) page - or indeed HowToAnswerQuestionsOnJavaRanch - for details.

Winston
 
Javin Paul
Ranch Hand
Posts: 297
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sumalatha Durugappa wrote:Hi,
I want to know what are the other ways of printing Tree class' x value in the below code example. Please provide the solution.


class Tree{
int x=5;
}

class Branch1 extends Tree{
int x=10;
}
public class Branch2 extends Branch1{
int x=15;
public static void main(String[] args) {
Tree t= new Tree();
System.out.println(t.x);
//what are the other ways to print Tree class' x
}

}


Since field x is hidden in Branch1 and Branch2 I am not sure if there is way to access it outside the class other than Reflection, inside the Branch1 you can use super keyword to get value of x.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Javin Paul wrote:
Since field x is hidden in Branch1 and Branch2 I am not sure if there is way to access it outside the class other than Reflection, inside the Branch1 you can use super keyword to get value of x.


The reference is pointing to an instance of Tree. It is *not* an instance of Branch1 or Branch2, so there isn't any more "values of x" to access.

Henry
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Javin Paul wrote:
Sumalatha Durugappa wrote:Hi...
Since field x is hidden...

Javin, just a point to save our CTS on the scroll button.

You don't need to include the entire quote in your reply. A sentence or two is usually enough especially if the original post contains code.

Winston
 
Prasad prap
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
implement toString method in Tree class such as



Now call

Tree tree = new Tree(20);

System.out.print(tree); // will print X=20
 
Campbell Ritchie
Marshal
Posts: 56521
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
I have used the code button on your post, and you can see how much better it looks
You will doubtless have heard about “IS‑A” relationships, so attempting to use members of the superclass” superclass means your class is working outside the “IS‑A” relationship. I presume you simply chose those names at random, because a branch is not a tree.
Daft example follows
If you had classes called Animal, Mammal, Dog, you might have reproduce() methods like this, remembering that the default means of reproduction in just about every sort of animal is to lay eggs:It is a good thing that won’t compile, unless you want a dog which lays eggs!
Remember that fields are not overridden. The only thing you can override is an instance method.
You might be able to gain access to the field in the Tree class by casting, like this: System.out.println(((Tree)this).x);
 
Sumalatha Durugappa
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Thanks to all for your valuable inputs. Between, I had been asked this question by an interviewer!!
 
Campbell Ritchie
Marshal
Posts: 56521
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why didn’t you tell us it was an interview question? Interview questions are not about “real” things, but about how you approach an awkward problem.

And … you’re welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!