7
Reply

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

Vivek Bansod

Jan 08, 2016
2.3k
0

    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.

    Gaurav Mishra
    May 23, 2016
    1

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

    Vivek Bansod
    January 08, 2016
    1

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

    praveen chauhan
    August 03, 2016
    0

    No

    praveen chauhan
    August 03, 2016
    0

    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

    Prakash Tripathi
    April 09, 2016
    0

    it is possible if you use the method as Abstract method

    Amit Parmar
    March 11, 2016
    0

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

    Ravi Kumar
    January 12, 2016
    0