Classes, Inheritance and Overriding in SalesForce

Oops Concepts in Salesforce

Classes

In a class, we have two types of numbers

  • Static Class
  • Non-Stack Class

Classes in Salesforce

  • Static means belongs to class & non – static means belongs to object
  • Static number methods can access the static and nonstatic data members
  • Non –static member methods can access the static and non –static data members
  • You can access the static methods through

 

If you would like to become a Salesforce Certified professional, then visit Mindmajix - A Global online training platform: " Salesforce Certification Training Course ". This course will help you to achieve excellence in this domain.

 

Classname. Method name

  • You can access the non-static methods through

Object name. method name;
                                 ↓

Object handle. method name;

Types of methods we perform all over Apex are

  1. Constructors  → [DC, PC]
  2. Setters
  3. Getters
  4. Operations (actions) →  in the apex

When to use static & non-static method as?
→ The non-static method is used w.r.t the object what you perform
→ The static method is used independently of the object what you perform

MindMajix Youtube Channel

Memory Belongs To Static & Non-Static  

 Memory Belongs To Static & Non Static

  • Only one single copy of memory for the static memory method. It also contains no. of objects,

Inheritance

  • Routing of one encapsulation of performing elements to build another encapsulation
  • The main advantage is the reusability of features
  • Because of these features oops are highlighted

Q: Why do we need inheritance?
It enables better and faster development of new features with the ability to reuse and modify the existing features
Ex:

Class account{
Name
Number
Balance
Class SavingAccount{
Name
Number
Balance
Min bal
Class Current Account{
Name
Number
Balance
Min bal
Own bal
Constructor
Setter
Getter
showBal
withdraw
deposit
}
Constructor
Setter
Getter
showBal
withdraw
deposit
}
Constructor
Setter
Getter
showBal
withdraw
deposit
}
  • Saving account = accounts + mm bal
  • Current account = SA + owner name
  • = account + min Bal + owner name
  • To work with inheritance we require at least two classes, but all previous features are explored through a single class
  • Out of these two classes, a class that contains the essential features and provides its members for another encapsulation is known as a parent (or) base (or) superclass
  • The class which reuses the existing class members or depends on the other class for its existence  is known as a child (or) derived class (or) subclass

Syntax For Inheritance:

Class parent{
}
Class child extends parent{
}

Extends
(It is a keyword)
Nothing but visa

  • The parent members are coming to child class for visiting through visa

Class parent {
Private integer pm1: → security
Private integer pm2: → reusability
Protected integer pm3: → security + reusability
Scope: Within the class and related classes
Types of classes in an inheritance
1. Final
2. Virtual
3. Abstract
Every class is final by default in inheritance
 

Class type
What we’re in a class
Objects creation
reversible
Final
Data members
member methods
def’s
We can create
objects
Not reversible
Virtual
Data members
member methods
def’s
We can create
objects
Reversible
Abstract
Data members
member methods
def’s and also
method prototypes
construct
No object can
be created
Reversible


Virtual → [Class, Method]
Override  →  method  ↓
Years strike-off older one work with new only
→     Only virtual and abstract methods can be overridden
→     Abstract methods can only write in abstract classes

Overriding

It is object-oriented programming that enables the child class to provide different implementations for a method that is already implemented in its parent class.

  • This is possible through only inheritance
  • Multiple methods containing the same name, the same signature is inherited (virtual) and another is originated (override) in the child class

Q.Why overriding?

If a parent class method serves the purpose of  a child class, do not override (or) else make the child over the ride

Abstract Class:

  • No object can be created for this class
  • An abstract method can exist only in the abstract class
  • An abstract method is declared only to enforce the implementation of the method
  • Any class that inherits an abstract class must definitely override the parent abstract Method, or else the compiler run through an error
  • For the actions performed by the child class, we go to abstract classes
  • The parent does not need anything to override if any class is defined as an abstract class
Class Typemethod definitionMethod prototypeCreate objectsinherited
Final_/×_/×
Virtual_/×_/_/
abstract_/_/×_/

 

Class Typemethod definitionMethod prototypeoverrideClass type
Final_/××Final
Virtual_/×_/Virtual and abstract
abstract×_/_/abstract

 

Key Words:

  • Extend
  • Protected
  • Virtual
  • Abstract
  • Override

Q.When to make a method final?

  • If its implementation is perfect and does not require any further modification, use, make it as final

Q.When to make a method virtual?

  • A method is implemented for a specific purpose and can be modified for any other purpose.

Q.When to make a method abstract?

  • Whenever we can’t decide  the implementation and need to carry out it at a later point of time, then we make it abstract

Abstract method

Ex:

Public virtual class parent {
Private integer pvt_mem;
Protected integer ptd_mem;
Public integer pub_mem;
Public virtual void gets values (){
System. Debug (‘Pvt Mem’ +pvt_mem);
System.debug(‘ptd Mem’ +ptd_mem);
System. Debug (‘pub mem’+ pub_mem);
}
}
Public class child extends parent {
Private integer pvt_ch_mem
Public override void get values () {
System. Debug (‘Pvt ch Mem’ + pvt_ch_mem);
}
}
Global class test {
Public static test method void main () {
Parent p1= new parent ();
P2.get values ();
Child c1= new child();    c1.gets values ();
}


Ex:

Public abstract class parent {
Public abstract void gets values ();
}
Public class child extends parent {
Private’s integer pvt_ch_mem;
Public override void get values () {
System. Debug c’ Pvt.Mem’ + pvt_ch_mem);
}
}
Public class test {
Public static test method void main () {
Child c1 =new child ();
C1.gets values ();
}
}
Parent p1 = new  child ();
Parent handle can refer to the child object
Child c1 = new parent ();


Parent object doesn’t handle the child
→ Child handles can’t refer to the parent object
Child handles
Controller Appex

Explore Salesforce Sample Resumes! Download & Edit, Get Noticed by Top Employers!
Course Schedule
NameDates
Salesforce TrainingOct 19 to Nov 03View Details
Salesforce TrainingOct 22 to Nov 06View Details
Salesforce TrainingOct 26 to Nov 10View Details
Salesforce TrainingOct 29 to Nov 13View Details
Last updated: 04 Apr 2023
About Author

Arogyalokesh is a Technical Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Salesforce, Blockchain, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.

read less
  1. Share:
Salesforce Articles