Top Liked Articles

1. Why we cannot see god

2. Science in Hinduism-Gravitational force and repulsive force

3. Does God exists everywhere

4. Is Goddess Durga the supreme universal mother

5. Science in Hinduism-Einstein Theory of relativity

6. Why hanuman is the only savior of this age- kaliyuga

7. Science in hinduism-Evolution in vishnu avatars

8. Science in Hinduism-Structure of universe and various planets

9. Why humans are naturally herbivorous

10. Proof of Lord krishna existence-1

11. Vedas Quotes Against Present Caste System

12. Science in hinduism-Extraction-Contraction and creation of universe

13. Stories of radha krishna love

14. Legend of Ramayana across the world

15. Why scientists want to protect ram setu

16. Swastika-Most sacred symbol of all ancient civilization

17. Why gayatri mantra is the most powerful mantra

18. Historic examples against present caste based system

19. Why lord shiva is known as king of dance

20. Science in Hinduism-Large numbers and infinity
SubmitLinkURL Web Directory
Top Programming Sites
ExactSeek: Relevant Web Search


Janani Janma-bhoomi-scha Swargadapi Gariyasi(Mother and Motherland are Greater than Heaven')-Lord Rama

HomeBlogs

Display any content here, from text, images, to rich HTML. Use the close link to dismiss the box. Click the close box to dismiss it.

Commonly asked interview questions on inheritance


Posted By admin on Aug 09, 2012       185 Views         Latest Hinduism news

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

Download this article as PDFPDF files are not generated for latest articles.

Tags: Inheritance, base, derived, OOPS, static, method, overloading, constructors

Share this to your friends. One of your friend is waiting for your share.
Google Print Browser Favorite
  



If you can't see comments, then please hover here.



All content of this website(Except Blogs) is handled by an automated process and is subjected to errors.


Good Thoughts
What lies behind us and what lies before us are tiny matters compared to what lies within us
-Ralph Waldo Emerson

Flag Counter

Top Viewed Articles

1. Floating stones of Shri Ram Setu

2. Scientific explanation of Hindu cosmology and reincarnation

3. Is god female-Yes or No

4. How many Gods in Hinduism

5. Why all gods are born in india

6. Why Hindus greet namaste and its significance

7. Is lord ganesha God and worthy of worship

8. Science in Hinduism-Motion of earth around sun

9. Why cow is sacred to hindus

10. Science in Hinduism-Place value and Decimal number system

11. Why Idol worship in hinduism

12. Symbolism of story behind kumbh mela-world largest gathering on earth

13. Why Lord krishna had more than 16000 wives

14. Proving historicity of Krishna-Archaeological and astronomical evidences

15. Cheating and Deception by lord krishna

16. Why Animal worship in hinduism

17. Did Greeks worshipped lord krishna

18. Who invented snakes and ladders

19. Did Egyptians worshipped lord krishna

20. is Lord Kuber indian santa claus
Spiritual Blogs |  Poems |  Technical Blogs |  Spiritual News |  Faq
Spiritual Poems |  General Poems |  Bhagavad Gita |  Spiritual Wallpapers |  References |  Donate
Asp.Net |  C#.Net |  Silverlight |  Sql/T-Sql |  Ajax |  Javascript |  Jquery
Bollywood Videos |  Funny Videos |  Spiritual Videos
This Website is Designed, Developed and Maintained by Sarin Mall. Copyright@Mallstuffs.com