Skip to main content

Notifications

Finance | Project Operations, Human Resources, ...
Suggested answer

Abstract, Interface and inheritance

(0) ShareShare
ReportReport
Posted on by 1,550
Hi,
 
How to decide if my base class should be abstract OR interface OR normal class that sub classes can extend?
 
Can you please give me an example and clearly explain why each one was chosen in this example
  • Suggested answer
    GirishS Profile Picture
    GirishS 27,821 Super User 2024 Season 1 on at
    Abstract, Interface and inheritance
    Hi Junior,
     
    For interface let us consider you have two calculations - Based on some conditions you need to call the respective methods.
    For this we will create 1 method in interface.
    interface Class1
    {
         void calculations1(){}
    }
     
    Public class2
    {
         //add 1st calculations.
          void calculation1()
          {
          }
    }
     
    Public class3
    {
          //add 2nd calculations.
          void calculation1()
          {
          }
    }
    //Now we will call the respective class based on some condition.
    Public class4
    {
         If(1stCalculation)
         {
               Class1 obj = new Class2(); this will execute the logic on class2.
             
         }
         
         If(2ndCalculation)
         {
               Class1 obj = new Class3(); this will execute the logic on class3.
             
         }
     
    }
    So, in interface with the help of the single method we are calling the respective class logics which implements interface.
    Abstract class is also same as that of interface but it will have both abstract and Non abstract method. In abstract method there is no body but in Non abstract method you can add your while body. In that child class you can just simply override the logic of Non abstract method.
     
    Thanks,
    Girish S.
     
  • Suggested answer
    Anton Venter Profile Picture
    Anton Venter 19,493 Super User 2025 Season 1 on at
    Abstract, Interface and inheritance
    Hi,
     
    In a nutshell: in most cases you will just use classes for making business logic and or extending existing code. Abstract classes are used if you don't want the class to be instantiated directly. This is mostly used in the base/parent class which contains the 'common' code./methods. The child classes inheriting from the base/parent class specialise the common code/methods declared in the base class, therefore is does not make sense to instantiate the base class but one of the chile classes. Have a look at the Bank class and child classes.
     
    Interface classes are not used that much and forces child classes to implement methods in the child class so that the caller knows it can call those methods. Have a look at RunbaseBatch/Runbase as an example, it inherits and implements.
     
    For information about the subject, search for Object Oriented Programming (OOP).

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…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

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

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,971 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,846 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans