How Inheritance & Polymorphism works?

A class extends another class using the extends keyword in the class definition. A class can only extend one other class, but it can implement more than

Table of Contents

Recap of previous Sessions

In the previous blog, we learned “Polymorphic Query in Salesforce” If you have missed the session, please refer to the below link.

Introduction

Inheritance is a fundamental concept in any programming language where the child class can access the properties or methods of the parent class that it’s extending.

If you have a requirement where you want to inherit one class in another class then you need to make that class either virtual or abstract.

You can extend a class to provide more specialized behavior. Below is the example virtual class with virtual methods.


					
				

A class extends another class using the extends keyword in the class definition. A class can only extend one other class, but it can implement more than one interface.


					
				

Important concept of Classes in Salesforce

  • Static keyword
    • A static variable or method is a class-level construct that is initialized only once and is associated with the outer class, along with its initialization code. Such variables and methods can be accessed directly using the class name, without the need to create an instance of the class.
  • Keywords
    • Final
      • This keyword is used to define constants and methods that can’t be overridden.
    • super
      • This keyword invokes a constructor on a superclass. Refers to the Variables and Method of a superclass
    • this
      • This keyword represents the current instance of a class.
    • return
      • This keyword returns a value from a method.
    • null
      • This keyword defines a null constant that can be assigned to any variable.

Method Overloading vs Method Overriding

  • Method Overloading
    • Does not Require a Parent-Child Relationship
    • method name should always be the same
    • method return type could be different
    • method parameter must be different or method parameter data types must be different
    • All the methods should be within the same class
  • Method Overriding
    • Requires a Parent-Child Relationship
    • return type should be the same as the parent class method
    • method variables/parameters should be the same as the parent class method
    • method name should always be the same
    • Uses override keyword in child class
    • in parent class, the method must be virtual

What is Polymorphism in Salesforce?

A person at the same time can have different characteristics. Like a man, at the same time is a father, a husband, and a team member. So the same person possesses different behavior in different situations. This is called polymorphism.

What is Method Overloading in Salesforce
If a class has multiple methods having the same name but different parameters, it is known as Method Overloading.

There are two ways to overload the method in Java

  • The number of arguments is different in the method
  • Changing the data type of the arguments

Example


					
				

Hands-On


					
				

					
				

					
				

					
				

					
				

Watch Complete Videos

Assignment

Create a virtual Class named Vehicle and create the below methods

  1. start
  2. stop
  3. changeGear

Create another class Mercedes that extends the Vehicle class and overrides the above methods within the Mercedes class.

Resources

Amit Singh
Amit Singh

Amit Singh aka @sfdcpanther/pantherschools, a Salesforce Technical Architect, Consultant with over 8+ years of experience in Salesforce technology. 21x Certified. Blogger, Speaker, and Instructor. DevSecOps Champion

Articles: 299

Newsletter Updates

Enter your email address below and subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *

Discover more from Panther Schools

Subscribe now to keep reading and get access to the full archive.

Continue reading