| Author |
how can we access a method from sub class from its super class?
|
kiran kumar
Greenhorn
Joined: May 24, 2008
Posts: 18
|
|
Hi, class A{ public void methodA(){} } class B extends A { public void methodA(){} } class C extends B { public void methodA(){} public static void main(String args[]){ // How can we call methodA in class A without creating instances how can we call. } }
|
Thanks & Regards,<br />Bhuvan Samrat.
|
 |
Paul Sturrock
Bartender
Joined: Apr 14, 2004
Posts: 10336
|
|
|
Sounds like a homework question. What do you think the answer is?
|
JavaRanch FAQ HowToAskQuestionsOnJavaRanch
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19232
|
|
|
This is really a beginner question, so that's where this thread is moving.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Ronald Schild
Ranch Hand
Joined: Jun 09, 2008
Posts: 117
|
|
|
Have a look at the 'super' keyword, you may find what you're looking for.
|
Java hobbyist.
|
 |
haripriya vedula
Greenhorn
Joined: May 31, 2008
Posts: 24
|
|
|
A subclass can call a constructor method defined by its superclass using keyword super..
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32833
|
|
Do you really mean to access a subclass member from the superclass? You can't. Simple. Imagine you have a "sub" keyword. Now, how could you write code in class A which could reach the method in a subclass with sub.method(). How can the compiler tell whether there are any subclasses at all, or whether you mean B or C? You could possibly write in class A, A b = new B(); but I am sure that's not what you mean.
|
 |
Ilja Preuss
author
Sheriff
Joined: Jul 11, 2001
Posts: 14112
|
|
|
Another interpretation would be that you *can* - through polymorphism. If the superclass declares the method, and the subclass implements/overrides it, the superclass can call it. (An example for this is the Template Method design pattern.)
|
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
|
 |
Peter Chase
Ranch Hand
Joined: Oct 30, 2001
Posts: 1970
|
|
|
In some circumstances, you could cast "this" to the subclass, if you know that the current object is really an instance of a particular subclass. However, this is very bad programming practice; a superclass should never know about its subclasses.
|
Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.<br /> <br />#:^P
|
 |
Mohamed Manas
Greenhorn
Joined: Jun 02, 2008
Posts: 24
|
|
Originally posted by rahul dravid: class C extends B { public void methodA(){} public static void main(String args[]){ // How can we call methodA in class A without creating instances how can we call. } }
I think Rahul wants to access methodA in classA from classC. In this case although we can't directly call a overridden method from a super class, following trick will do the job. I hope this helps.
|
It is better to make mistakes now and on purpose than later and accidentally...
|
 |
 |
|
|
subject: how can we access a method from sub class from its super class?
|
|
|