C# Programming - Inheritance - Discussion
Discussion Forum : Inheritance - General Questions (Q.No. 14)
14.
Assume class B is inherited from class A. Which of the following statements is correct about construction of an object of class B?
Discussion:
6 comments Page 1 of 1.
Max said:
7 years ago
Class B is inherited from class A, so B is the base class?
And the constructor of the base class will be called first!
Therefore, the answer is [A].
And the constructor of the base class will be called first!
Therefore, the answer is [A].
Lakshmi said:
8 years ago
The printBase() method as protected beacuse protected is accessible with in the class and derived classes too.
But we are accessing out side the class so it is throwing compile time error.
But we are accessing out side the class so it is throwing compile time error.
Umang said:
8 years ago
Can somebody tell me why I am not able to call printBase() in below code?
public class Inheritance
{
static void Main(string[] args)
{
Base objBase= new Derived();
objBase.printBase();
Console.ReadKey();
}
}
public class Base
{
public Base()
{
Console.WriteLine("Hello From Base!");
}
protected void printBase()
{
Console.WriteLine("Hello from printBase!");
}
}
public class Derived : Base
{
public Derived()
{
Console.WriteLine("Hello from Derived!");
}
}
}
public class Inheritance
{
static void Main(string[] args)
{
Base objBase= new Derived();
objBase.printBase();
Console.ReadKey();
}
}
public class Base
{
public Base()
{
Console.WriteLine("Hello From Base!");
}
protected void printBase()
{
Console.WriteLine("Hello from printBase!");
}
}
public class Derived : Base
{
public Derived()
{
Console.WriteLine("Hello from Derived!");
}
}
}
Darshan H T said:
1 decade ago
Actually according to Question being asked. Ans: 'C' is correct. Why because while creating object for class B, only constructor of B will be called, cause class B is inherited by class A.
Is it right understanding?
Is it right understanding?
Fareeha said:
1 decade ago
I'm not getting the point " the constructor of class A will be called followed by constructor of class B.
Sagar verma said:
1 decade ago
Its depend on constructor is static or instance. If constructor is static then subclass constructor will execute first and if instance then superclass instance constructor will execute first.
Post your comments here:
Quick links
Quantitative Aptitude
Verbal (English)
Reasoning
Programming
Interview
Placement Papers