• 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
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

abstract child class method in Parent abstract class

 
Ranch Hand
Posts: 1491
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How to call abstract child class method in Parent abstract class ?


 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 17734
302
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic