This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Call a method in super's super class??

 
sivakumar k r
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Folks,

1) Class A is the Super class and B extends from A
2) Class C extends B
3) The method display has been over-ridden in all the classes.
4) From the method in class C, I wish to call that of B & A.
5) I am able to call that of B using super.

Question: Is there a way to call A's display from C's display directly???

 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sivakumar k r:
Is there a way to call A's display from C's display directly


No.
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
class C extends B {
public void display() {
new A().display(); // call like this
System.out.println("Display of Class C called");
super.display(); // calls B's Display
}}
}
}


bye for now
sat
 
sivakumar k r
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Class C = A + B + delta C.
I wanted to call C's --> A's display.

I didn't want to call display by simply create an object of A and calling the method from that newly created object within C.
 
Henry Wong
author
Marshal
Pie
Posts: 21115
78
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sivakumar k r:
I didn't want to call display by simply create an object of A and calling the method from that newly created object within C.


Unfortunately, the answer is still "no".

Think of it like this. You are the designer of class C, which inherits from B. Your class overrides B, and implements new functionality. But one of the reasons why you override certain methods is that some of the methods of B, are no longer valid, for your C class -- you need those objects overriden, or your class will not function correctly.

Now... do you really want some other developer to create a D class which subclasses your C class, and bypasses the method calls that you overriden?

Henry
[ February 08, 2007: Message edited by: Henry Wong ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't say that it's "unfortunate".

The whole desire to call a "super's super method" is an indication that you should rethink your design. There probably is an abstraction missing or something that would obliterate that need.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic