polymorphism
-
Defination: "Through inheritance, a class can be used as more than one type; it can be used as its own type, any base types, or any interface type if it implements interfaces. This is called polymorphism". But does it mean changing a behaviour of a method in a drieved class also means polymorphism. e.g public class BaseClass { public virtual void DoWork() { } public virtual int WorkProperty { get { return 0; } } } public class DerivedClass : BaseClass { public override void DoWork() { } public override int WorkProperty { get { return 0; } } }
-
Defination: "Through inheritance, a class can be used as more than one type; it can be used as its own type, any base types, or any interface type if it implements interfaces. This is called polymorphism". But does it mean changing a behaviour of a method in a drieved class also means polymorphism. e.g public class BaseClass { public virtual void DoWork() { } public virtual int WorkProperty { get { return 0; } } } public class DerivedClass : BaseClass { public override void DoWork() { } public override int WorkProperty { get { return 0; } } }
-
This is indeed a case of polymorphism based on the definition here.
The funniest thing about this particular signature is that by the time you realise it doesn't say anything it's too late to stop reading it.
-
Kind of, functional pointers (delegates in .NET) can achieve the same results as polymorphism in non-oo languages but by the strictest definition you need inheritance.
Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. A man said to the universe: "Sir I exist!" "However," replied the universe, "The fact has not created in me A sense of obligation." --Stephen Crane
-
Defination: "Through inheritance, a class can be used as more than one type; it can be used as its own type, any base types, or any interface type if it implements interfaces. This is called polymorphism". But does it mean changing a behaviour of a method in a drieved class also means polymorphism. e.g public class BaseClass { public virtual void DoWork() { } public virtual int WorkProperty { get { return 0; } } } public class DerivedClass : BaseClass { public override void DoWork() { } public override int WorkProperty { get { return 0; } } }
netJP12L wrote:
ut does it mean changing a behaviour of a method in a drieved class also means polymorphism
Yes but not necessarily: The base class's method is the one that is called until it is overridden in a sub class. If you need to define a method that you must change, the method must be
abstract
(as must the class in c#).Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
-
netJP12L wrote:
ut does it mean changing a behaviour of a method in a drieved class also means polymorphism
Yes but not necessarily: The base class's method is the one that is called until it is overridden in a sub class. If you need to define a method that you must change, the method must be
abstract
(as must the class in c#).Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.
-
so why did the polymorphism includes the behaviour word in its definition. It only talks about types. I think behaviour and type is two different things.
Behaviour and type are two things, otherwise we'd call them both behaviour or both type [edit] however, types exhibit behaviours. I think there has been some language based confusion here to clarify:
class A
{
public virtual void Write()
{
Console.Writeline ("Base Write Method");
}
}class B : A
{
public override void Write()
{
Console.Writeline ("Class B's Write Method");
}
}class C : A
{}
class Foo
{
new A().Write(); // Outputs "Base Write Method"
new B().Write(); // Outputs "Class B's Write Method" as base method is overridden
new C().Write(); // Outputs "Base Write Method" as the base method isn't overridden
}As per my original post, you can override the default behaviour, but it isn't necessary.
Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.