Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abstract class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "abstract class" Watch "abstract class" New topic

abstract class

Rahul Ramachandran
Ranch Hand

Joined: Feb 27, 2001
Posts: 48
Here's a simple one from Mughal.
Which of these two is a legal definition of a class that cannot be instantiated?
abstract class Ghost
void haunt();
abstract class Ghost
void haunt(){};

The answer given is 2.
Paul Anilprem
Enthuware Software Support
Ranch Hand

Joined: Sep 23, 2000
Posts: 3635
void haunt(); : If you do not provide a body then you must declare it abstract.
Get Certified, Guaranteed!
(Now Revised for the new Pattern)

Your guide to SCJD exam!
Try out the world's only WebCompiler!

Enthuware - Best Mock Exams and Questions for Oracle Java Certifications
Quality Guaranteed - Pass or Full Refund!
nitin sharma
Ranch Hand

Joined: Feb 24, 2001
Posts: 290
In your second case u have decalred an abstract class comtaining a non abstract method.That is perfectly fine.
sona gold
Ranch Hand

Joined: Feb 14, 2001
Posts: 234
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.

sona<br />SCJP
rajashree ghatak
Ranch Hand

Joined: Mar 10, 2001
Posts: 151
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.
sona gold
Ranch Hand

Joined: Feb 14, 2001
Posts: 234
thanks rajashree
u got a point here
i missed it
yes so the 2nd option is the right answer
I agree. Here's the link:
subject: abstract class
It's not a secret anymore!