Blog

Classes, Inheritance and Overriding in SalesForce

Classes

In a class we have two types of numbers

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

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 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 independent of the object what you perform
 

Memory Belongs To Static & Non Static  

 

  • Only one single copy of memory for 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 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 which contains the essential features and provides its members for another encapsulation is known as parent (or) base (or) super class
The class which reuses the existing class members or depends on the other class for its existence  is known as child (or) derived class (or) subclass
 

Syntax For Inheritance:
Class parent{
}
Class child extends parent{
}

Extends
(It is a key word)
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 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 overriden
→     Abstract methods can only write in abstract classes

Overriding

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

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

Q.Why over riding?
If a parent class method serves in the purpose of  a child class, do not override (or) else to 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 Type method definition Method prototype Create objects inherited
Final _/ × _/ ×
Virtual _/ × _/ _/
abstract _/ _/ × _/

 

Class Type method definition Method prototype override Class 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

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


        Enroll for Live Instructor Led Online SALESFORCE TRAINING


RELATED COURSES

Get Updates on Tech posts, Interview & Certification questions and training schedules