0% found this document useful (0 votes)
13 views6 pages

Salesforce OOP: Inheritance & Interfaces

The document provides a training overview on object-oriented programming concepts in Salesforce, specifically focusing on inheritance and interfaces. It includes examples of class inheritance, method overriding, and interface implementation in code. The training is conducted by Mohan from Stansys Software Solutions.

Uploaded by

Naresh Reddy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views6 pages

Salesforce OOP: Inheritance & Interfaces

The document provides a training overview on object-oriented programming concepts in Salesforce, specifically focusing on inheritance and interfaces. It includes examples of class inheritance, method overriding, and interface implementation in code. The training is conducted by Mohan from Stansys Software Solutions.

Uploaded by

Naresh Reddy
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

=====================================================================================

================

Salesforce Training By - 'MOHAN' || VLR Training|| Stansys software Solutions || +91 6301332114

=====================================================================================
================

Topic - oops [Inheritance & inteface]

=====================================================================================
==================

* A class that extends another class inherits all the methods and properties of the extended
class

* In addition, the extending class can override the existing virtual methods by using the override
keyword in the method definition.

* Overriding a virtual method allows you to provide a different implementation for an existing
method

=====================================================================================
===================

Inheritance

=====================================================================================
====================

Ex-1)

============================================================================

public virtual class Nov15_InheritanceParent {

public string studName = 'Joshna';

public string studBatchnumber = 'Joshna';

public student__c studRec = new student__c();

_______________________________________________________________________

public class Nov15_InheritanceChild extends Nov15_InheritanceParent{


public string mychildstd ;

public Nov15_InheritanceChild(){

[Link]('studName===>' + studName);

[Link]('studBatchnumber===>' + studBatchnumber);

[Link]('studRec===>' + studRec);

mychildstd = studName;

[Link]('mychildstd===>' + mychildstd);

______________________________________________________________________

Nov15_InheritanceChild ne = new Nov15_InheritanceChild();

============================================================================

Ex-2)

============================================================================

public virtual class Nov15_InheritanceParent {

public string studName = 'Joshna';

public string studBatchnumber = 'Joshna';

public student__c studRec = new student__c();

public account myparentaccount(){

account acc = new account();

[Link] = 'Mohan';
[Link]('acc===>' + acc);

return acc;

_______________________________________________________________________

public class Nov15_InheritanceChild extends Nov15_InheritanceParent{

public string mychildstd ;

public account accChild ;

public Nov15_InheritanceChild(){

[Link]('studName===>' + studName);

[Link]('studBatchnumber===>' + studBatchnumber);

[Link]('studRec===>' + studRec);

mychildstd = studName;

[Link]('mychildstd===>' + mychildstd);

accChild = myparentaccount();

[Link]('accChild==>' + accChild);

_______________________________________________________________________

Nov15_InheritanceChild ne = new Nov15_InheritanceChild();

===================================================

Ex-3) Overriding inheritance

=========================================
public virtual class Nov15_InheritanceParent {

public student__c studRec = new student__c();

public virtual void prentStudentCreat(){

[Link] = 'Joshna';

studRec.Batch_Number__c = 2;

studRec.Country__c = 'India';

_______________________________________________________________________

public class Nov15_InheritanceChild extends Nov15_InheritanceParent{

public override void prentStudentCreat(){

[Link] = 'Mani';

studRec.Batch_Number__c = 2;

studRec.Country__c = 'USA';

studRec.Bank_Detail_other__c = '89dfoafu4449ru';

[Link]('studRec==>' + studRec);

_______________________________________________________________________

Nov15_InheritanceChild ne = new Nov15_InheritanceChild();

[Link]();

_______________________________________________________________________

Nov15_InheritanceParent ne = new Nov15_InheritanceChild();


[Link]();

====================================================================

==============================================================================
=====

Interface

==============================================================================
=====

Ex-1)

===========================================================

public interface Nov15_Interface {

string start();

void execute();

_______________________________________________________________________

public class Nov15_IntefaceChild implements Nov15_Interface{

public string start(){

[Link]('mychi==>');

account acc = new account();

return 'success';

public void execute(){

[Link]('execute chidl==');

_______________________________________________________________________

Nov15_IntefaceChild ne = new Nov15_IntefaceChild();

string str = [Link]();


[Link]('str==' + str);

[Link]();

=========================================================

=====================================================================================
================

Salesforce Training By - 'MOHAN' || VLR Training|| Stansys software Solutions || +91 6301332114

=====================================================================================
================

Common questions

Powered by AI

Composition can be used as an alternative to inheritance by creating objects from other classes to achieve functionality. Instead of 'Nov15_InheritanceChild' inheriting 'Nov15_InheritanceParent', it could include an instance of a class that provides the needed functionality (e.g., student details and account management). This promotes greater flexibility, as the composed objects' behaviors can be changed at runtime, and classes can be easily replaced or decorated with new features without modifying existing systems, preventing the tight coupling seen in inheritance .

The potential drawbacks of using inheritance include increased complexity and reduced flexibility. In the document, the dependency on a single parent class means any change in 'Nov15_InheritanceParent' affects 'Nov15_InheritanceChild'. Furthermore, tight coupling between base and derived classes can lead to a rigid class hierarchy, making it difficult to adapt the design for new requirements without significant changes. This could lead to potential issues in scalability and maintainability .

Access modifiers determine the visibility of class members to other classes and are crucial in inheritance for encapsulation. In the document, public access modifier allows 'Nov15_InheritanceChild' to access 'studName', 'studBatchnumber', and associated methods from 'Nov15_InheritanceParent'. Proper use of these modifiers upholds encapsulation principles by allowing access to only necessary methods and properties, enhancing the integrity and security of the data within these objects .

In inheritance, constructors initialize object state and establish the necessary setup for instance variables. In the document's examples, when 'Nov15_InheritanceChild' is instantiated, its constructor is called, which in turn accesses the inherited attributes from 'Nov15_InheritanceParent'. The child’s constructor initializes 'mychildstd' using the inherited 'studName'. Constructors in inheritance ensure that the base class state is properly initialized before any specific behavior in the derived class is executed .

Inheritance in OOP allows a class, known as the child or derived class, to inherit attributes and behaviors (methods) from another class, known as the parent or base class. This promotes code reusability because you can create new functionalities or modify existing ones without altering the original code in the parent class. For example, in the provided class structure, 'Nov15_InheritanceChild' inherits properties like 'studName' and methods like 'myparentaccount()' from 'Nov15_InheritanceParent'. This allows the child class to use or override these properties and methods, demonstrated when 'Nov15_InheritanceChild' constructs its own account object 'accChild' using the inherited 'myparentaccount()' method .

Interfaces in OOP define a contract for classes, specifying methods that must be implemented without dictating how these methods should be implemented. This allows different classes to offer the same functionality while containing different internal implementations. In the document, the 'Nov15_Interface' is an example of this concept, defining a contract with methods 'start()' and 'execute()'. The class 'Nov15_IntefaceChild' implements this interface, providing its own implementations for these methods, hence allowing different classes to behave interchangeably at runtime by adhering to the same interface .

Method overriding allows a child class to provide a specific implementation of a method that is already defined in its parent class. This is beneficial for achieving polymorphism, allowing for dynamic method resolution and more flexible code. In the document, method overriding is shown in 'Nov15_InheritanceChild', which overrides 'prentStudentCreat()' from 'Nov15_InheritanceParent'. The child class changes the implementation to set different student details, such as changing the 'name' property to 'Mani' from 'Joshna', as illustrated when 'Nov15_InheritanceChild' and 'Nov15_InheritanceParent' are used polymorphically .

The 'virtual' keyword in inheritance structures is used to indicate that a method can be overridden in any derived class. It provides a baseline implementation which a derived class can choose to modify. In the document, 'Nov15_InheritanceParent' uses 'virtual' for the method 'prentStudentCreat()', indicating that derived classes such as 'Nov15_InheritanceChild' can provide an alternate implementation, thus facilitating polymorphic behavior .

Dynamic method dispatch is demonstrated through method overriding and polymorphic execution. In the document, an instance of 'Nov15_InheritanceChild' is referenced through a 'Nov15_InheritanceParent' type. When 'prentStudentCreat()' is called on this reference, the overridden method in 'Nov15_InheritanceChild' executes. This happens because, at runtime, the instance's actual class type determines which method implementation to invoke, a key feature of polymorphism .

Polymorphism in OOP allows objects of different classes to be treated as objects of a common superclass. It manifests in the examples through method overriding and the use of interface-based polymorphism. In the document, 'Nov15_InheritanceChild' overrides 'prentStudentCreat()', allowing it to modify behavior when accessed through a reference of type 'Nov15_InheritanceParent'. This is significant because it supports the implementation of flexible and maintainable code, where more generalized code is used to manipulate more specialized types as displayed by the creation and execution of 'Nov15_InheritanceChild' objects through a parent class type .

You might also like