Vivek Bansod
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();
By Vivek Bansod in C# on Jan 08 2016
  • Gaurav Mishra
    May, 2016 23

    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.

    • 2
  • Vivek Bansod
    Jan, 2016 8

    Yes, it is allowed and can be used while using method overriding suing virtual keyword in base class and override in derived class

    • 1
  • praveen chauhan
    Aug, 2016 3

    A objaa = new B(); will give compile error. Because you can not make parent object with child class instance.

    • 0
  • praveen chauhan
    Aug, 2016 3

    No

    • 0
  • Prakash Tripathi
    Apr, 2016 9

    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

    • 0
  • Amit Parmar
    Mar, 2016 11

    it is possible if you use the method as Abstract method

    • 0
  • Ravi Kumar
    Jan, 2016 12

    I think, B objbb = New A(); will cause the error. Type A cannot implicitly convert to B which is need to be explicitly cast.

    • 0


Most Popular Job Functions


MOST LIKED QUESTIONS