Friday, November 30, 2012

difference between static polymorphism and dynamic polymorphism



Static Polymorphism

Static Polymorphism means “Compile” time polymorphism or “Early binding”.

     Method Overloading is an example of Static Polymorphism, where method name is same with different parameters and implementation.

Dynamic polymorphism

Dynamic polymorphism means “Runtime” time polymorphism or “Late binding”.

    Method Overriding is an example of Dynamic Polymorphism, where the base class method is overridden by override keyword and its implementation is also changed.

Example: -
Below is the simple code snippet for Method Overloading.
public class StaticPoly
{
public void Display(int x)
{
Console.WriteLine("Value of x:"+x);
}
public void Display(int x, int y)
{
int z = 0;
z = x + y;
Console.WriteLine("Addition of x and y is:"+ z);
}
static void Main(string[] args)
{
StaticPoly Obj = new StaticPoly();
Obj.Display(10);
Obj.Display(10,3);
Console.ReadLine();
}
}

Example: -
Below is the simple code snippet for Method Overriding.
class A
{
public virtual void Display()
{
System.Console.WriteLine("Base Class A::Display");
}
}
class B : A
{
public override void Display()
{
System.Console.WriteLine("Derived Class B::Display");
}
}
class Demo
{
public static void Main()
{
A b;
b = new A();
b.Display();
b = new B();
b.Display();
Console.ReadLine();
}
}

Thursday, November 29, 2012

Method Overloading and Overriding in Object Oriented Programming


Method Overloading:

Method overloading means having two or more methods with the same name but different signatures in the same scope. These two methods may exist in the same class or one in base class and another in derived class. 

E.g.
class Person
{
private String firstName;
private String lastName;
Person()
{
this.firstName = "";
this.lastName = "";
}
Person(String FirstName)
{
this.firstName = FirstName;
this.lastName = "";
}
Person(String FirstName, String LastName)
{
this.firstName = FirstName;
this.lastName = LastName;
}
}

Calling Overloaded Methods.
Person(); // as a constructor and call method without parameter
Person(userFirstName); // as a constructor and call method with one parameter
Person(userFirstName,userLastName); // as a constructor and call method with one parameter


When to use Method Overloading?
Generally, you should consider overloading a method when you have required same reason that take different signatures, but conceptually do the same thing. 

Method Overriding:
Method overriding means having a different implementation of the same method in the inherited class. These two methods would have the same signature, but different implementation. One of these would exist in the base class and another in the derived class. These cannot exist in the same class. 

Overriding method definitions
In a derived class, if you include a method definition that has the same name and exactly the same number and types of parameters as a method already defined in the base class, this new definition replaces the old definition of the method.

Explanation
A subclass inherits methods from a superclass. Sometimes, it is necessary for the subclass to modify the methods defined in the superclass. This is referred to as method overriding. The following example demonstrates method overriding.
In this example we will define a base class called Circle
Step 1
class Circle
{
protected double radius;
public Circle(double radius)
{
      this.radius = radius;
}
public double getArea()
{
      return Math.PI*radius*radius;

}
}

When the getArea method is invoked from an instance of the Circle class, the method returns the area of the circle.
Step 2
The next step is to define a subclass to override the getArea() method in the Circle class.
class Cylinder extends Circle
{
protected double length;
public Cylinder(double radius, double length)
{
     super(radius);
     this.length = length;
}
public double getArea()
{
     // method overriden here
     return 2*super.getArea()+2*Math.PI*radius*length;
}

}

When the overriden method (getArea) is invoked for an object of the Cylinder class, the new definition of the method is called and not the old definition from the superclass (Circle).
This is the code to instantiate the above two classes

Circle myCircle;
myCircle = new Circle(1.20);
Cylinder myCylinder;
myCylinder = new Cylinder(1.20,2.50);

Wednesday, November 28, 2012

OOPS in Short For Interview




OOPS

Class:


- Templates used for defining new types.
- describes all the attributes of objects, as well as the methods that implement the behavior of member objects.

Objects:


- Instance of a class is called an Object
- Object is real world entity which has a well defined structure and behavior.





Characteristics of objects:


1. State:

The state of an object encompasses the current values of all of its attributes. An attribute could be static (whose value doesn’t change over period of time) or dynamic (whose values change).

E.g. A car is defined by: color, average, power, fuel type (static), speed, fuel level, gear (dynamic)... Etc.


2. Behavior:

Behavior is how an object acts or reacts, in terms of state changes and operations performed on it.

E.g. A Windows object, the operations performed on it can be open, close, maximize, move, resize, and so on. Totality of operations performed on the window and the consequent changes in the attributes define the behavior of the window.


3. Identity:

Identity is that property of an object which distinguishes it from all other objects. E.g. Each Student in the class identified by a unique roll number.


4. Responsibility:

The Responsibility of an object is the role it serves within the system.

Example: Object –BANK ACCOUNT


Attributes
State
Behavior
Identity
Responsibility
Balance
Balance=1000

Open
Deposit
Withdraw

Close
A/c No=10
Keeps a track of a stores money with the facility of deposits and Withdraw.
Interest Rate
Rate=10%
Account No
A/c No=10



1. Abstraction:

Abstraction refers to the act of representing essential features without including background details.

It facilitates the easy conceptualization of real world objects into the software program.

Classes use the concept of abstraction and are defined as a list of abstract attributes, and functions to operate on these attributes.

2. Encapsulation:

The property of being self-contained unit is called ‘Encapsulation’.

The wrapping up of data and functions into a single unit (called class) called ‘Encapsulation’.

This insulation of the data from direct access by the program is called
‘data hiding.’

Encapsulation essentially deals with data hiding and providing the interface to the user of the object.

Complexity of the system is encapsulated from end user to improve usability using simple interface.

3. Polymorphism

Ability to take more than one form is called ‘polymorphism’. In other words, one command may invoke different implementations for different objects.

For Example, Consider the operation of addition, For two numbers. The operation will generate a sum. If the operands are strings, then the operation would produce a third string by concatenation.

Polymorphism
/\
/   \

/      \

CompileTime                RunTime
/\                              |

/   \                            |

/      \                          |

Function     Operator       Virtual functions

Overloading    Overloading

4. Inheritance

It is a property of class hierarchy whereby each derived class inherits attributes and methods of its base class.

It provides code reusability and extensibility.

It defines relationship between classes and share common structure and behavior.