This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Abstract Class Problem

 
Piyush Jain
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Piyush,
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.
 
Tom Tolman
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Original class A extends Object which is concrete class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic