static void Main(string[] args) { Parent p1 = new child1();//upcasting /* upcasting is done so that one parent obj can hold reference of any child classs obj, * so we dont need to create more than one variable to hold references of more than one child class */ p1.Message(); //variable of parent can hold ref of child class objects but reverse is not true //In overriding, system looks at the refernce type of p Parent p2 = new child2(); //In hiding, system looks only at the type of p p2.Message(); //child2 obj = p1;//Not possible ,child class obj cant hold ref of parent class child1 obj = (child1)p1;//downcasting obj.Message(); p1 = new ChildChild2(); p1.Message(); }
static void Main(string[] args) { baseclass[] bc = new baseclass[6]; bc[0] = new child1(); bc[1] = new child1(); bc[2] = new child1(); bc[3] = new child2(); bc[4] = new child2(); bc[5] = new child2(); for (int i = 0; i < 6; i++) { bc[i].method1("Subham"); } }