File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Abstract Class Problem 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 "Abstract Class Problem" Watch "Abstract Class Problem" New topic

Abstract Class Problem

Piyush Jain
Ranch Hand

Joined: Apr 25, 2003
Posts: 60
This is from Certification book by Kathy Sierra and Bert Bates. (chapter 2, Q3)

abstract class A{
abstract short m1();
short m2(){return (short) 420;}


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


wouldn't this statement be true in this case?

"if class A was not abstract and method m1() in class A was implemented, the code would not compile"

how can an abstract class extend a non abstract class? is it legal? the other question is since in this statement he didn't say anything about removing the abstract keyword, just saying implements is vague. so it should give a compiler error, if its abstract and implemented.
Jay Pawar
Ranch Hand

Joined: Aug 27, 2004
Posts: 411
I wrote modified the code you posted and was able to compile it successfully.

Looks like abstract class can extend concrete class.I do not know why anybody would want abstract class extended from concrete class.

Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
Tom Tolman
Ranch Hand

Joined: Sep 02, 2004
Posts: 83
You might consider extending an abstract class from a concrete class if you were adding some methods which you didn't want to define but would serve as a prototype for deriving classes.

Although I think it would be easier to declare an interface, and have these derived classes implement the interface instead.

An interface would not suffice if your derived abstract class needed to manipulate some of its own base class variables in some of its methods.
Vlado Zajac
Ranch Hand

Joined: Aug 03, 2004
Posts: 245
Original class A extends Object which is concrete class.
I agree. Here's the link:
subject: Abstract Class Problem
It's not a secret anymore!