Solved

polymorphism without inheritance

Posted on 2006-11-07
4
545 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
  • 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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help with Query not working in client's PC 1 37
Modal Popup Extender control 1 22
vb.net class 3 17
Help with preventing selection from a combobox 11 28
Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

821 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