• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Reg. abstract class and method

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Angela,
James is right, I'm sorry !

Val
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic