Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract class

 
Rahul Ramachandran
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a simple one from Mughal.
Which of these two is a legal definition of a class that cannot be instantiated?
//1
abstract class Ghost
{
void haunt();
}
OR
//2
abstract class Ghost
{
void haunt(){};
}

The answer given is 2.
Why?
Thanks
Rahul
 
Paul Anilprem
Enthuware Software Support
Ranch Hand
Posts: 3711
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
void haunt(); : If you do not provide a body then you must declare it abstract.
------------------
Get Certified, Guaranteed!
(Now Revised for the new Pattern)
www.enthuware.com/jqplus

Your guide to SCJD exam!
www.enthuware.com/jdevplus
Try out the world's only WebCompiler!
www.jdiscuss.com
 
nitin sharma
Ranch Hand
Posts: 290
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In your second case u have decalred an abstract class comtaining a non abstract method.That is perfectly fine.
 
sona gold
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but the question here is which is the right declaration for a class that cannot be instantiated.
both the above shown classes are abstract and hence cannot be instantiated.
 
rajashree ghatak
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
no the first one is illegal declaration of an abstract class.The reason being "If you do not provide a body to a method then you must declare it abstract". Notice void haunt(); which doesnot have abstract keyword in its declaration.
Therefore only 2nd class declaration is valid.
rajashree.
 
sona gold
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks rajashree
u got a point here
i missed it
yes so the 2nd option is the right answer
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic