Click here to hide categories Click here to show left categories

User: Home          welcome : Guest          Log In / Register here     




QuestPond: Interview Questions and Answers covering basics of .NET (Object Oriented Programming) OOP

How is inheritance implemented in .NET?

Inheritance is implemented by using the “:” symbol.

Below is a simple code snippet where we have “Customer” class which is the parent class. We have then created a child class called as “CustomerDiscount” which inherits all the properties and adds a “Discount” property.

class Customer

{

public string customerName;

public string customerCode;

}

 

class CustomerDiscount : Customer

{

public double Discount;

}

How can we implement static polymorphism?

Static polymorphism is implemented by using method overloading. In compile time itself we come to know if there are mismatches. Below code snippet shows how method overloading is implemented. The add method can take either 2 inputs or 3 inputs.

Depending on the number of inputs the addition logic is executed.

// add method with 2 inputs.

objmaths.add(1,2);

// add method with 3 inputs.

objmaths.add(1,2,3);

How can we implement dynamic polymorphism?

Dynamic polymorphism is implemented by using overriding and virtual keyword.

Below is a simple code snippet which has three classes, Customer class is the parent class.CustomerDiscount10Percent and CustomerDiscount20Percent are child classes.

Customer parent class has a discount function which returns zero discounts. This function is defined as virtual and then overridden by both the child classes with 10 and 20% discount.

class Customer

{

public string customerName;

public string customerCode;

public  virtual int Discount()

{

return 0;

}

}

 

class CustomerDiscount10Percent : Customer

{

public  override int Discount()

{

return 10;

}

 

}

 

class CustomerDiscount20Percent : Customer

{

public  override int Discount()

{

return 20;

}

 

}


Now on the client side on the fly your parent object can point to any child classes and invoke the child implementation accordingly. This is called as dynamic polymorphism; the parent object can point to any of the child objects and invoke the child function dynamically.

Customer obj;

obj = new CustomerDiscount10Percent();

obj = new CustomerDiscount20Percent();


See following video on Aggregation, Association and Composition: -

Click and see here for ShivPrasad Koirala's Questpond tutorials.












Helpful Website Url
http://questpond.com/
Share this article   |    Print    |    Article read by 3656 times
Author:
Shivprasad koirala Koirala
I am a Microsoft MVP for ASP/ASP.NET and currently a CEO of a small E-learning company in India. We are very much active in making training videos , writing books and corporate trainings. Do visit my site http://www.questpond.com for .NET, C# , design pattern , WCF , Silverlight , LINQ , ASP.NET , ADO.NET , Sharepoint , UML , SQL Server training and Interview questions and answers
Related Articles:
Related Interview Questions: No related interview question