-
March 13th, 2009, 10:17 PM
#1
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...
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|