Pages

SPONSORED

Abstract class and Interface

Abstract Class
Abstract class is a class which cannot be instantiated. We need to inherit this class and then instantiate the child class. Abstract class always acts as base class and contains one or more abstract or non abstract methods. An abstract method is a method that has only definition but not implementation. The child class must provide the definition for all the abstract method of its parent. However if it does not provide the implementation then the child class too must be declared abstract.

Static, Value Types & interface doesn't support abstract modifiers. Static members cannot be abstract. Classes with abstract member must also be abstract.

Example
Consider implementing the shapes available under Insert > Shape option of a word document. Some of the properties like fill, border, line style etc are common among all shapes. However other properties must be implemented separately.


We create an abstract class Shape. It is declared abstract as we do not intend to instantiate as shape in general makes no sense. Then create sub classes rectangle, triangle, circle etc.

Interface
Interfaces are used to separate implementation and definitions. It comes to use when the implementations need to be interchangeable and also in cases where the implementation changes frequently.

Interfaces define a generic template and just have method declaration and might contain properties. These method declarations are implicitly public and abstract. Interfaces cannot be instantiated. Interfaces can inherit other interfaces.

A class can implement the interface and implement the methods as per requirement. Each class which implements the interface can implement the method in its own way and can reflect the expected behaviour as per requirement.

In case the class does not implement all the methods from the interface then the class must be declared abstract. This class must be further extended to sub class and implementation must be provided in that sub class. This sub class can be later instantiated and put into use.

Interface definition
public interface Shape
{
//Can have any function which a shape required to be implemented
function area();
}

Implementing the interface
public circle implements shape
{
//Implement the area as required for circle
Function area()
{
                    //calculate area of circle
}
}


The below link has detailed explanation on when to use abstract class and interfaces

 

Composition and Aggregation

Composition
Composition expresses relationship between objects. It is an association in which one class belongs to a collection. The containing object is responsible for the life time of the object it holds within it. The containing object cannot exist without the main object. It expresses the “has a” relationship among the objects

Example:  Office has a reception, Student has a name, Car has a tyre etc.

Aggregation
It is relationship which is just a reference between objects. The objects are independent of each other.

Example: Chair and person, Chair does not own the person.
Employee and address, address stays even after employee is destroyed.

The table below lists the key differences between composition and aggregation

Composition
Aggregation
Relationship where a part cannot exist without whole
Relationship where a part can exist without whole
Stronger relationship
Weaker relationship
Main object owns the containing objects
Objects are independent of each other
Main object instantiated the containing objects
Objects within the relationship are instantiated independently
Containing objects are destroyed when main object is destroyed
If one object is destroyed other object can survive.




What do you mean by Encapsulation?

As the name suggests Encapsulation means keeping all the objects data is contained together within the object. Data of object here refers to its variables and methods. Object hides the inner implementation from the outside view. Encapsulation controls interactions of objects. It prevents object interaction in unexpected way. This improves code modularity as well as improves application security.
 
Encapsulation Example


Class Product{
private double cost = 0.0;

public void setCost(double cost){
if(cost > 0)
this.cost = cost;
else
throw new Exception("No negative Values for Cost");
}

public int getCost(){
return cost;
}
}

The sample code above shows how the value of cost of the product is controlled. The value of cost can be set only via setCost method which takes care to avoid negative values to be set for the variable.Secure member can be modified in this manner and can be helful to control the code from insecure access.

Access modifiers are keywords used to specify the declared accessibility of a member or a type. The following are access modifiers for C++, C# and Java.
C# and C++
Public: All Objects can access it.
Protected: Access is limited to members of the same class or descendants.
Private: Access is limited to members of the same class.
Internal: Access is limited to the current assembly.
Protected Internal: Access is limited to the current assembly or types derived from the containing class.

The first 3 access levels are available both in C# and C++. But the last 2 are available only in C#.

Java
Public: All Objects can access it.
Protected: Access is limited to members of other classes within the same package or descendants.
Private: Access is limited to members of the same class.
No Modifier: Access is limited to the current package.

Benefits
Maintainability: Encapsulation helps increases the maintainability of the code and reduces the refactoring effort.
Security: It increases security by avoiding objects interaction in unexpected way. This will protect the variables from unaccepted values.