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();
}
}
}
Discussion:
28 comments Page 3 of 3.
Harsh said:
1 decade ago
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.
Since no access modifiers is specified in derive class, it is private.
So it is accesible, so base class method will be called.
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
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
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.
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.
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" + " ");
}
}
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" + " ");
}
}
Mahdi said:
1 decade ago
Of course, the output should be "Derived2 class".
Shanal said:
1 decade ago
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.
Prashant said:
1 decade ago
Output should be "Derived2 class" because there is no base.fun() in program to calling the baseclass.
Vikas said:
1 decade ago
I m not understanding the output. Since there is new keyword,Output should be "Derived2 class".
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers