Showing posts with label Derived Class. Show all posts
Showing posts with label Derived Class. Show all posts

What is Upcasting and Downcasting in C# ?

Upcasting is basically an object creation mechanism by which we create objects by referring to their base class. We do this by replacing the sub-class name by the base-class name in the object definition. This particularly comes in handy when you know that a specialized object created will not necessarily use all the functions of a specialized sub-class has got to offer. So, replace a sub-class(inherited class) by a base-class and all you have done is Upcasting.


Class Inheritance

Classes can inherit from other classes. They can gain Public/Protected Variables and Functions of the parent class. The class then acts as a detailed version of the original parent class(also known as the base class). 
The code below shows the simplest of examples :

public class A
{
    //Define Parent Class
    public A() { }
}

public class B : A
{
    //Define Child Class
    public B() { }
}