web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

How to call methods from subclasses when we are defining the parent class

(0) ShareShare
ReportReport
Posted on by 1,552
Hi,

I'm getting an error at base.getField2 -- i know that base is returning to me subclassA  -- but how can i call the method of child based on base class.
getField2 doesn't have to exist in base class
        enum = enum::A        BaseClass base = BaseClass::newFromEnum(enum);        base.initFields(_line);        Table1.Field2 = base.getField2();  //how can i call the intended method
 
abstract class BaseClass{    str commonField;    abstract void initFields(str _line)    {    }    public str getCommonField()    {        return commonField;    }    public static BaseClass newFromEnum(Enum _enum)    {        EnumAtrribute enumAtrribute = new AMIVolkswagenInterfaceTypeAtrribute(_enum);        return SysExtensionAppClassFactory::getClassFromSysAttribute(classStr(BaseClass), enumAtrribute);    }}
[EnumAtrribute(Enum::A)]class SubClassA extends BaseClass{    str field1;    str field2    public void initFields(str _line)    {        field1            = subStr(_line, 1, 2);        field2            = subStr(_line, 73, 1);            }    public str getField2(str _field2 =  field2)    {        field2 = _field2;        return field2;    }}
[EnumAtrribute(Enum::B)]class SubClassB extends BaseClass{    str field1;    str field2;    str field3;    public void initFields(str _line)    {        field1            = subStr(_line, 3, 5);        field2            = subStr(_line, 9 , 10);        field3            = subStr(_line, 91 , 98);                   }    public str getField2(str _field2 =  field2)    {        field2 = _field2;        return field2;    }}
 
I have the same question (0)
  • Suggested answer
    Martin Dráb Profile Picture
    237,679 Most Valuable Professional on at
    How to call methods from subclasses when we are defining the parent class
    Please post your code once more (with line breaks). You already know how to do it (because we discussed it in your thread How to post code in dynamics community in a proper format).
     
    In general, the parent class has no idea about any child classes, therefore it can't call their methods. But you can create a method (possibly abstract) in the parent class, call it there and override it in a child class.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 683 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 563 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 398 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans