as in run time if you choose the reference of base class and the object with new keyword is of child .... and the method u declared in the base class is virtual or abstract , and u have derived it in child class... now the instance u are generating is of type child but the reference is of parent ... so compiler is gonna look parent for the method u defined and then in child the particular method u have defined... it' not easy for compiler for to do so ...so what happens is that at run time which function is to be chosen is decided when the object is passed... this is run time polymorphism
BaseClass bObj = New ChildClass() bObj.commonMethodName
1> If Base method 'virtual'(Or 'override' in case on multi level inheritance) and child method 'override' -> child method executes 2> If Base method is not 'virtual'(Or 'override' in case on multi level inheritance) and child method 'new'(optional, vs prompt) -> base method executes (Also called method hiding)
Object reference not set to an instance of an object. after execting im getting object reference please help
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections;
namespace ConsoleApplication2 {
class Program { static void Main(string[] args) {
employee[] e1 = new employee[4]; e1[0] = new employee(); e1[1] = new parttimemeployee(); e1[2] = new fulltimeemployee();
foreach (employee e in e1) { e.add();
} Console.ReadLine();
}
} } class employee { public string firstname="sai"; public string lastname="kumar"; public virtual void add() { Console.WriteLine("Base class method called"); } }
class fulltimeemployee : employee { public new void add() { Console.WriteLine(firstname+lastname); }
}
class parttimemeployee : employee { public override void add() { Console.WriteLine("partime employee called"); }
public class PartTimeEmployee : Employee { public override void PartTimeEmployee() { Console.WriteLine(FirstName + " " + LastName + "- PartTimeEmployee"); } } public class FullTimeEmployee : Employee { public override void FullTimeEmployee() { Console.WriteLine(FirstName + " " + LastName + "- FullTimeEmployee"); } }
public class TemporaryTimeEmployee : Employee { public override void TemporaryTimeEmployee() { Console.WriteLine(FirstName + " " + LastName + "- TemporaryTimeEmployee"); } }
class Program { static void Main(string[] args) { //Employee E = new Employee(); //E.PrintFullName(); Employee[] employees = new Employee[4]; employees[0] = new Employee(); employees[1] = new PartTimeEmployee(); employees[2] = new FullTimeEmployee(); employees[3] = new TemporaryTimeEmployee();
foreach (Employee e in employees) { e.PrintFullName(); } } }
There are no methods like PartTimeEmployee(),FullTimeEmployee(),TemporaryTimeEmployee() in base class to override you might have confused while writing..change the method name to PrintFullName() in all those three places public class PartTimeEmployee : Employee { public override void PrintFullName() { Console.WriteLine(FirstName + " " + LastName + "- PartTimeEmployee"); } } public class FullTimeEmployee : Employee { public override void PrintFullName() { Console.WriteLine(FirstName + " " + LastName + "- FullTimeEmployee"); } }
public class TemporaryTimeEmployee : Employee { public override void PrintFullName() { Console.WriteLine(FirstName + " " + LastName + "- TemporaryTimeEmployee"); } }
Hi venkat , I want clear doubt about method hiding and method overriding..Is it correct that method hiding comes in inheritance concept and method overriding comes in polymorphism.please share the answer.
Hello Venkat,
ReplyDeleteI'm bit confused between new(shadow) and override keyword.Kindly explain what's the difference and when to use these.
Thanks,
Rohan
Got my query resolved...
DeleteThe answer is in your next video.
Thanks...
Hi Sir,
ReplyDeleteThere is a confusion. I have heard that there are two type of polymorphim.
1.Compile Time
2.Runtime
I think the above video explains the runtime polymorphism.
Kindly explain compile time polymorphism.
I think this is simplier way to explain polymorphism:
ReplyDeleteEmployee FTE = new FullTimeEmployee();
FTE.PrintFullName();
Employee E = new Employee();
E.PrintFullName();
what are the uses of static key word and how should i use ?
ReplyDeleteHi Venkat,
ReplyDeleteI have one confusion about run time polymorphism. How one can say that overriding of method happens at run time and overloading at compile time.
Thanks
Rakesh
as in run time if you choose the reference of base class and the object with new keyword is of child .... and the method u declared in the base class is virtual or abstract , and u have derived it in child class... now the instance u are generating is of type child but the reference is of parent ... so compiler is gonna look parent for the method u defined and then in child the particular method u have defined... it' not easy for compiler for to do so ...so what happens is that at run time which function is to be chosen is decided when the object is passed... this is run time polymorphism
Deletecontd.
ReplyDeleteBaseClass bObj = New ChildClass()
bObj.commonMethodName
1> If Base method 'virtual'(Or 'override' in case on multi level inheritance) and child method 'override' -> child method executes
2> If Base method is not 'virtual'(Or 'override' in case on multi level inheritance) and child method 'new'(optional, vs prompt) -> base method executes (Also called method hiding)
Object reference not set to an instance of an object.
ReplyDeleteafter execting im getting object reference please help
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
employee[] e1 = new employee[4];
e1[0] = new employee();
e1[1] = new parttimemeployee();
e1[2] = new fulltimeemployee();
foreach (employee e in e1)
{
e.add();
}
Console.ReadLine();
}
}
}
class employee
{
public string firstname="sai";
public string lastname="kumar";
public virtual void add()
{
Console.WriteLine("Base class method called");
}
}
class fulltimeemployee : employee
{
public new void add()
{
Console.WriteLine(firstname+lastname);
}
}
class parttimemeployee : employee
{
public override void add()
{
Console.WriteLine("partime employee called");
}
}
Replace like ...
Deleteforeach (employee emp in e1)
{
emp.add();
}
try this
Deleteemployee[] e1 = new employee[3];
e1[0] = new employee();
e1[1] = new parttimemeployee();
e1[2] = new fulltimeemployee()
"member names cannot be the same as their enclosing type"
ReplyDelete==========================================================
using System;
public class Employee
{
public string FirstName = "FN";
public string LastName = "LN";
public virtual void PrintFullName()
{
Console.WriteLine(FirstName + " " + LastName);
}
//public virtual void PartTimeEmployee()
//{
// Console.WriteLine(FirstName + " " + LastName + "- PartTimeEmployee");
//}
//public virtual void FullTimeEmployee()
//{
// Console.WriteLine(FirstName + " " + LastName + "- PartTimeEmployee");
//}
//public virtual void TemporaryTimeEmployee()
//{
// Console.WriteLine(FirstName + " " + LastName + "- TemporaryTimeEmployee");
//}
}
public class PartTimeEmployee : Employee
{
public override void PartTimeEmployee()
{
Console.WriteLine(FirstName + " " + LastName + "- PartTimeEmployee");
}
}
public class FullTimeEmployee : Employee
{
public override void FullTimeEmployee()
{
Console.WriteLine(FirstName + " " + LastName + "- FullTimeEmployee");
}
}
public class TemporaryTimeEmployee : Employee
{
public override void TemporaryTimeEmployee()
{
Console.WriteLine(FirstName + " " + LastName + "- TemporaryTimeEmployee");
}
}
class Program
{
static void Main(string[] args)
{
//Employee E = new Employee();
//E.PrintFullName();
Employee[] employees = new Employee[4];
employees[0] = new Employee();
employees[1] = new PartTimeEmployee();
employees[2] = new FullTimeEmployee();
employees[3] = new TemporaryTimeEmployee();
foreach (Employee e in employees)
{
e.PrintFullName();
}
}
}
There are no methods like PartTimeEmployee(),FullTimeEmployee(),TemporaryTimeEmployee() in base class to override you might have confused while writing..change the method name to PrintFullName() in all those three places
Deletepublic class PartTimeEmployee : Employee
{
public override void PrintFullName()
{
Console.WriteLine(FirstName + " " + LastName + "- PartTimeEmployee");
}
}
public class FullTimeEmployee : Employee
{
public override void PrintFullName()
{
Console.WriteLine(FirstName + " " + LastName + "- FullTimeEmployee");
}
}
public class TemporaryTimeEmployee : Employee
{
public override void PrintFullName()
{
Console.WriteLine(FirstName + " " + LastName + "- TemporaryTimeEmployee");
}
}
Hi venkat ,
ReplyDeleteI want clear doubt about method hiding and method overriding..Is it correct that method hiding comes in inheritance concept and method overriding comes in polymorphism.please share the answer.