Solved

polymorphism without inheritance

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now