• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reg. abstract class and method

 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Following is from JavaCaps mock exam #2

The correct answer given is C.
But i think it should be A, C because we get compiler
error for both the class declaration adn the method Test().
 
Mariusz Szurnacki
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> But i think it should be A, C
Hi!!!
I agree. if we have an abstract method in the class,
then also the class must be abstract.
So we have to remove abstract modifier from method or add abstract modifier to the class. In this way, correct answers are
A i C.
Have a nice day,
Mariusz
 
Ravindra Mohan
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah Ranchers,
The answer is A and C. A class with abstract method MUST be
declared as "abstract". An abstract method/ native method CAN NOT
have a body /definition.
I guess this clears this issue.
Ravindra Mohan.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I think the right answer is still only C, since the method Test has a body ! If you just remove the abstract modifier from the method declaration of Test it compiler fine. BUT if you add the abstract modifier to the class declaration it won't compile !!
Anyone ?
Val
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If without considering the assumptions, i compile
the code, it will give both the class error as well as
the method error. So i still think it is A,C

To make the code compile we can do "either" of the below:
1.Remove the method body for Test() and declare the class as abstract.
2. Remove the abstract keyword from Test() method.
Correct me if wrong ..
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Angela,
First, the only error I get by compiling the code is about the body of the abstract Test method, but nothing about the class declaration.
I agree with you BUT, in the proposed answers there is no "In the body of the Test method" !!! The only thing about the Test method is about its declaration so considering the given answers, the only correct answer is C, because if you remove the abstract modifier in the declaration of the method Test, it will correct the error given by the compiler !!!
Val
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get the below two errors :
MyTest.java:1: MyTest should be declared abstract; it does not define Test() in MyTest
public class MyTest {
^
MyTest.java:8: abstract methods cannot have a body
public abstract void Test() {
^
2 errors

So acc. to me the answers should be A,C
 
james hoskins
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i think this discussion is more about poor question writing than code. both answers could be seen as correct depending on your viewpoint. everyone picked up on the need for an abstract class declaration and removal of method body, ambiguous questions can drive you mad.
kind regards,
james.
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Angela,
James is right, I'm sorry !

Val
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic