• Post Reply Bookmark Topic Watch Topic
  • New Topic

abstract child class method in Parent abstract class  RSS feed

 
kri shan
Ranch Hand
Posts: 1489
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How to call abstract child class method in Parent abstract class ?


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kri shan wrote:How to call abstract child class method in Parent abstract class ?


Basically, you need the reference to be of the type to allow the call to the method. So, in this case, you need to check the this reference, to confirm that it is a B class type, and cast the reference, to call the method.

Henry
 
Junilu Lacar
Sheriff
Posts: 11494
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kri shan wrote:How to call abstract child class method in Parent abstract class ?


1. Given just this code, you won't be able to make any calls to anything. Both classes are declared as abstract so while there is a potential to make polymorphic calls later on in a concrete class, the code you just show will not do anything because you can't instantiate abstract classes.

2. Be careful what you wish for. A superclass should not have any logic in it that demonstrate any knowledge of a specific subclass.  To do so would violate object-orientation rules and it results in very brittle code.  If you were designing that call to be polymorphic, then that's fine. It's like saying "I want to have a method, blah(), that should behave in a certain general way but I will leave it to whatever subclass that extends me to decide what the specifics are."  If, on the other hand, you design that method call you're making from the superclass to the subclass method as something like, "I know my subclass ClassB is going to inherit and implement my blah() method, let call that one instead" then this is what I like to call "Superclass having inappropriate knowledge of its subclass" -- if you like to use the names Parent/Child instead, you can think of it as "Parent class having incestuous relations with its Child class." Yes, it's that gross of a design.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!