File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes K&B book Chapter 2 Self Test Question No.3 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 "K&B book Chapter 2 Self Test Question No.3" Watch "K&B book Chapter 2 Self Test Question No.3" New topic
Author

K&B book Chapter 2 Self Test Question No.3

Niranjan Deshpande
Ranch Hand

Joined: Oct 16, 2005
Posts: 1277
Given the following,
1. abstract class A {
2. abstract short m1() ;
3. short m2() { return (short) 420; }
4. }
5.
6. abstract class B extends A {
7. // missing code ?
8. short m1() { return (short) 42; }
9. }

which three of the following statements are true?(Choose three)

A. The code will compile with no changes.
B. Class B must either make an abstract declaration of method m2() or implement
method m2() to allow the code to compile.
C. It is legal, but not required, for class B to either make an abstract declaration of method
m2()or implement method m2() for the code to compile.
D. As long as line 8 exists, class A must declare method m1() in some way.
E. If line 6 were replaced with �class B extends A {� the code would compile.
F. If class A was not abstract and method m1() on line 2 was implemented, the
code would not compile.

The answer is A,C,E. I agree with A and E. But the option C is somewhat weird. It talks of method m2( ) whereas it should talk of m1 ( ) (as per my view).Also, m2( ) is already declared and defined in class in class A, how come it can be re-declared in class B (as option C says). Please explain

Thanks in advance�


SCJP 1.4 - 95% [ My Story ] - SCWCD 1.4 - 91% [ My Story ]
Performance is a compulsion, not a option, if my existence is to be justified.
Mani vannan
Ranch Hand

Joined: Aug 21, 2004
Posts: 185
Hi Niranjan,
method m2() in class A has already been implemented. So, leave it. B implements m1() alone.
So, we can infer the followings:
[1] The code is perfect and no changes required further. (This is what option A says).
[2]Class B needs not to be abstract since, it implements m1() (This is what option C says).
[3] Once again: class B needs not to be abstract (This is what option E says).


Manivannan
Cheenu Subramanian
Ranch Hand

Joined: Aug 15, 2005
Posts: 40
Hi Mani,
Option C also implies that m2() can be overridden to be abstract.isnt it?

So can a concrete method be overridden to be abstract?
Mani vannan
Ranch Hand

Joined: Aug 21, 2004
Posts: 185
Hi cheenu,
Since m2() already implemented, it can be (or) can not be implemented again.
But, if it is made abstract, then the calss itself should be made abstract.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

Any method can be overridden unless final, and the overriding method can be abstract (tested this with toString for another question around here).
This does require class B to be abstract, but guess what: IT IS.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Krishna Latha Grandhi
Ranch Hand

Joined: Nov 08, 2005
Posts: 110
c. for class B to either make an abstract declaration of method m2()or implement method m2() for the code to compile.


Hi friends,

I think there is no need to make abstract declaration as well as implementation.

Is it right.
help plz..

Thanks
Regards
Hari Krishna
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi...


//implementation of m2

abstract class B extends A {
short m2() {
return (short) 3;
}
short m1() { return (short) 42; }
}

//m2 as abstract

abstract class B extends A {
abstract short m2();
short m1() { return (short) 42; }
}


//without m2()

abstract class B extends A {

short m1() { return (short) 42; }
}


All the above 3 cases compile....

So the code first posted in the thread is legal....

which essentially means

it is not required,

for class B to either make an abstract declaration of method
m2()or implement method m2() for the code to compile


Regards
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19692
    
  20

It is not required no. But it is perfectly legal. And that is exactly what C says.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: K&B book Chapter 2 Self Test Question No.3