Last week, we had the author of TDD for a Shopping Website LiveProject. Friday at 11am Ranch time, Steven Solomon will be hosting a live TDD session just for us. See for the agenda and registration link
  • 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
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

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

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

}

}
 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sounds like a homework question. What do you think the answer is?
 
Sheriff
Posts: 22649
126
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is really a beginner question, so that's where this thread is moving.
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have a look at the 'super' keyword, you may find what you're looking for.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A subclass can call a constructor method defined by its superclass using keyword super..
 
Marshal
Posts: 75703
354
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic