Help with this sample

  • Thread starter Thread starter Praveen
  • Start date Start date
P

Praveen

trying to learn plymorphism.
My sample is

public class Class1
{
public static void Main(string[] args)
{
Cls1 x = new Cls1();
Cls2 y = new Cls2();
Cls3 y = new Cls3();

Object[] coll = new Object[] {x,y,z};

foreach (Object obj in coll)
{
Console.WriteLine(obj.Rtn());
// here is the issue.
//How to get the return value of Rtn();
//I want to use the loop and the concept of polymorphism

}
}
}


public class Cls1
{
public string Rtn()
{
return "From Cls1";
}
}


public class Cls2
{
public string Rtn()
{
return "From Cls2";
}
}

public class Cls3
{
public string Rtn()
{
return "From Cls3";
}
}
 
In order to exhibit polymorphism, your classes Cls1, Cls2, Cls3 need to
all derive from a base class with your common method defined.

Declare an abstract base class with one method called Rtn(). Derive
your classes from this class and then during your foreach loop, call
the method on the base class rather than the concrete classes

eg

public abstract MyBaseClass
{
public abstract string Rtn();
}

public class Cls1 : MyBaseClass
{
public override string Rtn()
{
return "From class 1";
}
}


//repeat this for your other classes

when calling the method use

foreach (MyBaseClass c in coll)
{
Console.Writeln( c.Rtn() );
}

Hope this helps.

ps if you dont want to use an abstract class then you can just create a
concrete base class and mark the method as protected virtual
 
Hi,

Polymorphism exists in .NET via inheritance and interfaces. You haven't
illustrated the use of interfaces in your example, and you've barely
scratched the surface of inheritance. Here's a simple article that you may
want to read:

"Polymorphism in object-oriented programming"
http://en.wikipedia.org/wiki/Polymorphism_in_object-oriented_programming
Cls3 y = new Cls3();

Your code won't compile because the line above attempts to redefine "y".
foreach (Object obj in coll)
{
Console.WriteLine(obj.Rtn());

You can't call obj.Rtn() because obj is of the Type, "System.Object", which
doesn't provide a method named, "Rtn". You must cast "obj" to a Type that
has a method named, "Rtn":

foreach (object obj in coll)
{
if (obj is Cls1)
Console.WriteLine(((Cls1) obj).Rtn());
else if (obj is Cls2)
Console.WriteLine(((Cls2) obj).Rtn());
else if (obj is Cls3)
Console.WriteLine(((Cls3) obj).Rtn());
}

Obviously, this isn't very dynamic at all.

Since you'd like to late-bind to an instance of Cls1, Cls2 and Cls3 to call
"Rtn", a method which each of these classes has in common with the same
signature, you could use an interface:

interface IReturn
{
string Rtn();
}

class Cls1 : IReturn
{
public string Rtn() { return "From Cls1"; }
}

class Cls2 : IReturn
{
public string Rtn() { return "From Cls2"; }
}

....

And you can use the interface as such:

IReturn[] returningColl = new IReturn[] {
new Cls1(), new Cls2()
};

foreach (IReturn returningObject in returningColl)
Console.WriteLine(returningObject.Rtn());

--
Dave Sexton

Praveen said:
trying to learn plymorphism.
My sample is

public class Class1
{
public static void Main(string[] args)
{
Cls1 x = new Cls1();
Cls2 y = new Cls2();
Cls3 y = new Cls3();

Object[] coll = new Object[] {x,y,z};

foreach (Object obj in coll)
{
Console.WriteLine(obj.Rtn());
// here is the issue.
//How to get the return value of Rtn();
//I want to use the loop and the concept of polymorphism

}
}
}


public class Cls1
{
public string Rtn()
{
return "From Cls1";
}
}


public class Cls2
{
public string Rtn()
{
return "From Cls2";
}
}

public class Cls3
{
public string Rtn()
{
return "From Cls3";
}
}
 
Hi Praveen,

You cannot invoke the Rtn method from the variable of type object without
casting it to the appropriate class type.
But since you want to use polymorphism, you dont want to cast it.

polymorphism can be applied in this case, can be accomplished in the
following ways

--Derive Cls1, Cls2 and Cls3 from a common base type.
The base type should have a virtual method Rtn which you should override
in each of the classes Cls1, Cls2 amd Cls3.

Example:
class BaseType
{
protected virtual string Rtn()
{
return String.Empty;
}

}
class Cls1 : BaseType
{
protected override string Rtn()
{
return "From Cls1";;
}
}
Use BaseType[] instead of object[]

OR

--Create a particular Interface which defines the method Rtn.
Implement the Rtn method in each of the classes Cls1, Cls2 amd Cls3.


interface IBaseType
{
string Rtn();
}
class Cls1 : IBaseType
{
public string Rtn()
{
return "From Cls1";;
}
}
Use IBaseType[] instead of object[]

Regards,
Hameer Saleem
 
Hi Jeff,

I don't think that "events" have anything to do with "implementing"
polymorphism.
 
Hi Jeff,

I'll agree with you that delegates implement polymorphism since they can be
late-bound to their targets, but not events. Events must be part of an
interface, which is already polymorphic in nature. It's the delegates
registered with the events that are polymorphic, not the event itself.
 
Hi Dave.. This is how I see it. Events and Delegates are intertwined in
NET. In
general they are often used in tandom. Delegates are type safe pointers
to a
polymorphic method. Events are used to simplify the use of Delegates.
When
you fire an event you are calling one or more polymorphic methods. This
type of
semantic debate is important but clouds the concept that polymorphism is
not
limited to interfaces and abstract classes in .NET.

I am trying to say that you can implement polymorphic behaviour in .NET
using
events _and delegates_. To some, this idea is new and may be an "aha"
moment.
I apologize if I was not clear in my original post.

Regards,
Jeff
 
Hi Jeff,
When
you fire an event you are calling one or more polymorphic methods. This
type of
semantic debate is important but clouds the concept that polymorphism is
not
limited to interfaces and abstract classes in .NET.

I disagree.

My point is that delegates are providing the polymorphism, not the events.
Events are just a construct that aggregates delegates for private
invocation.

When you write, "you can implement polymorphic behaviour in .NET using
events _and delegates_.", I disagree that "events" have anything to do with
it. Delegates being added to an "event" are polymorphic regardless. Adding
delegates to an event does not provide any further polymorphic aspects to
the already late-bound delegates. An event itself, being part of a class
definition or interface, may be polymorphic in the sense that it's
implemented or inherited, just like a public property. Public properties
alone are not polymorphic and neither are events.

I think the key here is that Typed delegates derive from System.Delegate,
which is why delegates are polymorphic - inheritance. Events are not
objects, so they cannot be polymorphic.

To say that you can use events to implement polymorphism is like saying you
can use public properties to implement polymorphism, when in fact it's
actually inheritance and interfaces that are required for polymorphism, not
the class members.
 
Dave... I never said events themselves were polymorphic. I just
finished
saying that you could implement polymorphic behavior using events and
delegates. I added the "and delegates" in response to your post as I
thought
you had a valid point, that I was not being absolutely clear. Here is a
definition of Implement:

Main Entry: 2im·ple·ment
Pronunciation: -"ment
Function: transitive verb
1 : CARRY OUT, ACCOMPLISH; especially : to give practical effect to and
ensure of actual fulfillment by concrete measures
2 : to provide instruments or means of expression for

Are you arguing that events are not related to delegates? I think that
they are
very much related to delegates.

"Events use delegates to provide type-safe encapsulation of the methods
that
will be called when triggered. A delegate can encapsulate both Named
Methods and Anonymous Methods."

IMHO events and delegates are tightly related and together provide a
convenient set of instruments and means for polymorphic behavior in
..NET.
Sometimes I tire of these semantic debates. The concept that
polymorphism
can be accomplished beyond the classical interface and abstract class is
what
is important, not these arguments. I am begining to wonder if it is even
worth
the effort to try to contribute ideas to this forum.


Regards,
Jeff
 
Hi Jeff,

Are you arguing that events are not related to delegates? I think that
they are
very much related to delegates.

No.

I'm suggesting that defining an event on a class is not implementing
polymorphism, although it may be implementing an interface, which is one way
to implement polymorphism. Events and delegates cannot be used to implement
polymorphism. Polymorphism is implemented through the use of inheritance or
interfaces. For example, delegates are polymorphic through inheritance. If
delegates didn't share a base class then there would be no interface on
which to invoke the target method, and delegates wouldn't be of much use.
But defining a Typed delegate, with or without the use of an event, is not
"implementing" polymorphism.

Maybe you should cite the definition of polymorphism instead :)

IMHO events and delegates are tightly related and together provide a
convenient set of instruments and means for polymorphic behavior in
NET.
Sometimes I tire of these semantic debates. The concept that
polymorphism
can be accomplished beyond the classical interface and abstract class is
what
is important, not these arguments. I am begining to wonder if it is even
worth
the effort to try to contribute ideas to this forum

What's important is that people understand what polymorphism means so that
it will be clear what others are talking about when the word comes up in
conversation or print. If we can't agree on the definition of polymorphism,
then the ambiguity created by our competing definitions of the word will
render it useless.

If you think I'm being too closed-minded then you might want to provide a
more compelling argument than just citing the definition of "implement" :)
 
Dave... I never said events themselves were polymorphic. I just
finished
saying that you could implement polymorphic behavior using events and
delegates. I added the "and delegates" in response to your post as I
thought
you had a valid point, that I was not being absolutely clear. Here is a
definition of Implement:

Main Entry: 2im·ple·ment
Pronunciation: -"ment
Function: transitive verb
1 : CARRY OUT, ACCOMPLISH; especially : to give practical effect to and
ensure of actual fulfillment by concrete measures
2 : to provide instruments or means of expression for

Are you arguing that events are not related to delegates? I think that
they are
very much related to delegates.

"Events use delegates to provide type-safe encapsulation of the methods
that
will be called when triggered. A delegate can encapsulate both Named
Methods and Anonymous Methods."

IMHO events and delegates are tightly related and together provide a
convenient set of instruments and means for polymorphic behavior in
.NET.
Sometimes I tire of these semantic debates. The concept that
polymorphism
can be accomplished beyond the classical interface and abstract class is
what
is important, not these arguments. I am begining to wonder if it is even
worth
the effort to try to contribute ideas to this forum.


Regards,
Jeff
 
Back
Top