Up casting is a cast towards its super class or parent class.
using System;
class Person
{
public string name;
}
class Employee : Person
{
public string companyName;
}
class Program
{
static void Main(string[] args)
{
Employee e = new Employee();
e.name = "java2s.com";
Person p = e;
Console.WriteLine(e.name);
Console.WriteLine(p.name);
}
}
The output:
java2s.com
java2s.com
Down cast happens when casting to subclass or child class.
Down cast needs an explicit cast.
using System;
class Person
{
public string name;
}
class Employee : Person
{
public string companyName;
}
class Program
{
static void Main(string[] args)
{
Person p = new Employee();
Employee e = (Employee)p;
}
}
java2s.com | Contact Us | Privacy Policy |
Copyright 2009 - 12 Demo Source and Support. All rights reserved. |
All other trademarks are property of their respective owners. |