Pages

SPONSORED

Static Blocks or Static Constructors

As the name suggests static blocks are used to initialise the static fields of the class.

·         These blocks or methods get executed only once for a class.
·         They are executed even before the constructors are executed.
·         The static fields with initializes in declaration will get executed before the  static blocks.
·         They are triggered when any of the following events occur:
o   An instance of the class is created.
o   If any of the static members of the class is accessed.


 The implementation of this is slightly different in Java and C#.

In Java
All blocks that are declared static are executed when a class is loaded. Static blocks in Java do not have a function name.

public class StaticBlockExample {
public static final int abc = 20;
public static final int def;

//Static block to initialise the static variable def
static {
          if(abc == 20)
def = 10;
else
def = 15;
}
//Constructor gets executed after static block is executed
public StaticBlockExample (){}
}


In C#
Static blocks are called as Static Constructors in C#. They are declared using static constructor declarations.

public class StaticBlockExample
{
static int abc = 20;
static int def;

  static StaticBlockExample ()
  {
if(abc == 20)
def = 10;
else
def = 15;
   }
}

What do you mean by Inheritance?

Inheritance is a process through which one object can inherit the characteristics of another object. This is similar to the parent-child relationship in real time scenario where a child can inherit the characteristics of parent.

Inheritence  helps in setting up relationships among objects and can help in reusing of code. Implementations of the parent can be simply inherited by the child. The child need not re-implement the functionality unless any change in behaviour is expected.This also impleves maintainability by avoiding duplication of code and hence reduces defects as well as development time. This in turn can improve code quality.

In inheritance, objects are defined by classes. The existing class is also called as– super class, parent class, base class and ancestral class. The inherited class is called as – sub class, child class, heir class or derived class.

 
Inheritence can be depicted by a simpe example -
To represent different kinds of geometric shapes, such as circles or rectangles programmatically we can use inheritance. We can create separate classes for each shape, but these different kinds of shapes share a number of things in common. We can define a type called "Shapes" to represent any of these different kinds of figures (circles or rectangles). We could then say that circles and rectangles are a kind of figure. A given type includes all of the record components specific to it, plus all the record components of its ancestors, so a square has all the information a rectangle would.

We'll also need to define some operations on these types, such as "Draw", "Area", and "Perimeter". Each calculated in a different way for each of the shape. Pictorial representation of this example is shown below.



Inheritance Example
 
 
We have seen how and when to use inheritence. We have also discussed its advantages. This does not mean that we implement inheritence in all scenarios. Do not use inheritence just for the sake of reuse. Avoid using it if there is no sub type relationship. Overuse of implementation inheritance can break all the subclasses, if the superclass is modified. Use composition for code reuse in such cases.

Quality of code can be achieved when the developer is smart enough to decide on the feature to put into use from OOP.

What do you mean by Polymorphism?

 Polymorphism, as the name suggest represents many forms.["Poly" means many, and "morph" means form].Polymorphism allows methods to behave differenlty in different situations depending on the input/scenario.The polymorphic method methods behaves in the same consistent manner with all the communicating objects even though their implementations varies.

In polymorphism the behaviour of method is decided during run time depending on the type of input passed to the method. The process used by object-oriented programming languages to implement polymorphism is called dynamic binding.


Polymorphism Example
 Polymorphism can be implemented with inheritence. Base class must have virtual methods defined and implemented. Child Class can override these virtual methods by providing their own implementation as per requirement.During run time the complier decides the implemetation to be executed depending upon the type of the object requesting the execution. Thus calling the method of the base class causes the execution of the required child class.

Visit http://www.codeproject.com/Articles/1445/Introduction-to-inheritance-polymorphism-in-C
for more details on implementation and real time examples on Polymorphism.

Note : In C#, every type is polymorphic. This is because all types, including user-defined types is inherited from Object.

Why do we need object oriented programming?

The Object Oriented Programming Languages directly represent the real life objects like Automobile, Flower, Pen etc. The most used  and powerful features of OO programming are polymorphism, inheritance and encapsulation.
[Tip to remember : PIE - Polymorphism, Inheritence and Encapsulation.].

Benefits of OOP

Reusable: Using the concept of inheritance the implementation of other class can be used. For example, if we have a to create classes for birds, we can have a prent class Bird which can implement functionality fly(). This functionality remains same for all child bird classes which inherit the parent class Bird.

Simple: OOP represents the real life objects. This real mapping easy understanding, reduction of complexity and makes the program structure simpler.

Modular: Usage of objects, classes etc helps in clubbing the relevent functinality together. This helps in easy understanding if the system and also makes the implementation independent of each other. This makesThis in turn will make it very easy to modify the system when required.  
Maintainable: It becomes very easy to make minor changes to the system as this approach is modular and is loosely coupled. The entire system need not be modified to incorporate the changes and hence reduces the development time.
Extensible: It is specified that the system becomes loosely coupled. This makes it easy to plug in more features to the system. This way we can extend the application to provide more features to the user. 
Summary
Considering the above points we can conclude that using OOP can definetly help us in providing quality code. This can be achieved as we will be able to reuse the code which helps us avoid code and test the reuse sections. This not only helps us in reducing the defects but can also reduces the development time.

Maintainence of the application becomes very easy as OOP is more extensible, reusable and modular.