aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes abstract Classes 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 Classes" Watch "abstract Classes" New topic
Author

abstract Classes

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
I do not think any following selection is correct.
But answer is (c). I don't get it. Can anyone help me?
Thanks.
Which one of the following class definitions is a legal definition of a class that cannot be instantitated?
Select the one right answer.
(a) class Ghost
{
abstract void haunt()
}
(b) abstract class Ghost
{
void haunt();
}
(c) abstract class Ghost
{
void haunt() {};
}
(d) abstract Ghost
{
abstract void haunt();
}
(e) static class Ghost
{
abstract haunt();
}
my explanation as follows.
(a) is not correct because of no semicolon.
(b) you have to use "abstract" keyword to define a prototype
for your abstract method in a abstract class.
(c) you can not use semicolon to end the instance method in
your class after your curly bracket.
(d) no class keyword.
(e) no return type.
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
(c) you can not use semicolon to end the instance method in
your class after your curly bracket.
- Apparently not. It is okay to end an instance method with a ;
I tried the following and it compiles.
class test
{
void haunt() {
System.out.println("do nothing");
};
}
Another thing we have to remember ..........
Chris Cleverley
Greenhorn

Joined: Mar 09, 2007
Posts: 22
(C) is correct. Compile it and see, it compiles fine with no error.

BTW who said that you cannot have a semicolon at the end of instance method after the curly brackets.
Chris
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
a) class Ghost { abstract void haunt() }
a is wrong as the class Ghost has to be abstract if it declares a abstract method. Also the colen is missing which may be a typo mistake
(b) abstract class Ghost { void haunt(); }
b is wrong as one must declare b as either of
abstract class Ghost {
abstract void haunt();
}
or abstract class Ghost{
void haunt(){}
}
thus the method haunt must have a body or be declared abstract
(c) abstract class Ghost {
void haunt() {};
}
try compiling this it works
even this works
class x{
void haunt(){ }
};

(d) abstract Ghost {
abstract void haunt();
}
d is wrong as there is no class keyword for Ghost
(e) static class Ghost
{
abstract haunt();
}
e is wrong as
a top level class cannot be static
the method haunt does not have a return type
and the class should be abstract
since only c works c is the obvious choice
Regds.
Rahul
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hi, you all,
Thanks lot
 
 
subject: abstract Classes