aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on Declarations and Modifiers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on Declarations and Modifiers" Watch "Question on Declarations and Modifiers" New topic
Author

Question on Declarations and Modifiers

M Huang
Greenhorn

Joined: Jun 18, 2003
Posts: 9
I have the Sun Certified Programmer and Developer for Java 2 Study Guide book by Kathy Sierra and Bert Bates. This question is from Chapter 2, #3.
Here's the question:
3. 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 key says that A, C, and E are correct and I agree. But isn't F also correct? Even if m1() were implemented, it is still declared as abstract, but since class A is not abstract, the code would not compile. Or am I missing something?
Thanks for anybody who can help!
shilpa ad
Greenhorn

Joined: Jun 08, 2003
Posts: 6
F. If class A was not abstract and method m1() on line 2 was implemented, the code would
not compile.
It is not true because if class a is not declared as abstract then it would not accept abstract
method in line 2 which would lead to compile error.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
F. If class A was not abstract and method m1() on line 2 was implemented, the code would not compile.
if you restate F as:
F. If class A was not abstract and method m1() on line 2 was implemented, the code would compile.
now F is true because class A is not abstract anymore and only abstract method is replaced by an implementation. So it should compile. There is no reason for it to NOT Compile. Hence original F statement is not true.
Thanks
Barkat
[ June 18, 2003: Message edited by: Barkat Mardhani ]
[ June 18, 2003: Message edited by: Barkat Mardhani ]
M Huang
Greenhorn

Joined: Jun 18, 2003
Posts: 9
It seems to me that the question is very poorly worded (or maybe I do not understand the question at all!). Anyways, choice F seems to be saying that the following code would NOT compile:
class A {
abstract short m1() {
// implementation here
}
short m2() { return (short) 420; }
}
abstract class B extends A {
// missing code
short m1() { return (short) 42; }
}
Which is true, but Barkat suggests that "implemented" means the abstract modifier is removed. However, the way it is phrased would not lead one to make such an assumption.
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561
What I understand is that if you see an abstract method in a class, then that class must be declared abstract.
end of story.


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Alton Hernandez
Ranch Hand

Joined: May 30, 2003
Posts: 443

This would compile!
[ June 19, 2003: Message edited by: Alton Hernandez ]
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
abstract short m1() {
// implementation here
}

This is a compiler error.
This is a compiler error because an abstract method can not have a body. So K&B we say that a method is implemented I guess they mean something like this.
Brian Joseph
Ranch Hand

Joined: May 16, 2003
Posts: 160
I don't like that question. I know the rules for abstract classes and extending them, but somehow I couldn't really follow any of the choices, other than A and E, but C is a little funky.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on Declarations and Modifiers
 
Similar Threads
A question from K & B book
K&B book Chapter 2 Self Test Question No.3
abstract method INFO Plz?
RE:Question from K&B book
Question#3 in K&B book question