Skip to main content

Inheritance in Apex

Inheritance in Apex


Inheritance in Apex is a mechanism by which one object acquires all the properties and behaviors of parent object.

The idea behind inheritance in Apex is that you can create new classes that are built upon existing classes. Whenever you inherit from an existing class, you can reuse methods and fields of parent / super class, as well as you can add new methods and fields also based on the requirements.


Reason to use inheritance :

  • For Method Overriding (to achieved runtime polymorphism).
  • For Code Reusability

Various term used in inheritance :

  1. Class: A class is a concept/prototype/template of common properties, from which objects are created.
  2. Child Class/Sub Class: Child class is a class which inherits the other class, known as child class. It is also called extended class, derived class, or sub class.
  3. Parent Class/Super Class: Parent class is the class from which a subclass inherits the features. It is also called a Super class or a base class.
  4. Virtual: The virtual modifier declares that this class allows for extension and overrides.
  5. Override: You cannot override a method with the override keyword in the child class unless the class and method have been defined as virtual.

Example ,

here I have created a parent class and declared it as virtual , so that it can be inherited by child / other class.
public virtual class ParentClass {

    public void showDataParent() {
        System.debug('This is Parent-Class');
    }
    
}

here I have created a child 
public class ChildClass extends ParentClass {
    
    public void showDataChild() {
        System.debug('This is Child-Class');
    }

}
Now create an object of ChildClass apex class.
ChildClass objChildClass = new ChildClass(); // child-class object created.

objChildClass.showDataChild(); objChildClass.showDataParent();
See the output :


By : Harshal Lad 

Comments

Post a Comment

Popular posts from this blog

Salesforce Development Index

C ourse C ontent Declarative Features Vs Programmatic Features Salesforce Architecture ( MVC ) Data Types Apex Classes If – Conditional and loops Collections in APEX List Set Map Oops Concepts Inheritance Polymorphism Encapsulation Abstraction Data Manipulation Language (SOQL) & SOSL Exception Handling Triggers and its Best Practices Custom Setting, Custom Metadata and Custom Labels Where to use it ? How to use it ? Apex Trigger + Custom Setting Asynchronous Apex Batch Classes Scheduler Classes Queueable Classes Future Method Apex Governor Limits Test Classes for Triggers and Apex classes Visualforce Pages Standard Controllers Extension Controllers Custom Controllers Wrappers Classes in Apex By : Harshal Lad ( CorpoVision Te

Opportunities at Paidwork Company

Unveiling the Potential of Paidwork Company : Unlocking Earning Methods, Referral Programs, and Achievements In the ever-evolving landscape of remote work and freelancing, Paidwork has emerged as a noteworthy platform that not only offers a diverse range of earning methods but also provides users with unique opportunities for growth and recognition. This article will delve into the intricacies of Paidwork, exploring its earning methods, referral program, and the benefits of achieving milestones on the platform. Go to Paidwork Website 1. Earning Methods on Paidwork: Paidwork provides freelancers and remote workers with various avenues to earn money, catering to a wide range of skills and expertise. The primary earning methods on Paidwork include: a. Freelance Projects: Users can browse through a plethora of freelance projects posted on Paidwork. From graphic design and content creation to software development and digital marketing, the platform hosts diverse opportunities for indi

Apex Triggers and Classes

  Apex Triggers   is use to execute the functionality or piece of code on record manipulation ( i.e., CREATE / UPDATE / DELETE / UNDELETE   records ). A trigger executes before and after an event occurs on record.