Is this possible? Class A{void show(();} Class B: A {void show();} A obja = new A A objaa = new B() B objb - New B(); B objbb = New A();
Vivek Bansod
B objbb = new A(); this will give compile type error because parent can not be assigned to child. Casting will ignore compile time error but will throw run time error.
Yes, it is allowed and can be used while using method overriding suing virtual keyword in base class and override in derived class
A objaa = new B(); will give compile error. Because you can not make parent object with child class instance.
No
Your question has two parts. Let's take them one by one. 1. Yes, It's possible to have same method name in base and extended class when using virtual/override as following; class A{public virtual void Show(){ }}class B: A{public override void Show(){ }}2. A obja = new A(); //CorrectA objaa = new B(); //Correct, Parent can hold the reference of child.B objb = new B(); //Correct//B objbb = new A(); //Compile type error as parent instance cann't be assigned to child.B objbb = (B) new A(); //Explicit type casting supresses compile time error but throws run time error
it is possible if you use the method as Abstract method
I think, B objbb = New A(); will cause the error. Type A cannot implicitly convert to B which is need to be explicitly cast.