• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

what are the different ways of calling super class members

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.





 
Ranch Hand
Posts: 54
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you declaring x again in the subclasses?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23919
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 75724
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 75724
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ruth Stout was famous for gardening naked. Just like this tiny ad:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic