Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. An OPP Question

An OPP Question

Scheduled Pinned Locked Moved C#
question
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Majid Shahabfar
    wrote on last edited by
    #1

    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?

    G L 2 Replies Last reply
    0
    • M Majid Shahabfar

      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?

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      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;

      M M 2 Replies Last reply
      0
      • G Guffa

        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;

        M Offline
        M Offline
        Majid Shahabfar
        wrote on last edited by
        #3

        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).

        G 1 Reply Last reply
        0
        • M Majid Shahabfar

          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?

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          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] }


          1 Reply Last reply
          0
          • G Guffa

            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;

            M Offline
            M Offline
            MicealG
            wrote on last edited by
            #5

            A bit harsh don't you think Guffa?

            Freedom is the right to say that 2+2=5 if this is so everything else will follow.

            1 Reply Last reply
            0
            • M Majid Shahabfar

              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).

              G Offline
              G Offline
              Guffa
              wrote on last edited by
              #6

              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;

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups