Commonly asked interview questions on inheritance I have summed up all the inheritance control flow related concepts generally asked during O O P S technical interview. M ore or les s, if you unders tand the below example, then you will be able to ans wer any ques tions as ked by interviewers on flow of c ontrols between the bas e c las s and the derived c las s.
For this example, Let us consider the following class which we'll us e as a bas e c las s.. class Shapes s tatic Shapes () C ons ole.writeline("shape Static M ethod"); public Shapes ()
C ons ole.writeline("shapes c ons truc tor"); public void A rea() C ons ole.writeline("shape A rea"); public void C irc umferenc e() C ons ole.writeline("shape C irc umferenc e"); public virtual void s ides () C ons ole.writeline("shapes Sides "); ; N ow let us derive another c las s from this bas e c las s. c las s C irc le : Shapes s tatic C irc le() C ons ole.writeline("c irc le Static M ethod"); public C irc le() C ons ole.writeline("c irc le c ons truc tor"); public new void A rea() C ons ole.writeline("c irc le A rea");
public override void s ides () C ons ole.writeline("c irc le C irc umferenc e"); ; Now try this code out. Shapes a1 = new Shapes (); a1.a rea(); a1.c irc umferenc e(); a1.s ides (); // output: Shape Area Shape Circumference Shapes Sides Static method is c alled when the c las s is loaded into the memory. So, the moment you us e the c las s, s tatic method of that c las s is c alled and s o, Static method c all is before constructor method call. A lso, static method is called only once during your c ode exec ution. Let us now try us ing an objec t of parent c las s and as s ign it to the ins tanc e of derived c las s Shapes a2 = new C irc le(); a2.a rea(); a2.c irc umferenc e();
a2.s ides (); // output: Circle Static Method Circle constructor Shape Area Shape Circumference Circle Sides H ere, we s ee that the c ompiler c alls the s tatic method of bas e class after calling static method of derived class. However execution of the constructor method is exactly reverse. We have an objec t of type Shapes, but it referenc es an objec t of type Circle. T hat is why, the bas e c las s c ons truc tor gets c alled firs t, followed by the derived c las s c ons truc tor. N ow we c all Area () and find the method that's exec uted is the bas e c las s method. T his is bec aus e you have dec lared objec t to be of the bas e type whic h is Shapes in this c as e. Sinc e there is no overridden C ircumference method in derived class, when we call C ircumference (), base class method is called.now, when we call sides () method, we find that the derived class method got called. T his is bec aus e in the bas e c las s 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. C irc le a2 = new C irc le(); a2.a rea(); a2.c irc umferenc e(); a2.s ides (); // output: Circle Static Method
Circle constructor Circle Area Shape Circumference Circle Sides A s explained above, static methods and constructor were called as expected. Since we have provided a new implementation to area () method of bas e c las s in derived c las s, area () method of derived class gets called. Since there is no C ircumference method in derived class, C ircumference () method of base class gets c alled. T he fac t that we c ould invoke the C irc umferenc e () method of bas e c las s is proof of inheritanc e in C #. Now let us overload circumference method of base class and make original C irc umferenc e() method as virtual public virtual void C irc umferenc e() C ons ole.writeline("shape C irc umferenc e"); public void C irc umferenc e(int radius ) C onsole.writeline("shape C ircumference radius is "+radius ); Now run this code out. Shapes a1 = new Shapes (); a1.c irc umferenc e(); a1.c irc umferenc e(8 ); // output
Shape Circumference Shape Circumference radius is 8 O kay, that went fine as expec ted. N ow let's override Circumference func tion in the derived c las s as s hown below: public override void C irc umferenc e() C ons ole.writeline("c irc le C irc umferenc e"); public new void C irc umferenc e(int radius ) C onsole.writeline("c ircle C ircumference radious is "+radius ); N ow let's try this c ode out. Circle a1 = new C irc le(); a1.c irc umferenc e(); a1.c irc umferenc e(8 ); // output Circle Static Method Circle Circumference Circle Circumference radius is 8 Well, that went fine too. T hus if you have overloaded methods, you c an mark s ome of them as virtual and override them in the derived class or create an entirely new implementation in derived c las s. N ow, it will be very eas y to unders tand the output of the below
c ode. Shapes a1 = new C irc le(); a1.c irc umferenc e(); a1.c irc umferenc e(8 ); // output Circle Static Method Circle constructor Circle Circumference Shapes Circumference radius is 8 Before we end, let s see some stuff on overloaded constructors too. C reate an overloaded c ons truc tor in bas e c las s as s hown below protec ted int _s ides ; public Shapes () _s ides = 5 ; C ons ole.writeline("shapes c ons truc tor"); public Shapes (int isides ) _s ides = isides ; N ow let us modify our c hild c las s c ons truc tor as s hown below: public C irc le() C ons ole.writeline("c irc le c ons truc tor");
C ons ole.writeline(_s ides ); N ow let s try running this c ode Circle a1 = new C irc le(); // C irc le a1 = new C irc le(8 ); C ompile time error output: Circle Static Method Circle constructor 5 T he bas e c las s has two overloaded c ons truc tors. O ne that takes zero arguments and one that takes an int parameter. I n the derived class we only have the zero argument constructors. C ons truc tors are not inherited by derived c las s es. T hus we cannot instantiate a derived class object using the constructor that takes an int as parameter N ow take a look at this s ec ond derived c las s. class Square : Shapes //Here I am explicitly informing the compiler about the overload constructor of the base class to be called public Square(int s ide1 ): bas e(s ide1 ) C ons ole.writeline(_s ides ); //Here I am explicitly informing the compiler to first call
the overloaded constructor of the base class. public Square(int s ide2, int s ide1 ) : this (s ide1 ) C ons ole.writeline(s ide2 ); ; H ere we have two c ons truc tors, one that takes a s ingle int parameter and one that takes two int parameters. Now run the following code. Square du1 = new Square(6 ); //O utput 6 Square du2 = new Square(5, 7 ); //O utput 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