Skip to main content

Notifications

Announcements

No record found.

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,550
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;    }}
 
  • Suggested answer
    Martin Dráb Profile Picture
    Martin Dráb 230,994 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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Congratulations to the January Top 10 leaders!

Check out the January community rock stars...

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,234 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,994 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans