Click here to check my latest exciting videos on youtube
Search Mallstuffs

Flag Counter
Spirituality, Knowledge and Entertainment


Locations of visitors to this page


Latest Articles


Move to top
Commonly asked interview questions on inheritance
Posted By Sarin on Aug 09, 2012     RSS Feeds     Latest Hinduism news
3253 Views

Commonly asked interview questions on inheritance
I have summed up all the inheritance control flow related concepts generally asked during OOPS technical interview. More or less, if you understand the below example, then you will be able to answer any questions asked by interviewers on flow of controls between the base class and the derived class.

For this example, Let us consider the following class which we'll use as a base class..
  
  
class Shapes
     {
       
static Shapes()
         {
           
Console.WriteLine("Shape Static Method");
        }
       
public Shapes()
         {
           
Console.WriteLine("Shapes constructor");
        }
       
public void Area()
         {
           
Console.WriteLine("Shape Area");
        }
       
public void Circumference()
         {
           
Console.WriteLine("Shape Circumference");
        }
       
public virtual void sides()
         {
           
Console.WriteLine("Shapes Sides");
        }
    };
  
Now let us derive another class from this base class.
  
  class Circle : Shapes
     {
       
static Circle()
         {
         
Console.WriteLine("Circle Static Method");
        }
       
public Circle()
         {
           
Console.WriteLine("Circle constructor");
        }
       
public new void Area()
         {
           
Console.WriteLine("Circle Area");
        }
       
public override void sides()
         {
           
Console.WriteLine("Circle Circumference");
        }
    };
  
Now try this code out.
  
Shapes a1 = new Shapes();
a1.Area();
a1.Circumference();
a1.sides();      
  
// output:
Shape Static Method
Shapes constructor
Shape Area
Shape Circumference
Shapes Sides

Static method is called when the class is loaded into the memory. So, the moment you use the class, static method of that class is called and so, Static method call is before constructor method call. Also, static method is called only once during your code execution.
  
Let us now try using an object of parent class and assign it to the instance of derived class


Shapes a2 = new Circle();
a2.Area();
a2.Circumference();
a2.sides();
  
// output:
Circle Static Method
Shape Static Method
Shapes constructor
Circle constructor
Shape Area
Shape Circumference
Circle Sides
  
Here, we see that the compiler calls the static method of base class after calling static method of derived class. However execution of the constructor method is exactly reverse. We have an object of type Shapes, but it references an object of type Circle. That is why, the base class constructor gets called first, followed by the derived class constructor. Now we call Area () and find the method that's executed is the base class method. This is because you have declared object to be of the base type which is Shapes in this case. Since there is no overridden Circumference method in derived class, when we call Circumference (), base class method is called.Now, when we call sides() method, we find that the derived class method got called. This is because in the base class the method is prototyped as public virtual void sides () and in the derived class we have overridden it by using  public  override  void  sides ().
Now try the following code out.
Circle a2 = new Circle();
            a2.Area();
            a2.Circumference();
            a2.sides();
  
// output:
Circle Static Method
Shape Static Method
Shapes constructor
Circle constructor
Circle Area
Shape Circumference
Circle Sides
  
As explained above, static methods and constructor were called as expected. Since we have provided a new implementation to area () method of base class in derived class, area () method of derived class gets called. Since there is no Circumference method in derived class, Circumference () method of base class gets called. The fact that we could invoke the Circumference () method of base class is proof of inheritance in C#.
Now let us overload circumference method of base class and make original Circumference() method as virtual
  
  public virtual void Circumference()
         {
           
Console.WriteLine("Shape Circumference");
        }
       
public void Circumference(int radius)
         {
           
Console.WriteLine("Shape Circumference radius is "+radius);
        }
Now run this code out.
Shapes a1 = new Shapes();
            a1.Circumference();
            a1.Circumference(8);

// output
Shape
Static Method
Shapes constructor
Shape Circumference
Shape Circumference radius is 8
  
Okay, that went fine as expected. Now let's override Circumference function in the derived class as shown below:
  
public override void Circumference()
         {
           
Console.WriteLine("Circle Circumference");
        }
public new void Circumference(int radius)
         {
           
Console.WriteLine("Circle Circumference radious is "+radius);
        }
  
Now let's try this code out.
  
Circle a1 = new Circle();
            a1.Circumference();
            a1.Circumference(8);
  
// output
Circle
Static Method
Shape Static Method
Shapes constructor
Circle Circumference
Circle Circumference radius is 8
  
Well, that went fine too. Thus if you have overloaded methods, you can mark some of them as virtual and override them in the derived class or create an entirely new implementation in derived class.  
Now, it will be very easy to understand the output of the below code.
  
Shapes a1 = new Circle();
a1.Circumference();
a1.Circumference(8);

// output
Circle
Static Method
Shape Static Method
Shapes constructor
Circle constructor
Circle Circumference
Shapes Circumference radius is 8
  
Before we end, let’s see some stuff on overloaded constructors too.  
Create an overloaded constructor in base class as shown below  

       
protected int _sides;
       
public Shapes()
         {
            _sides = 5;
           
Console.WriteLine("Shapes constructor");
        }
       
public Shapes(int iSides)
         {
            _sides = iSides;
        }
  
Now let us modify our child class constructor as shown below:
  
public Circle()
         {
           
Console.WriteLine("Circle constructor");
           
Console.WriteLine(_sides);
        }
  
Now let’s try running this code

Circle
 a1 = new Circle();
// Circle a1 = new Circle(8);  Compile time error
  
output:
Circle Static Method
Shape Static Method
Shapes constructor
Circle constructor
5
The base class has two overloaded constructors. One that takes zero arguments and one that takes an int parameter. In the derived class we only have the zero argument constructors. Constructors are not inherited by derived classes. Thus we cannot instantiate a derived class object using the constructor that takes an int as parameter
  
Now take a look at this second derived class.
  
  class Square : Shapes
     {
       
//Here I am explicitly informing the compiler about the overload constructor of the base class to be called
       
public Square(int side1): base(side1)
         {
           
Console.WriteLine(_sides);
        }
  
       
//Here  I am explicitly informing the compiler to first call the overloaded constructor of the base class.
       
public Square(int side2, int side1)
            :
this(side1)
         {
           
Console.WriteLine(side2);
        }
    };
  
Here we have two constructors, one that takes a single int parameter and one that takes two int parameters. Now run the following code.
  
Square du1 = new Square(6);
//Output
6

Square
 du2 = new Square(5, 7);
//Output
7
5

First code calls the base class constructor and set the protected _sides variable to 6. And then the derived class constructor prints this _sides variable onto the console.
Second code first calls the derived class double parameter constructor and print 7 onto the console.  Since we have made call to the derived class single parameter constructor method using
this object, this derived class constructor in turn calls the base class constructor and set the protected _sides variable to 5 and then the derived class constructor print this _sides variable onto the console.
  
  


here


Share this to your friends. One of your friend is waiting for your share.
Share on Google+ Share on Tumblr Print Browser Favorite
Related Articles
Displaying Image in silverlight Datagrid
Get set field, Properties,events or method using reflection
Changing CSS Properties of HTML element using JQuery
Commonly asked interview questions on inheritance

Post Comment