• 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

Question on Declarations and Modifiers

 
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What I understand is that if you see an abstract method in a class, then that class must be declared abstract.
end of story.
 
Ranch Hand
Posts: 443
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This would compile!
[ June 19, 2003: Message edited by: Alton Hernandez ]
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic