aspose file tools*
The moose likes Beginning Java and the fly likes How to call a non-static non-abstract method in an abstract class from a static context? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to call a non-static non-abstract method in an abstract class from a static context?" Watch "How to call a non-static non-abstract method in an abstract class from a static context?" New topic
Author

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

Tarun Mohandas
Ranch Hand

Joined: Feb 12, 2013
Posts: 33

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?
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2771
    
  10

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

Joined: Feb 12, 2013
Posts: 33

So, only through instances of sub-class a concrete method of an abstract class can be called. Ok thank you so much
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to call a non-static non-abstract method in an abstract class from a static context?
 
Similar Threads
lil explanation requied ..
abstract
abstract class instantiation
Why a Static method cannot refer to an instance variable?
Dan's single topic-anonymous classes.