Solved

polymorphism without inheritance

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

17 Experts available now in Live!

Get 1:1 Help Now