C# Programming - Inheritance - Discussion

Discussion Forum : Inheritance - General Questions (Q.No. 6)
6.
What will be the output of the C#.NET code snippet given below?
namespace IndiabixConsoleApplication
{ 
    class Baseclass
    { 
        public void fun()
        { 
            Console.Write("Base class" + " ");
        } 
    } 
    class Derived1: Baseclass
    { 
        new void fun()
        {
            Console.Write("Derived1 class" + " "); 
        } 
    } 
    class Derived2: Derived1
    { 
        new void fun()
        { 
            Console.Write("Derived2 class" + " ");
        }
    }
    class Program
    { 
        public static void Main(string[ ] args)
        { 
            Derived2 d = new Derived2(); 
            d.fun(); 
        } 
    } 
}
Base class
Derived1 class
Derived2 class
Base class Derived1 class
Base class Derived1 class Derived2 class
Answer: Option
Explanation:
No answer description is available. Let's discuss.
Discussion:
28 comments Page 1 of 3.

VENDI said:   1 decade ago
class A
{
public void Foo() { Console.WriteLine("A::Foo()"); }
}

class B : A
{
public new void Foo() { Console.WriteLine("B::Foo()"); } //make this function public.......else the base class function will be called
}

class Test
{
static void Main(string[] args)
{
A a;
B b;

a = new A();
b = new B();
a.Foo(); // output --> "A::Foo()"
b.Foo(); // output --> "B::Foo()"

a = new B();
a.Foo(); // output --> "A::Foo()"
}

Sandeep Singh said:   1 decade ago
Functions of class Derived1 and Derived2 are inaccessible due to its protection level i.e private (default access specifier). So it will access the function of Base Class because its public.

class Derived2: Derived1
{
Public new void fun()
{
Console.Write("Derived2 class" + " ");
}
}

Deriver2 Class will Execute.

class Derived1: Baseclass
{
Public new void fun()
{
Console.Write("Derived1 class" + " ");
}
}

Deriver1 Class will Execute..
(5)

Srinivas said:   1 decade ago
When ever if you have same method name in the parent (base) class and derived class, if you create an object for derived class then base class will be executed. If we want to execute both the methods to be executed we want to use 'virtual' key word in the base class and 'override key word in the derived class method.

Public virtual void fun1() in base class.

Public override void fun1() in derived class.

Rajesh k. said:   1 decade ago
New keyword : hides the members inherited from its parent class.

By default it will call its own class method , If we want to call method of parent class ,we can by base.<method_name>();

-Access modifier of method of Derived2 class is not mentioned i.e. it is private.

So, private methods are accessible from same class.

So, I think ans should be "Derived2".

Kajal said:   1 decade ago
Answer is Base class only.

Because in this case in dervied class access modifier is not given.if we use access modifier as public in dervied class the the output will be of dervied class.

Write:

class Derived2: Derived1
{
public new void fun()
{
Console.Write("Derived2 class" + " ");
}
}

Mohammad Adil said:   9 years ago
Output: Derived2.

Here the new keyword only hides the compile time error which showing the hiding of base class method by derived class method.

So here the output is Derived2 because Derived1 hides base class method and Derived2 hides Derived1 class method because they have the same signature.
(1)

Chandrashekar said:   1 decade ago
output : Base Class

Because in Derived1 and Derived2 there are no access modifier specified for fun().

If public is specified for fun() in Derived1 class then
output : Derived1

If public is specified for fun() in Derived1 and Derived2 class then
output : Derived2

Lingaswamy said:   10 years ago
In new key work use before void fun() then it will be hide of that fun() method.

If you want to invoke Derived2 class void fun() method, add virtual void fun() in Base class and add override void fun() in Derived2.

Hemant Jakkani said:   1 decade ago
Default access modifiers for member function of class is private.

Since no access modifiers is specified in derived class, it is private, So it is not accessible, so base class method will be called.

Prathamesh said:   1 decade ago
Output is Base class only.
Because, if we use new keyword then we have to use virtual keyword for the base class function.if we dont use virtual keyword den d.fun() by default call base class method.


Post your comments here:

Your comments will be displayed after verification.