Session 3 Interface Collection
Session 3 Interface Collection
Extending Class,Interface,Collection
Extending a class:
Inheritance in Apex is a mechanism by which one object acquires all the properties and behaviors of
parent object.
The idea behind inheritance in Apex is that you can create new classes that are built upon existing
classes. Whenever you inherit from an existing class, you can reuse methods and fields of parent /
super class, as well as you can add new methods and fields also based on the requirements.
System.debug(‘value of var1=’+var1);
System.debug(‘value of var2=’+var2);
execution:
c.display();
c.show();
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. This means that the behavior of a particular method is different based on the object you’re
calling it on. This is referred to as polymorphism.
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.
This example shows how the YellowMarker class extends the Marker class. To run the inheritance
examples in this section, first create the Marker class.
public virtual class Marker {
public virtual void write() {
System.debug('Writing some text.');
}
public virtual Double discount() {
return .05;
}
}
Then create the YellowMarker class, which extends the Marker class.
// Extension for the Marker class
Execution:
Marker obj1, obj2;
obj1 = new Marker();
obj1.write(); // This outputs 'Writing some text.'
obj2 = new YellowMarker();
obj2.write(); // This outputs 'Writing some text using the yellow marker.'
Double d = obj2.discount(); // We get the discount method for free// and can call it from the
YellowMarker instance.
This code segment shows polymorphism. The example declares two objects of the same type
(Marker). Even though both objects are markers, the second object is assigned to an instance of the
YellowMarker class.
Hence, calling the write method on it yields a different result than calling this method on the first
object, because this method has been overridden. However, you can call the discount method on the
second object even though this method isn’t part of the YellowMarker class definition. But it is part
of the extended class, and hence, is available to the extending class, YellowMarker.
The extending class can have more method definitions that aren’t common with the original
extended class. For example, the RedMarker class below extends the Marker class and has one extra
method, computePrice, that isn’t available for the Marker class. To call the extra methods, the object
type must be the extending class.
Extensions also apply to interfaces—an interface can extend another interface. As with classes,
when an interface extends another interface, all the methods and properties of the extended interface
are available to the extending interface.
Method Overloading:
If a class have multiple methods with same name but different parameters (either different length of
arguments or different data types of the arguments), is knwon as method overloading.
Example:
}
public void sum(Decimal no1,Decimal no2){
System.debug('Sum of two Decimal is='+(no1+no2));
}
}
Execution:
MethodOverloading obj=new MethodOverloading();
obj.sum(10,20);
obj.sum(10.5,20.5);
Method overriding:
If a subclass (child class) has the same method as declared in the parent class, it is known as method
overriding in Apex.
In other words, we can say If a subclass provides the specific implementation of the method that
has been provided by one of its parent class, it is known as method overriding.
Note: Always make sure to achieve method overriding the scope must be of parent and child
class relationship and you must have same name method needs to declare into child class with
same signature of the method from Parent class to achieve method overriding in Apex.
Method name must have same parameter as in the parent class’s method.
Interfaces
An interface is like a class in which none of the methods have been implemented—the method
signatures are there, but the body of each method is empty. To use an interface, another class must
implement it by providing a body for all of the methods contained in the interface.
Interfaces can provide a layer of abstraction to your code. They separate the specific
implementation of a method from the declaration for that method. This way you can have different
implementations of a method based on your specific application.
Defining an interface is similar to defining a new class. For example, a company might have two
types of purchase orders, ones that come from customers, and others that come from their
employees. Both are a type of purchase order. Suppose you needed a method to provide a discount.
The amount of the discount can depend on the type of purchase order.
You can model the general concept of a purchase order as an interface and have specific
implementations for customers and employees. In the following example the focus is only on the
discount aspect of a purchase order.
Here is the definition of the PurchaseOrder interface.
// An interface that defines what a purchase order looks like in general
public interface PurchaseOrder {
Double discount();
}
Abstract Class:
To create abstract class, we need to use abstract definition modifier.
Allow to extend the child class.
Abstract class can contain methods signed as abstract, to clarify, it is a method that has only a
signature (body is not defined).
Child class must implement all methods declared as abstract!
Abstract class can also include other methods, which have the logic. To allow child class access
those methods use protected or public keyword.
Cannot be initialize directly: new TestAbstractClass();
Abstract class can contain both virtual and abstract methods.
Abstract class is some kind of “partial” class. Therefore, some methods are implemented, some
needs to be implemented by child class.
virtual methods can be override, but this is not mandatory.
- Collection supports dynamic memory allocation, which provides efficient memory utilization.
- Collection can grow or shrink at runtime, depends upon number of elements added or removed
from collection.
- Collection provides utility methods to manage and manipulate elements stored in collection.
1. List
2. Set
3. Map
List:
Example 2:
Methods of List:
1. add(<element>):Used to add element in List collection. Element is inserted at the end of List
collection.
Ex:
if(lstElements.IsEmpty())
system.debug('Collection is Empty.');
else
system.debug('Collection is Not Empty.');
5. addAll(<collection>):Used to add one List collection of elements in current List collection.
6. get(<Integer index>):Used to return element stored at specified index position in List collection.
Ex:
system.debug('Element exist at the 2nd position is..: '+ lstElements.Get(2));
7. remove(<Integer indexPosition>): Used to remove element exist at specified index position from
List collection.
Ex:
lstElements.remove(2);
11. Integer indexOf(elementName): it returns the index position of specified elements in list.
Ex:
Ex:
List<String> backupCopy = customerNames.Clone();
Set:
To declare a set, use the Set keyword followed by the primitive data type name within <>
characters.
For example:
To access elements in a set, use the system methods provided by Apex. For example:
// Define a new set
Set<Integer> mySet = new Set<Integer>();
// Add two elements to the set
mySet.add(1);
mySet.add(3);
mySet.remove(1);
Methods of Set:
1. add(<element>):Used to add element in Set collection. Upon adding the element, Salesforce
arranges elements in sorting order by default.
2. addAll(<collection>): Used to add one Set collection of elements in current Set collection.
Map:
Example:
- Syntax:
Map<keyDataType, valueDataType> var = new Map<keyDataType, valueDataType>();
Methods of Map:
5. remove(<key>): Used to remove key-value pair element from Map collection, whose key is
specified.
7. get(<key>): Used to get the value which is associated with specified key.
8. Set<datatype> keySet(): It returns all keys exist in Map collection in the form of Set collection.
9. List<datatype> values(): It returns all values exist in Map collection in the form of List
collection.