CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2009
    Posts
    3

    Smile Private or Protected access for super class variables

    What is the best practice...
    Assume there is a class hierachy like
    Person (Super class) / StaffMember/ Professor (Sub class)

    1) The best way is to keep all the instance variables of each and every class private and access the private variables of super classes through subclass constructors (calling "super()")

    Ex:-

    public class Person {
    private String empNo;

    public Person (String empNo) {
    this.empNo = empNo;
    }
    }

    public class Professor extends Person {
    private String ........;
    private int ...........;

    public Professor (String pEmpNo) {
    super(pEmpNo);
    }
    }

    OR

    2)Changing the access level of the super class variables into "protected" or "default" and access them directly within the sub classes...

    Ex:-

    public class Person {
    protected String empNo;

    public Person () {

    }
    }

    public class Professor extends Person {
    String ........;
    int ...........;

    public Professor (String empNo) {
    this.empNo = empNo;
    ..............
    ..............
    }
    }

    Thank you...

  2. #2
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: Private or Protected access for super class variables

    Best practice is to keep superclass variables private wherever possible (see below) and always initialise them through the superclass constructor.

    Purists argue for always keeping all variables private. Keeping all variables private obviously means the subclass can't ever access them directly, so must access them indirectly through superclass methods that aren't private. This can make it tempting to provide protected get/set methods for the subclass to use. However, this is little better than direct acccess, and also allows other package classes access.

    For the purist, encapsulation is all-important, and the superclass is treated like a separate class, so inheritance becomes a form of composition without the inconvenient forwarding methods (to do this via true composition would mean that the 'subclass' would have to implement the 'superclass' interface, and forward calls to those methods to the 'superclass' object).

    Good designs need to be carefully though out in advance, and in the real world, the superclass variables may often be made 'protected' to allow subclass access, because it's inconvenient not to have direct access, or requirements have changed and it's too much effort to refactor the design. But in my experience, in large projects that are continually being extended and enhanced, this can lead to nasty problems where superclass variables are modified by subclasses without the superclass knowing (e.g. subclass sets a variable to a value the superclass doesn't expect, etc).

    Inheritance is powerful, but should be used sparingly, and be kept as shallow as possible. Never subclass a concrete class, insert an abstract class if necessary. Unless the relationship is clearly and obviously A is a type of B, prefer composition over inheritance, using interfaces. Interfaces make your designs far more flexible.

    Simplicity and flexibility will trump optimization and power in a world where connectivity is the key...
    A. Bosworth
    Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  3. #3
    Join Date
    Mar 2009
    Posts
    3

    Re: Private or Protected access for super class variables

    Thank you very much....
    That was a great answer, i leant lot of things...
    Thank you once again.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured