An OPP Question
-
Hi there, I have two classes; ClassA and ClassB. I want that ClassA has an object of classB by which call the methods of ClassB. like this: objectOfClassB.Methods(); ClassB is an abstract class so it cannot be instantiated. In other words I want other classes access to ClassB only by its object in ClassA. How can I afford to do this?
-
Hi there, I have two classes; ClassA and ClassB. I want that ClassA has an object of classB by which call the methods of ClassB. like this: objectOfClassB.Methods(); ClassB is an abstract class so it cannot be instantiated. In other words I want other classes access to ClassB only by its object in ClassA. How can I afford to do this?
Majid Shahabfar wrote:
I have two classes; ClassA and ClassB.
No, you don't. Nobody gives threir classes stupid names like that. In reality they have better names, that says something about what they are for. By taking away those names and creating phony names, you only make it harder to understand what it is you are trying to do.
Majid Shahabfar wrote:
I want that ClassA has an object of classB
As ClassB is an abstract class, you can't create instances of it, so ClassA can not have an instance of ClassB. You have to have a class that inherts from ClassB, so that you can create an instance of that class.
Majid Shahabfar wrote:
How can I afford to do this?
You can't. You would have to buy Microsoft, so that you could make them change how the language works, and obviously you can't afford to buy Microsoft. ;)
--- single minded; short sighted; long gone;
-
Majid Shahabfar wrote:
I have two classes; ClassA and ClassB.
No, you don't. Nobody gives threir classes stupid names like that. In reality they have better names, that says something about what they are for. By taking away those names and creating phony names, you only make it harder to understand what it is you are trying to do.
Majid Shahabfar wrote:
I want that ClassA has an object of classB
As ClassB is an abstract class, you can't create instances of it, so ClassA can not have an instance of ClassB. You have to have a class that inherts from ClassB, so that you can create an instance of that class.
Majid Shahabfar wrote:
How can I afford to do this?
You can't. You would have to buy Microsoft, so that you could make them change how the language works, and obviously you can't afford to buy Microsoft. ;)
--- single minded; short sighted; long gone;
Ok, Suppose ClassB is not an abstract class. Now show me a way by which other classes make access to ClassB members only through ClassA. (not directly instantiate an object of ClassB).
-
Hi there, I have two classes; ClassA and ClassB. I want that ClassA has an object of classB by which call the methods of ClassB. like this: objectOfClassB.Methods(); ClassB is an abstract class so it cannot be instantiated. In other words I want other classes access to ClassB only by its object in ClassA. How can I afford to do this?
Hi, you could make ClassB a private inner class of ClassA, so nothing outside ClassA would be able to instantiate or otherwise reference ClassB. Making ClassB "abstract" is irrelevant in this; if you do you need yet another concrete class that inherits from it. :)
Luc Pattyn
try { [Search CP Articles] [Search CP Forums] [Forum Guidelines] [My Articles] } catch { [Google] }
-
Majid Shahabfar wrote:
I have two classes; ClassA and ClassB.
No, you don't. Nobody gives threir classes stupid names like that. In reality they have better names, that says something about what they are for. By taking away those names and creating phony names, you only make it harder to understand what it is you are trying to do.
Majid Shahabfar wrote:
I want that ClassA has an object of classB
As ClassB is an abstract class, you can't create instances of it, so ClassA can not have an instance of ClassB. You have to have a class that inherts from ClassB, so that you can create an instance of that class.
Majid Shahabfar wrote:
How can I afford to do this?
You can't. You would have to buy Microsoft, so that you could make them change how the language works, and obviously you can't afford to buy Microsoft. ;)
--- single minded; short sighted; long gone;
-
Ok, Suppose ClassB is not an abstract class. Now show me a way by which other classes make access to ClassB members only through ClassA. (not directly instantiate an object of ClassB).
What is it that you are trying to accomplish really? You can prevent creations of ClassB insteances by making the constructor non-public. If you want to restrict creation of ClassB instances to ClassA, you have to put the two classes in an assembly by themselves, so that you can make the constructor internal.
--- single minded; short sighted; long gone;