This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Invoking abstract method

 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question from Enthuwares JQPlus


answer is : It'll print "calculating",
[ January 14, 2007: Message edited by: ramya sri ]
 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
After printing "calculating" it is throwing NullPointerException.
 
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Firtsly, You cannot make OBJECT of a class which is abstract, you can make only reference of that.

When you create reference of calculator and try to call method on that , but at that time reference which you created is NULL , so it will through NullPointerException after printing calculating
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, it will print "calculating" - and then throw a NullPointerException. Have you not compiled and run the program? Obviously you have to extend this class and provide an implementation of the calculate() method.

Because you do not say what the "it" is that does not provide any explanation we can only move on to other things.

Always provide the complete question and answer and a reference to it.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And ram is correct - you have to replace that null with an instance of the extended Calculator class.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hai,

Actually Abstract classes are not allowed to instantiate.

Here in this ex: we have just created a reference of type abstract class and using that reference we have invoked the abstract method call(or any non-abstract method in abstract class).

so while compiling compiler checks only if that absract class is instantiated if so,will throw an error at compile time,if used the reference of it to acces methods in it,compiler gets satisfied as we didnt create object of it,
but the runtime will throw an error as there is no object created for it and we are accessing method by null...so JVM throws nullpointer exception.

Harish...
 
Ramya Chowdary
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understood ,thanks Barry Gaunt and Ram
 
reply
    Bookmark Topic Watch Topic
  • New Topic