Solved

polymorphism without inheritance

Posted on 2006-11-07
4
547 Views
Last Modified: 2008-02-01
Hi

I have two classes which have exactly the same properties and methods. These classes aren't created by me. If they were I could have them inherit from the same class. Sadly i can't do that.

For example, lets say the class are of type ClassA and ClassB

Without inheritance, is there any way i can achieve polymorphisn. I woudl like to create a property to which I can assign either an object of ClassA and ClassB and then call the methods on this object which both of them have.

thanks a lot
andrea
0
Comment
Question by:andieje
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 

Author Comment

by:andieje
ID: 17888902
I've been thinking.

I do have some access to the code. I could use partial classes and give them  both a shared interface?
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 total points
ID: 17888945
Exactly, you answered your own question. Interface is the way.
Write interface which contains all common properties and methods, derive both classes from this interface. Client code must call interface methods using interface reference.
0
 
LVL 13

Expert Comment

by:newyuppie
ID: 17889035
i dont know if i fully understand...

1) you have 2 classes which expose the same methods and properties, but obviously they perform different operations on these same methods otherwise whats the point of having 2 different same classes, right?

2)now you have created a property (on a third class, created by you) that instantiaties an object of type 1 or 2.

3) now when you say<<then call the methods on this object which both of them have.>>, i dont understand why you cant call the methods now as is. i mean, if proposition 1) and 2) are true, it would be very clear that if you instantiate the object of type 1 you will get different results than if you instantiate it as type 2 (type 1 i mean of class 1).

4) if 3 is true then what's the problem?

sorry if i dont understand the question maybe im very on the wrong track...?
NY
0
 

Author Comment

by:andieje
ID: 17889074
yippe, don't you just love partial classes
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question