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. OOPs question: whats the difference between these ways of accessing methods !!!

OOPs question: whats the difference between these ways of accessing methods !!!

Scheduled Pinned Locked Moved C#
question
7 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.
  • A Offline
    A Offline
    Arindam Tewary
    wrote on last edited by
    #1

    class P { public void foo1() { Console.WriteLine("Inside class AA method foo1"); } public void foo2() { Console.WriteLine("Inside class AA method foo2"); } }
    I can use the methods of this class "P" two way !!!! 1. I can inherit in a derived class like this wayclass Q:P { } Q q = new Q(); q.foo1(); q.foo2();
    2. I can take an instance of class P in some class and I can expose the object of class P using some property .... class R { P aa = new P(); public P Prop { get { return this.aa; } } } R r = new R(); r.Prop.foo1(); r.Prop.foo2();My question is what is the pros and cons of this kind of method accessing technique!!! When to use which one ?? Is there any difference between these two ???

    Thanks, Arindam D Tewary

    N P G 3 Replies Last reply
    0
    • A Arindam Tewary

      class P { public void foo1() { Console.WriteLine("Inside class AA method foo1"); } public void foo2() { Console.WriteLine("Inside class AA method foo2"); } }
      I can use the methods of this class "P" two way !!!! 1. I can inherit in a derived class like this wayclass Q:P { } Q q = new Q(); q.foo1(); q.foo2();
      2. I can take an instance of class P in some class and I can expose the object of class P using some property .... class R { P aa = new P(); public P Prop { get { return this.aa; } } } R r = new R(); r.Prop.foo1(); r.Prop.foo2();My question is what is the pros and cons of this kind of method accessing technique!!! When to use which one ?? Is there any difference between these two ???

      Thanks, Arindam D Tewary

      N Offline
      N Offline
      N a v a n e e t h
      wrote on last edited by
      #2

      Hi I think there's no much difference. It is a type of organizing the code like parent child relation ship. Both method can access the public members. But using inheritance method will give a clear hierarchy of the program. public class parent { //methods } public class child:parent { //methods } I think this will give a clear picture that child having parent properties.

      Cheers Navaneeth!! www.w3hearts.com

      A 1 Reply Last reply
      0
      • A Arindam Tewary

        class P { public void foo1() { Console.WriteLine("Inside class AA method foo1"); } public void foo2() { Console.WriteLine("Inside class AA method foo2"); } }
        I can use the methods of this class "P" two way !!!! 1. I can inherit in a derived class like this wayclass Q:P { } Q q = new Q(); q.foo1(); q.foo2();
        2. I can take an instance of class P in some class and I can expose the object of class P using some property .... class R { P aa = new P(); public P Prop { get { return this.aa; } } } R r = new R(); r.Prop.foo1(); r.Prop.foo2();My question is what is the pros and cons of this kind of method accessing technique!!! When to use which one ?? Is there any difference between these two ???

        Thanks, Arindam D Tewary

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        Well, option 1 means that Q is derived from P. This means that it has access to protected methods in P. Consider the following:

        class P
        {
        protected bool IsValid { get { return ... ; } }
        }

        class Q : P
        {
        public void Validate()
        {
        if (IsValid)
        ...
        }
        }

        Now, you can't do the same with the following:

        class R
        {
        private P foo = new P();
        public void Validate()
        {
        // This bit won't compile.
        if (foo.IsValid) ...
        }
        }

        What you are seeing is explained using IsA and HasA relationships. In the case Q, we say that Q is a P - there is an implicit inheritance chain and Q has access to certain *exposed* portions of P. In the case of R, we say that R has a P - no inheritance chain, and no access to the internals of P.

        Deja View - the feeling that you've seen this post before.

        A 1 Reply Last reply
        0
        • A Arindam Tewary

          class P { public void foo1() { Console.WriteLine("Inside class AA method foo1"); } public void foo2() { Console.WriteLine("Inside class AA method foo2"); } }
          I can use the methods of this class "P" two way !!!! 1. I can inherit in a derived class like this wayclass Q:P { } Q q = new Q(); q.foo1(); q.foo2();
          2. I can take an instance of class P in some class and I can expose the object of class P using some property .... class R { P aa = new P(); public P Prop { get { return this.aa; } } } R r = new R(); r.Prop.foo1(); r.Prop.foo2();My question is what is the pros and cons of this kind of method accessing technique!!! When to use which one ?? Is there any difference between these two ???

          Thanks, Arindam D Tewary

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

          What you use depends on what is logical in the relation between the classes. Inheritance is used when the objects are basically the same. Like an airline pilot is also a person, so the class AirlinePilot would inherit from the class Person. Class members are used when one object is a part of or a member of another. An airline pilot is not the same thing as an aeroplane, but rather a part of it, so the AirlinePilot object would be a member of the AeroPlane class.

          --- single minded; short sighted; long gone;

          A 1 Reply Last reply
          0
          • P Pete OHanlon

            Well, option 1 means that Q is derived from P. This means that it has access to protected methods in P. Consider the following:

            class P
            {
            protected bool IsValid { get { return ... ; } }
            }

            class Q : P
            {
            public void Validate()
            {
            if (IsValid)
            ...
            }
            }

            Now, you can't do the same with the following:

            class R
            {
            private P foo = new P();
            public void Validate()
            {
            // This bit won't compile.
            if (foo.IsValid) ...
            }
            }

            What you are seeing is explained using IsA and HasA relationships. In the case Q, we say that Q is a P - there is an implicit inheritance chain and Q has access to certain *exposed* portions of P. In the case of R, we say that R has a P - no inheritance chain, and no access to the internals of P.

            Deja View - the feeling that you've seen this post before.

            A Offline
            A Offline
            Arindam Tewary
            wrote on last edited by
            #5

            Dear Pete O'Hanlon Excellent !! I am sure this is what I was looking for .... and the way you have presented is awesome. Thanks a lot !!!! :-D:-D

            Thanks, Arindam D Tewary

            1 Reply Last reply
            0
            • G Guffa

              What you use depends on what is logical in the relation between the classes. Inheritance is used when the objects are basically the same. Like an airline pilot is also a person, so the class AirlinePilot would inherit from the class Person. Class members are used when one object is a part of or a member of another. An airline pilot is not the same thing as an aeroplane, but rather a part of it, so the AirlinePilot object would be a member of the AeroPlane class.

              --- single minded; short sighted; long gone;

              A Offline
              A Offline
              Arindam Tewary
              wrote on last edited by
              #6

              Dear Guffa, Thanks for a nice reply. Indeed it was very much helpful.

              Thanks, Arindam D Tewary

              1 Reply Last reply
              0
              • N N a v a n e e t h

                Hi I think there's no much difference. It is a type of organizing the code like parent child relation ship. Both method can access the public members. But using inheritance method will give a clear hierarchy of the program. public class parent { //methods } public class child:parent { //methods } I think this will give a clear picture that child having parent properties.

                Cheers Navaneeth!! www.w3hearts.com

                A Offline
                A Offline
                Arindam Tewary
                wrote on last edited by
                #7

                Dear Navaneethkn, Thanks for a nice reply. Indeed it was helpful.:-D

                Thanks, Arindam D Tewary

                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