OOP concept in c#

W

weird0

C# corner on one of its tutorial says "Polymorphism allows objects to
be represented in multiple forms".

Can somebody elaborate and make it with an example.

How can an object be represented in multiple forms when c# does not
allow multiple inheritance?

Regards
 
M

Mr. Arnold

weird0 said:
C# corner on one of its tutorial says "Polymorphism allows objects to
be represented in multiple forms".

Can somebody elaborate and make it with an example.

How can an object be represented in multiple forms when c# does not
allow multiple inheritance?

Maybe, you're mis-interpreting.

MODEL-VIEW-PRESENTER

http://www.polymorphicpodcast.com/

click 'Shows'

click 'Design Patterns Bootcamp: Model View * Patterns'

view part 1-5
 
J

Jeff Louie

This may help.

http://www.geocities.com/jeff_louie/OOP/oop37.htm

IMHO, C# does allow a very limited form of multiple inheritance through
"interfaces" which are similar to pure virtual classes in C++. If you
accept that interfaces in C# are analogous to pure virtual classes in
C++ then, in essence, C# supports single inheritance of an
implementation hierarchy and multiple inheritance of interfaces.

Regards,
Jeff
 
A

Arne Vajhøj

weird0 said:
C# corner on one of its tutorial says "Polymorphism allows objects to
be represented in multiple forms".

Can somebody elaborate and make it with an example.

How can an object be represented in multiple forms when c# does not
allow multiple inheritance?

You are looking at two different things.

multiple parents : one child class with multiple parent classes

polymorphism : one parent class with multiple child classes that
can all be treated the same

Arne
 
A

.\\\\axxx

C# corner on one of its tutorial says "Polymorphism allows objects to
be represented in multiple forms".

Can somebody elaborate and make it with an example.

How can an object be represented in multiple forms when c# does not
allow multiple inheritance?

Regards

At the risk of over simplifying:

If Shape is a class.
Square, Triangle, etc. inherit from Shape and override the Draw()
method.
your program can say

Shape x = new Square();
x.Draw();

So your x object is a Shape instantiated as a Square - and is
therefore represented in multiple forms (two, in this case).

If your oubjects also implement interfaces, each implemented interface
gives another potential reference - so you could have

(ITwoDimentsionalObject) x.Color = Red;

where x is now referred to as an ITwoDimentsionalObject.

Every objcet in .Net inherits frob Object - and so can always be
referred to as its own type and Object, at the very least. So every
object is polymorphic.
 
W

weird0

No... actually , what i want is that I have one user-defined class say
Animal.

Now, I want its single object representation through a label or
whatever, in every form. What should be design of classes?

If I update the attributes inside the object, how will it be updated
and changes be reflected in the all the three forms, say i have made
the change in Form3.

Regards
 
M

Marc Gravell

If the class supports change-notification, then any of the
observer-based patterns / MVP should allow it to be updated in all
bound user-interface elements. So you should be able to just use
normal data-binding. You might need to watch for threading, but that's
about it.

Change notification can commonly be either as a set of events
({PropertyName}Changed for every {PropertyName}), or via
INotifyPropertyChanged.

Marc
 
J

Jeff Louie

weird0... There are many ways to do this and the answers are not that
simple. On the most simple level look at the Observable pattern that
Mark suggest. Typically, the object of class Animal provides an Event
that can notify listeners of changes in the object's state and listeners
can register an interest in that Event. This works for a single View,
but gets complicated when there are multiple Views bound to the same
data.

You may be interested in the Passive View pattern where the Controller
may own the Model and View. The Controller may update the Views using
Events. In this pattern, there is no direct coupling between the Model
and the Views. The Controller talks directly to the Model and may fire
Events to the Views. The Views talk directly to the Controller. This
pattern works both locally and remotely.

You can google on:
Observable
Model View Controller
Model View Presenter
Passive View
Supervising Controller

Regards,
Jeff
 
L

Liz

No... actually , what i want is that I have one user-defined class say
Animal.

Now, I want its single object representation through a label or
whatever, in every form. What should be design of classes?

If I update the attributes inside the object, how will it be updated
and changes be reflected in the all the three forms, say i have made
the change in Form3.

create a method in Animal which can notify any class of changes in its
state; Color=Blue, etc

create a public method in Animal and a collection which will other classes
can call to "register" their "interest" in such state changes

each class that should reflect the state changes in Animal registers with
Animal by sending it a delegate via Animal's registration method; in
effect: here's my phone #, call me when you turn blue

when Animal turns blue, it calls all of the registered handlers it has
accumulated; each handler does whatever it's supposed to do ... turn blue
itself, call 911, whatever ..

if that's your intent ... if you're trying to keep three classes in sync and
none of them is "controlling," it's a different problem
 
J

Jon Skeet [C# MVP]

Liz said:
create a method in Animal which can notify any class of changes in its
state; Color=Blue, etc

create a public method in Animal and a collection which will other classes
can call to "register" their "interest" in such state changes

There's no need to write your own type of collection here - this is
precisely what events are good at. The subscriber provides a delegate
which is called when the appropriate "interesting thing" occurs.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top