• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

abstract Classes

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, you all,
Thanks lot
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic