Showing posts with label OOPS. Show all posts
Showing posts with label OOPS. Show all posts

Tuesday, October 20, 2009

Downcasting in OOPS

Assinging the parent class to child class this is called downcasting.

public class Class1
{
public string getValue()
{
return "sd";
}
}

public class class3 : Class1
{
public string GetValue1()
{

class3 cl =(class3) new Class1();
return cl.getValue();


}
}

Upcasting in OOPS

assigning the child class to the parent class that is called the Upcasting


public abstract class Class1
{
public string getValue()
{
return "sd";
}
}

public class class3 : Class1
{
public string GetValue1()
{
Class1 SS =new class3();
return SS.getValue();
}
}

Wednesday, July 8, 2009

Tip of the day

Calling classes with out Instance

1) Creating static objects.
2) Making the modifier as a public in the base class method. then inherit in to the derived class. after call that method directly.
3) After inheriting base class to derived class. using base key we can get the all the methods.

Example.

All programmers knows the first option. let me explain another two scenoria with the appropriate example.


public class classA
{
public classA()
{

}
public string A()
{
return "s";
}
}

public class classB: classA
{
public string callWithoutInstance()
{
//Inherite the base class then directly call the public method
return A();
}
public string callWithoutInstanceBase()
{
//Inherite the base class then call the public method through base class.
return base.A();
}
}

Encapsulation

Definition:

It's one of the basic concept in the object oriented software development. Wrapping up of a data in to a single unit is called encapsulation. It's basically hiding the objects.

Example:
Class

Polymorphism

Definition:

Polymorphism is a Greek word, it means more than one form in a single unit. it's one of the basic concepts in the object oriented programming language. specifically it allow variable to refer more than one objects.

Virtual methods allows to express the polymorphism concept.

They are two types of polymorphism. The types are as follows.

1) Compile time polymorphism
2) Run time polymorphism

Compile time polymorphism

Define:

Same member signature different type of arguments. it will decide the member in the compile time itself

Example:

1)Function overloading.
2)operator overloading.

Runtime Polymorphism.

This will decide the method at runtime.

Example:

Inheritance
Virtual polymorphism


Tuesday, July 7, 2009

Copy Constructor

Define:

Constructor will initialize only one at a time while coping the objects. New instance will not create. it will refer the existing class.

Copy constructor will not provide c# directly. But we can copy the objects using methods.

let me explain further with example.

public string var1 = string.Empty;
public string var2 = string.Empty;

//Copy Constructor
public CopyConstructor(CopyConstructor cpyCons)
{
var1 = cpyCons.var1;
var2 = cpyCons.var2;
}
//Instance of constructor
public CopyConstructor()
{
this.var1 = "s";
this.var2 = "s1";
}

CopyConstructor ss = new CopyConstructor();

CopyConstructor ss1 = new CopyConstructor(ss); //Coping the class using copy constructor.

Static Constructor in C#

Constructor:
Object will be created when it is declared or dynamically constructed on the heap through new key word

Static Constructor:

1) Static constructor initialize only one at a time.
2) No parameter for the method.
3) Access modifier are not allowed for the static constrcutor.
4) Only static datamember will be used in the static member.


Thursday, September 20, 2007

oops concepts overloading(C#)

Definition:Same member different arguments.

public class over
{
public over()
{

}
public void getvalue(int id, int id1)
{
HttpContext.Current.Response.Write(id);
HttpContext.Current.Response.Write(id1);
}
public void getvalue(string id, string empname)
{
HttpContext.Current.Response.Write(id);
HttpContext.Current.Response.Write(empname);
}
}

out put

over getvalue1 = new over();
getvalue1.getvalue("bala", "bala1"); //This is for string
getvalue1.getvalue(1,2); //This is for integer

Oops Concepts(Overriding in C#)

Oops Concepts(Overriding in C#)

namespace testbala
{
public class test
{
public test()
{
}
public virtual void getvalue()
{
HttpContext.Current.Response.Write("bala");
}
}
public class test1 : test
{
public test1()
{
}
public override void getvalue()
{
HttpContext.Current.Response.Write("bala1");
}
}
}

Output

test getvalue = new test1();

getvalue.getvalue();

Result:bala1

Note:When ever going to use shadowing and overriding you should mention "Virtual" in parent class.

Oops Concepts(Shadowing in C#)

Title:How to use Shoadowing in c#

namespace testbala
{
public class test
{
public test()
{
}
public virtual void getvalue()
{
HttpContext.Current.Response.Write("bala");
}
}
public class test1 : test
{
public test1()
{
}
public new void getvalue()
{
HttpContext.Current.Response.Write("bala1");
}
}
}

Output


test getvalue = new test1();
getvalue.getvalue();

Result:bala

Note:When ever going to use shadowing and overriding you should mention "Virtual" in parent class.