• Post Reply Bookmark Topic Watch Topic
  • New Topic

how can we access a method from sub class from its super class?  RSS feed

 
kiran kumar
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

}

}
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sounds like a homework question. What do you think the answer is?
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is really a beginner question, so that's where this thread is moving.
 
Ronald Schild
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at the 'super' keyword, you may find what you're looking for.
 
haripriya vedula
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A subclass can call a constructor method defined by its superclass using keyword super..
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.)
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Mohamed Manas
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!