• 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

Abstract Class Problem

 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Original class A extends Object which is concrete class.
reply
    Bookmark Topic Watch Topic
  • New Topic