This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer Study Guide: Exam 1Z0-830 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
  • 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

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.
 
All that thinking. Doesn't it hurt? What do you think about this tiny ad?
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic