Hi, Given classes A,B and C where B extends A and C extends B and where all classes implement the instance method void doIt().How can the doIt() method in A be called from an instance method in C? Regards Sunita
thomas
Ranch Hand
Joined: May 26, 2002
Posts: 79
posted
0
I don't think you can, except through an instance of class A. In particular, note that the following is NOT legal. super.super.method(); // not legal
satyen sanghavi
Greenhorn
Joined: Mar 19, 2008
Posts: 8
posted
0
u can declare new method in B say bmethod from this u can call super.method() and from C u can call super.bmethod()
latha
Greenhorn
Joined: Jul 17, 2002
Posts: 14
posted
0
Make the method doit() as static in all classes A,B& C.In doit() of C, call the doit() method with the A object.ie A Aobj = new A(); Aobj.doit(); I think this will work. Anybody can correct me,if I am wrong.
maha anna
Ranch Hand
Joined: Jan 31, 2000
Posts: 1467
posted
0
Yes Latha. Your method is valid. But you are deferring from your original post. In your original post you said doit() is an instance method. Because of that (I think ) thomas and satyen started thinking it is not directly possible. If you declate the doit() as static method, then you can very well call by just using the className itself. Like A.doit();. OR as you said through an instance of the class A. Both ways are possible. regds maha anna
Sunita
Greenhorn
Joined: Jun 08, 2000
Posts: 28
posted
0
Hi maha Following is one of the example in Khalid book,Could you please explain the stmt 1.How we can access the getbill() method and also could u please explain me the functionality of "this" in this method. Regards Priya class light{ protected String billtype ="small bill"; protected double getbill(int noofhrs) { double smallamount=10.0; double smallbill=smallamount*noofhrs; System.out.println(billtype + ":" +smallbill); return smallbill; } public void banner(){ System.out.println("Let there be light"); } } class tubelight extends light{ public String billtype="large bill"; public double getbill(final int noofhrs) { double largeamount=100.0; double largebill=largeamount*noofhrs; System.out.println(billtype +":" +largebill); return largebill; } public double getbill() { System.out.println("no bill"); return 0.0; } } class neonlight extends tubelight{ public void demonstrate() { super.banner(); super.getbill(20); super.getbill(); System.out.println(super.billtype); ((light)this).getbill(20); // 1 System.out.println(((light)this).billtype); } } public class client{ public static void main(String args[]) { neonlight neonref=new neonlight(); neonref.demonstrate(); } }
daryl olson
Ranch Hand
Joined: Aug 15, 2000
Posts: 36
posted
0
Hi Sunita, If I may, let me try and explain. Here is the output from the example in question: Let there be light large bill:2000.0 no bill large bill large bill:2000.0 small bill
I believe what Khalid is trying to show in this example is, even though you perform a cast (ie "(light)this)" ) in order to try to invoke the method, "getbill(20)", in the ancestor, "light": ((light)this).getbill(20); // 1 the method that actually gets invoked is the one closest in the inheritence heirarchy to the ACTUAL TYPE of object making the call (ie "neonlight"). In this case class "tubelight" is closer in the inheritence heirarchy than "light", so it is invoked. However, when you attempt the same sort of cast (ie "(light)this)" ) in order to access the value in an instance variable, "billtype", what happens is that the cast is honored and the value from class "light" is accessed: System.out.println(((light)this).billtype); What gets printed by the above line is the value of "billtype" from the class "light" even though there is a variable "billtype" defined in a closer class, "tubelight". As to the question of the functionality of "this" in: ((light)this).getbill(20); // 1 Well, what he wants to do is cast the current object, "neonlight", to the class of "light" (Note: this is a valid cast as "neonlight" is a subclass of "light"). In order to cast the current object to class "light", he needs to use a reference to the current object (ie something has to be cast). Every object has any implict reference to itself through the "this" keyword. This is much the same as the keyword "super" is an implict reference to the super class of the current object. I hope this helps...
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.