Monday, April 13, 2015

OOP'S in Salesforce

The core of the pure object-oriented programming is to create an object, in code, that has certain properties and methods. While designing Salesforce modules, we try to see whole world in the form of objects. For example a car is an object which has certain properties such as color, number of doors, and the like. It also has certain methods such as accelerate, brake, and so on.

There are a few principle concepts that form the foundation of object-oriented programming:


Object:

This is the basic unit of object oriented programming. That is both data and function that operate on data are bundled as a unit called as object. under the next section(Class) i clearly explained about object along with example...

Class:

When you define a class, you define a blueprint for an object. This doesn't actually define any data, but it does define what the class name means, that is, what an object of the class will consist of and what operations can be performed on such an object.


public class caluclator

{

// declaring data members

integer a;

integer b;

integer c;

//delaring and implementing member methods..

public void add()

{

c=a+b;

}

public void mul()

{

c=a*b;

}

public void div()

{

c=a/b;

}


in the above program we have both data members and member methods which work on data members..its just like blue print of the program.. when ever we create object for above class will help you to get expected out put..

Not clear ?

when ever  we go to tailoring shop we will be giving our measurements as taillor will note down the measurements acording to our body length. Taking notes is nothing but class or blue print however when he stiches the real cloth acording to the measurement will give you exptected dress which is nothing but object.   

Now taking measure measurements is nothing but class and implementing the taken notes can be called as object..

How to create object..?

we need to create instance of the class by using new operator..

syntax    

classname instancename =new classname();

caluclator cal1=new caluclator();

How to accesss methods of the class?

instancename.methodname();

cal1. add();

 cal1.mul();

cal1.div();


Abstraction:

Data abstraction refers to, providing only essential information to the outside world and hiding their background details, i.e., to represent the needed information in program without presenting the details.


In the below example code  getvalues() method is not implemented in the same class to hide the background details, complextiy of  that particuler method will be implemented in the child class.


public abstract class parent 

{

public abstract void getvalues()

}

Abstract methods will not have body,  these methods will contain only defination.  In the below code getvalues() method is not implemented in the parent class, implementation is done at child level. unlike Interface its not manditory to implement the abstract method .

public abstract class parent

{

public abstract void getvalues()

}


public class child extends parent

{

private integer pvt-ch-mem;

public override void getvalues()

{

syatem.debug'pvt ch mem'+pvt-ch-mem);

}

}

public class test

{

public static testmethod void main()

{

child c1=new child();

c1.getvalues();

}


}

To declare method as abstract we need to use abstract keyword infront of parent class and the intended method in the same class

Not clear ?

No problems...

suppose you need to plan a marriage function. first step what you will do is you will note down what the things you suppose to plan..  Ex : FoodServing();

                                        Decoration();

                                        Drinks().....etc..

public abstract  class marriage

{

abstract  FoodServing();

abstract  Decoration();

 abstract  Drinks().....etc..

}

in the above program you dont need to expose how foodserving will be,what drinks need to serve ....its just like plan to implement...


Encapsulation:

Encapsulation is placing the data and the Mehods that work on that data in the same place. While working with procedural languages, it is not always clear which methods work on which variables but object-oriented programming provides you framework to place the data and the relevant methods  together in the same object.

------------------------------------------------------------------------------------------------------------------------------------------------------------------

simple bank program to explain encaplsulation..

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

public class account

{

string name;

Integer ac-no;

interger bal;

pablic account()

{

name='no name';

ac-no=0;

bal=0;

}

pablic account(string name, integer ac-no, integer bal)

{

this.name=name;

this.ac-no=ac-no;

this.bal=bal;

}

pubilc void setvalues(string name, integer ac-no, integer bal)

{this.name= name;

this.ac-no=ac-no;

this.bal=bal;

}

public void deposit(integer amount)

{

bal+=amount;

}

public void withdraw(integer amount)

{

bal-=amount;

}

public void showbalance()

{

system.debug('balance rs='+bal');

}

public void getvalues()

{

system.debug('name='+name');

system.debug('ac-no='+ac-no');

system.debug('balance rs='+bal');

}

}

global class test

{

public static testmethod void main()

{

account acc1=new account('ashok', 100, 4000);

acc1.diposit(10000);

acc1.showbalance();

acc1.withdraw(15000);

acc1.getvalues();

account acc1=new account();

acc2.setvaluce('basu',101, 50000);

acc2.getvalues();

}

}


In the above program datamembers (string name;Integer ac-no;interger bal;) and Member methods  1)pubilc void setvalues(string name, integer ac-no, integer bal)   2)public void deposit(integer amount)  3)public void deposit(integer amount)   4)public void withdrow(integer amount)  5)public void showbalance()  6)public void getvalues()..  are binding together to perform some actions... In a simple way whenever datamembers are used in the member methods in the single class along with access modifiers(public, private,protected and global)   can be called as Encapsulation.


Inheritance:

One of the most useful aspects of object-oriented programming is code reusability. As the name suggests Inheritance is the process of forming a new class from an existing class that is from the existing class called as base class, new class is formed called as derived class.

This is a very important concept of object-oriented programming since this feature helps to reduce the code size.


-------------------------------------------------------------------------------------------------------------------------------------------------------------

code for inheritance:

note: in order to inherit the parent class properties to child class we need to use EXTEND key word

------------------------------------------------------------------------------------------------------------------------------------------------------------


public virtual class parent

{

private integer pvt-mem;

protected integer ptd-mem;

public integer pub-mem;

public virtual void getvalues()

{

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 getvalues()

{

system.debug('pvt mem'+pvt-mem);

system.debug('ptd mem'+ptd-mem);

system.debug('pub mem'+pub-mem);

system.debug('pvt ch mem'+ pvt-ch-mem);

}

}

global class test

{

public static testmethod void main()

{

parent p1=new parent();

p1.getvalues();

child c1=new child();

c1.getvalues();

}


}

Polymorphism:

The ability to use an operator or method in different ways in other words giving different meaning or Methods to the operators or methods is called polymorphism. Poly refers to many. That is a single method or an operator functioning in many ways different upon the usage is called polymorphism.

Following concepts demonstrate different types of polymorphism in Salesforce.
1) Method Overloading
2) Method Overriding


1)Method Overloading:
 it is possible to define two or more methods of same name in a class, provided that there argument list or parameters are different. This concept is known as Method Overloading.


 class Overload
 {

    void demo (integer a)
    {
       system.debug('from method demo with single parameter'+a);
    }
    void demo (integer a, integer b)
    {
       system.debug('from method demo with double parameters parameter'+a+' '+b));
    }
    double demo(double a) {
         system.debug('from method demo with single parameter differentiated by datatype of the method and parmeter'+a);
       return a*a;
    }
}

2) Method Overriding

Child class has the same method as of parent class. In such cases child class overrides the parent class method without even touching the source code of the parent class. This feature is known as method overriding.
Overriding means striking of old one and replacing new one

Example:

public virtual class parentClass
{
    public virtual void methodToOverride() //Parent class method
    {
         System.debug("I'm the method of ParentClass");
    }
}
public class ChildClass extends parentClass
{
    public override void methodToOverride() //Child Class method
    {
         System.debug ("I'm the method of ChildClass");
    }
}



Note : we need to use virtual  keyword before the parent class and method which you suppose to Override , on the other hand we need use override keyword before the child class mehod which you want to override

No comments:

Post a Comment