Showing posts with label oops. Show all posts
Showing posts with label oops. Show all posts

Saturday, July 20, 2013

Example Late binding and early binding in .net | Difference between late and early binding in c#.net

1. Early binding

Early  Binding portrays that compiler knows what sort of object it is. (like intellisense which we get while writing the program here we see all the object methods know by the .net)

For example.

if we create object of any class and call any methods of that class that is early binding.

a)
 class add
{
  int addmethod(int z, int y)
 {
    return z+y;
 }

 void main()
 { 
  int value = add.addmethod(5,5);
 }

}

b) Function overloading is example for early binding.


2. late binding

Late Binding portrays that compiler does not realize what sort of object it is, what are all the methods and properties it holds. You need to declare it as an object, later you need get the type of the object, methods that are archived in it. Everything will be known at the run time. 

Example :

Function overriding is example for late binding.

Sunday, June 16, 2013

Struct and Class Example | Difference Between Struct and Class

Structs and Class:

struct class
1 It is value type It is Reference Type
2 Default access specifier is Public Default access specifier is Private
3 It only has Constructor It has both Constructor and Destructor
4 Struct has values residing on Stack  Class have Object values residing on Heap and it has references to the objects on stack
5 do not support Inheritance It Supports Inheritance

Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication3
{
    struct emp
    {
        public int empid;
        public string empname;
    }

    class dept
    {
        public int deptid;
        public string deptname;
    }

    class Program
    {
        static void Main(string[] args)
        {
            emp e = new emp();
            emp e2 = e;

            e.empid = 1;
            e.empname = "chandan";

            e2.empid = 2;
            e2.empname = "Rahul";

            Console.WriteLine("ID:{0} and Name:{1}", e.empid, e.empname);
            Console.WriteLine("ID2:{0} and Name2:{1}", e2.empid, e2.empname);

            dept d = new dept();
            dept d2 = d;

            d.deptid = 100;
            d.deptname = "Accounts";

            d2.deptid = 101;
            d2.deptname = "IT";

            Console.WriteLine("ID:{0} and Name:{1}", d.deptid, d.deptname);
            Console.WriteLine("ID2:{0} and Name2:{1}", d2.deptid, d2.deptname);

            Console.ReadLine();
        }
    }
}

Result :

class object referring to the same values whereas struct has new values assigned


Tuesday, May 7, 2013

Overview of Inheritance in asp.net with example

What is it?
Inheritance is Object oriented programming feature.
Its is used to inherit the properties or methods of parent class into the child class, so that it can access the common method and properties present in the parent class.

Types of Inheritance:
1. Multiple
2. Multi Level
3. Single Inheritance
4. Hierarchical Inheritance
5. Hybrid Inheritance

Description:
Suppose we have a separate class for car, bike, bus and we defined accelerate,speed, color method inside all the class.
This would be very time consuming and it will also be difficult to manage when some changes has to be done in the methods.

Instead of rewriting this methods again and again i will take all the common method like accelerate,speed,color inside one separate class and will inherit this in another class like car,bike etc.

Thus if any changes has to be done in future, then just in one of class we will have to do the changes and also it will make our code more concise and clear.

Basic Syntax :
class a
{
//Common method
}

class b : a
{
//accessing common methods
}


class  c : a
{
//accessing common methods
}


Example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Inheritance : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        result r1 = new result();
        result2 r2 = new result2();
        result r3 = new result2();

        int i = r1.AreaREC();
        Response.Write("<script language='javascript'>alert('Value from base class Method: "+ i + "');</script>");
     
        int i1 = r2.AreaREC();
        int i2 = r2.AreaSq();
        Response.Write("<script language='javascript'>alert('Value from Base class method and Derived class method "+ i1 + " and " + i2 + "');</script>");

        int i3 = r3.AreaREC();
        Response.Write("<script language='javascript'>alert('value from base class Method: " + i3 + "');</script>");
    }
}

public class result
{
    public int AreaREC()
    {
        return 100;
    }
}

public class result2 : result
{
    public int AreaSq()
    {
        return 150;
    }
}



Monday, May 6, 2013

Polymorphism in asp.net

In this post i will show how Polymorphism (OOPS) is implemented in asp.net with the help of an example:

Polymorphism : It states that the same method names exhibit different behaviors according to the condition applied.

Types of Polymorphism:
1. Compile Time polymorphism (Method Overloading)
- In this we have same method name but the no of arguments can be different.

2. Run time polymorphism (Method Overriding)
- In this we skip calling the base method and instead call the derived method which is of the same name as of the base class.

Example Compile Time polymorphism :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class polymorphism : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //Over loading
        int val = add(10);
        Response.Write("<Script language='Javascript'>alert('Result: " + val + "');</Script>");
        int val1 = add(10, 20);
        Response.Write("<Script language='Javascript'>alert('Result: " + val1 + "');</Script>");
        int val2 = add(10, 20, 30);
        Response.Write("<Script language='Javascript'>alert('Result: " + val2 + "');</Script>");
    }

    public int add(int x)
    {
        return x;
    }

    public int add(int x, int y)
    {
        return x + y;
    }

    public int add(int x, int y, int z)
    {
        return x + y + z;
    }
}

Example Run Time polymorphism :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class polymorphism : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
         //Overridding
        displayBaseMessage d1 = new displayMessage();
        string s = d1.messg();
        Response.Write("<Script language='Javascript'>alert('Result: " + s + "');</Script>");
    }
}

public class displayBaseMessage
{
    public virtual string messg()
    {
        string s = "From Base class";
        return s;
    }
}
public class displayMessage : displayBaseMessage
{
    public override string messg()
    {
        string s = "From Derived class";
        return s;
    }
}


Friday, March 29, 2013

Constructors

Constructors are the methods in a class. The name of the constructors is same as the class name. Whenever we create object of class, constructor will get called.

Syntax :

class shape
{
  public shape()
  {
      //code
   }
}

shape s = new shape() --> shape constructor will get called here.

Types of constructor :

1. Default constructor:

This type of constructor don't have any parameter in there method.

example : 


class shape
{
  public shape() //--> no parameter
  {
      //code
   }
}


2. Parameterized Constructor:

This type of constructor can have parameters in there method.

example : 


class shape
{
  public shape(int width) //--> parameter constructor
  {
      //code
   }
}

3. Copy constructor :

In this type of constructor a object of a class is copied to the another object of the same class

example:



4. Constructor Overloading

Constructor overloading  include both default and Parameterized constructor. Here the  constructor can be with parameters or without parameters.

when we create object if we pass parameter value while creating the object that constructor  with that number of parameter will get called.

Example:
class shape
{
 public shape()
}

public shape(int x)
}

public shape(int x, int y)
}

}

class main
{
shape s = new shape(10) // this will call the constructor with one parameter.
}

5. Static constructor

this type of constructor gets called before the object creation.

example:

class shape
{
 static shape()
 {

 }
}

Now this method gets executed before the object creation of the class.