Last post Feb 12, 2016 06:12 AM by Navin Dhananshan
Feb 11, 2016 10:35 AM|sudip_inn|LINK
some time we create instance like this way but what is the objective and when people create instance like this way
public class Parent1
public virtual void PopulateRecord()
int y = 0;
public class child : Parent1
public void PopulateRecord()
int x = 0;
Parent1 p = new child();
what is the meaning of creating instance this way parent class = new child class () ?
this approach has any name or term ?
when we invoke p.PopulateRecord(); function this way then parent class function
PopulateRecord is getting called but when we override PopulateRecord() in child class then
PopulateRecord() function getting called of child class. this concept is not very clear though it is very basic OOPS question.
please help me to understand this behavior. thanks
Feb 11, 2016 02:58 PM|Navin Dhananshan|LINK
One of the fundamental concepts of object oriented software development is polymorphism. The term polymorphism (from the Greek meaning "having multiple forms") in OO is
the characteristic of being able to assign a different meaning or usage to something in different contexts
An override method provides a new implementation of a member that is inherited from a base class. The method that is overridden by an override declaration is known as the
overridden base method. The overridden base method must have the same signature as the override method
for an example:
public class Employee
public virtual decimal CalculatePay()
public class SalesEmployee : Employee
public override decimal CalculatePay()
return basepay + salesbonus;
Employee employee1 = new SalesEmployee();
Employee employee2 = new AdminEmployee();
Employee employee3 = new xxxEmployee();
An important thing to notice here is - you don't have to know the exact type of the class you're using, just the base type, and you will get the right result. This is very useful in more complex systems as well.
Feb 11, 2016 06:09 PM|sudip_inn|LINK
Feb 12, 2016 06:12 AM|Navin Dhananshan|LINK
OK, There are many scenarios where it will be useful but I'll give you an ideal situation.
In my previous example assume I have a collection of employee which i'll add it into a list dynamically and i want to calculate the pay.
List<Employee> employee = new List<Employee>();
employee .Add(new SalesEmployee());
employee .Add(new AdminEmployee());
employee .Add(new xxxEmployee());
foreach (var item in employee)
now imagine if we don't have a base class and how can you achieve this.