• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can an abstract class have a main method?

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, as I'm doing various mock exams, I came across this question:

The solution says that it will print "calculating" and then throw a NullPointerException, which makes sense, if it wasn't for the fact that it's an abstract class. So, we know that abstract classes can't be instantiated directly (and here we seem to have an object x of type Calculator which I thought it wasn't allowed) and we also have a main method. Is all this allowed?!
 
Dave Tolls
Ranch Hand
Posts: 2091
15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can have an abstract class with a main() method.
main() is a static method so does not require an instance of its containing class to execute.

As for the variable 'x', that is not an instance of Calculator.  It is a reference variable which, in this case, is referencing null.
As with the main() method, there is no Calculator instance at this point.
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK,thanks for clarifying that.
 
timothy adigun
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:Hi guys, as I'm doing various mock exams, I came across this question:

The solution says that it will print "calculating" and then throw a NullPointerException, which makes sense, if it wasn't for the fact that it's an abstract class. So, we know that abstract classes can't be instantiated directly (and here we seem to have an object x of type Calculator which I thought it wasn't allowed) and we also have a main method. Is all this allowed?!


Yes, it is "possible" as you have demonstrated here. But it really doesn't make sense at all [Please I don't mean this in an insulting way to the OP].
Why?
Like the OP rightly stated that abstract class can't be instantiated, and more over they are meant to "hold" one or more abstract methods.
Then why would one want to then make a "point" of entry in such class?

Again, like the OP is doing in his/her code, if modifier abstract and static can ONLY be combined, which is an illegal combination of modifiers
then one can say maybe (just maybe), one can call the function calculate in the main function. But would NOT WORK! Either.

if the abstract modifier is then removed from the function calculate, then that function will have to be fully defined and then the class will also have to lose her abstract modifier.
So, IMHO it really don't make sense to do so even when it is possible.
 
Paul Clapham
Sheriff
Posts: 21559
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure, it doesn't make sense. But here we are talking about a question from a certification exam, so there is no requirement for the code to make sense. On the contrary, the code, rubbish as it is, serves to illustrate a particular point.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic