• 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

How to call a non-static non-abstract method in an abstract class from a static context?

 
Ranch Hand
Posts: 33
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I know that abstract classes cannot be instantiated. I also know that this code is perfectly legal.


and produces the output 5. But what should be done to call twoo() (if it is not static) from main()(static ofcourse). We obviously cannot use an object because abstract classes cannot be instantiated and also not do Twooo.twoo() because a non static method cannot be called from a static context. Is there a way to do that? If so how?
 
Sheriff
Posts: 3063
12
Mac IntelliJ IDE Python VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abstract classes cannot be directly instantiated, but concrete classes that extend them can be. So if twoo() was not static, it could still be called through instances of a subclass. There is no way to call a non-static method through a static context, abstract or no.
 
Tarun Mohandas
Ranch Hand
Posts: 33
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, only through instances of sub-class a concrete method of an abstract class can be called. Ok thank you so much
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic