C# Programming - Inheritance - Discussion

Discussion :: 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(); 
        } 
    } 
}

[A]. Base class
[B]. Derived1 class
[C]. Derived2 class
[D]. Base class Derived1 class
[E]. Base class Derived1 class Derived2 class

Answer: Option A

Explanation:

No answer description available for this question.

Shubhra said: (May 24, 2011)  
I did not understand the output.

The new keyword in function decelration? what would it do?

If new was not there then Derived2 should be answer?

If instead of Fun they were contructors then what?

Vikas said: (Jun 29, 2011)  
I m not understanding the output. Since there is new keyword,Output should be "Derived2 class".

Prashant said: (Jul 6, 2011)  
Output should be "Derived2 class" because there is no base.fun() in program to calling the baseclass.

Shanal said: (Aug 31, 2011)  
The 'new' keyword if use with derived class variable it hides the variable of same name in the base class and if same is the case with member func then the output should be derived2.

Mahdi said: (Sep 15, 2011)  
Of course, the output should be "Derived2 class".

Kajal said: (Oct 22, 2011)  
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" + " ");
}
}

Prathamesh said: (Nov 17, 2011)  
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.

Chandrashekar said: (Dec 28, 2011)  
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

Harsh said: (Aug 27, 2012)  
Default access modifiers for member function of class is private.

Since no access modifiers is specified in derive class, it is private.

So it is accesible, so base class method will be called.

Api said: (Sep 2, 2012)  
Thanks chandrashekar.

Mihi said: (Sep 15, 2012)  
Since in derived classes for functions no access specifiers specified so those will not inherit. The final method that should inherit is Base class fun(). The output is "Base class"

Srinivas said: (Nov 4, 2012)  
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.

Vapandian said: (Mar 4, 2013)  
This is simple. Because other func() are not having public, only base class having public func() ; so this is only executed. If you provide public to derived 2 it will execute.

Amit said: (Apr 16, 2013)  
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.

Ipsita said: (Nov 30, 2013)  
What is new here?

Rajat said: (Dec 29, 2013)  
Here the new keyword means derived class is calling the hidden function of base class.

Vidit Tyagi said: (Mar 12, 2014)  
We need to use virtual keyword in base then we can hide the functionality of base class method with the help of new keyword :).

Hemant Jakkani said: (Apr 30, 2014)  
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.

Vaibhav M Nalwad said: (Aug 10, 2014)  
When you add a new keyword for a property, variable, Method it is said to be Shadowing so the output would be "".

Rajesh K. said: (Oct 5, 2014)  
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".

Arvind said: (Nov 3, 2014)  
The answer should be Derived 2, the access specifier doesn't put any difference.

Sandeep Singh said: (Nov 24, 2014)  
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..

Vendi said: (Mar 25, 2015)  
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()"
}

Lingaswamy said: (Nov 10, 2015)  
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.

Ishwar said: (Apr 27, 2016)  
Virtual Keyword are required in base class.

Mohammad Adil said: (Jun 6, 2016)  
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.

Harika said: (May 15, 2021)  
Sealed key word stops the inheritance execution.

Post your comments here:

Name *:

Email   : (optional)

» Your comments will be displayed only after manual approval.