• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Access

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
u can declare new method in B say bmethod from this u can call
super.method()
and from C u can call super.bmethod()
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1467
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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();
}
}
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic