4 Replies Latest reply: Dec 2, 2013 11:13 PM by fb229492-3083-48b8-b55b-155f129d5a82 RSS

    Accessing other class features? best pratice

    svn123

      Hi,

      i had a small doubts which is the best practice accessing the other class variable and so on.

       

      public class classname extends anotherclassname 
      {
      
      } 
      

       

      or else

       

      public class classname { 
      public returntype methodname(anotherclassname anotherclassobject)
      {
      } 
      } 
      

       

      thanks.

        • 1. Re: Accessing other class features? best pratice
          TPD-Opitz

          A forum is not the right place to learn basics of object oriented programming. You should buy a book or read related tutoials on the web (like this).

           

          bye

          TPD

          • 2. Re: Accessing other class features? best pratice
            baftos

            This is a very pertinent question with no clear answer. It takes a lot of experience to get it right. As long as you don't provide a public API, in which case wrong means forever wrong, don't worry too much, since both work.

            Just be ready to change your design, when necessary. Anyway, I cannot answer this difficult question, so just google for 'composition vs inheritance'.

            • 3. Re: Accessing other class features? best pratice
              rp0428

              TPD has offered you good advice. Both this question and your previous one show that you are not yet grounded in the basics of the object world. That isn't a critism - just making a point.

              i had a small doubts which is the best practice accessing the other class variable and so on.

              You say accessing the 'other class variable' but in your first scenario there IS no 'other' being referenced.

              public class classname extends anotherclassname   

              The 'classname' actually IS 'anotherclassname' - there is NO 'other' involved. They are one.

               

              A 'best practice' involves selecting ONE way of doing something out of many possible ways. What you posted illustrates two different things entirely so 'best practice' does not really apply.

               

              Take TPD's advice and get some actual experience DOING instead of just READING. Then you will have a much better sense of the different ways that things can be done and a greater appreciation for why it is important to use 'best practices'.

              • 4. Re: Accessing other class features? best pratice
                fb229492-3083-48b8-b55b-155f129d5a82

                Without getting into a full-blown Object-Oreinted programming lesson: extends (as in your first option) is primarily used to represent the "is a" relationship between two classes.  i.e classname is an anotherclassname.  As a practical example

                 

                public class Dog extends Animal {
                }
                
                

                 

                a dog is an animal.  This technique is usually used to save developers from having to re-write code for similar objects rather than to grant one class access to another classes features.

                 

                Your second suggestion is closer to how one class can allow other classes to access its features.  A technique known as accessor methods.

                 

                Take a basic class animal as an example

                 

                public class Animal {
                
                    private int age;
                
                    // insert constructors and other methods here
                
                    public int getAge() {
                        return this.age;
                    }
                }
                
                

                 

                You can see this class has an attribute age and an accessor method getAge().

                 

                Elsewhere in your program (probably in your main method while you are starting out) you then use something like this to access the animal objects age attribute:

                 

                Animal a = new Animal();       // creates an object of the animal class
                int animalsAge = a.getAge(); // stores the age attribute of the animal object in a variable called animalsAge.
                
                


                There is some debate about wether this is the best practice.  But it will stand you in good stead while you are starting out.

                 

                These are pretty core fundamentals of Object-Oriented programming and I can't help but agree with the previous posters that it sounds like you have a bit of reading to do   I hope these examples help you on your way.