• Post Reply Bookmark Topic Watch Topic
  • New Topic

inheriting an abstract class  RSS feed

 
Grant Saludo
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can I get some help, I am new and I can't figure out how to inherit an abstract class with sub classes




 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

You did all correct. Keyword 'extends' does that. What else you need to do, is to implement all the abstract methods specified in your Person class in an extended Student class.
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome again

I think you have not quite understood what abstract means. It means that the class cannot be instantiated in its present form, and indeed must have subclasses. But it does not necessarily mean that all methods must be abstract. You shou‍ld consider which methods will be the same for all subclasses (or the same for nearly all), and which methods will be different for all subclasses (or different for nearly all). Implement all those methods which will be the same and don't mark them abstract. I think you shou‍ld have all those fields as private, and the setXXX and getXXX methods shou‍ld not be abstract.That is a gross oversimplification. The moveAbout method cannot be implemented in Animal because it is different in every subclass. So I marked it abstract. It is now possible to implement it differently in Fish Bird or Mammal. You can add other subclasses; in Mollusc it would call slide() and in Insect it might call walk() or fly(), so maybe in Insect it would still be abstract.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!