Results 1 to 4 of 4
  1. #1
    Madushan is offline Member
    Join Date
    Oct 2008
    Posts
    1
    Rep Power
    0

    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
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    i prefer to keep all the variable instances private and access the variables of super classes through protected method

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,569
    Rep Power
    12

    Default

    Also crossposted on Sun forums

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    I'm not convinced there is a best practice. Default protections are what the Java designers had in mind as best practice, but this tends to lead to sloppy encapsulation, since default methods *and member variables* can be accessed by any class in the package. YUCK!

    Protected doesn't help.

    Private is too restrictive. Even subclasses are locked out.

    IMHO, the default protection *should* shield methods and member variables from access by anything other than the class of interest or it's subclasses and inner classes. Other classes in the package should not have access. I code this way when I own the package, but mistakes can be made.

Similar Threads

  1. How to access private data types from public classes?
    By kevzspeare in forum New To Java
    Replies: 3
    Last Post: 03-07-2009, 05:19 AM
  2. Ask for help on Java access to protected methods
    By fangzhong in forum Advanced Java
    Replies: 3
    Last Post: 02-17-2009, 02:50 PM
  3. Class Reflection: Finding super class names
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-23-2008, 09:12 PM
  4. Replies: 1
    Last Post: 08-07-2007, 08:53 AM
  5. help with protected method in vector class
    By katie in forum Advanced Java
    Replies: 1
    Last Post: 08-06-2007, 11:59 PM

Posting Permissions

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