Well, what you are dealing with here is some basic Java 101 type stuff. I don't mean that in a bad way. But these are common object oriented ideals that need to be learned. So I'll try and run through a brief tutorial.
Scope - Variables in Java have scope depending on their scope declaration and when/how they are used.
- Public - Visible to all classes. In all honesty you probably won't use this scope as much as you might think
- Protected - visible to classes outside the package that inherit the class, also to all classes in the package.
- Private - visible only within the class, not by inheritors, not by other classes in the package.
- Default - visible to all classes of the package.
There is also a scope considered 'local' which is a variable defined within a block of code, like a method. These local variables are only visible to that block of code.
The standard practice when it comes to JavaBean type classes is to have all private variables with accessor methods (getters/setters) as mende suggested. This could be debated but 99.9% of the time it is accepted practice in the Java world.
Code:
public class Teacher{
private int severity; /* on a scale from 1-10*/
private int enthusiasm; /* on a scale from 1-10*/
private String mood;
private String wayOfTeaching;
private String health; /* possible values: healthy or sick*/
private String subject;
private String sex;
// provide getters and setters for all private variables
}
Code:
class TeacherFrench extends Teacher {
TeacherFrench() {
setSeverity(7);
setEnthusiasm(8);
setMood("Good");
setWayOfTeaching("Writing everything on the blackboard.");
setHealth("Healthy");
setSubject("French");
setSex("Male");
}
}
Code:
class ClassSituation {
public static void main (String[] args) {
TeacherFrench misterBackaert = new TeacherFrench();
System.out.println("Severity: " + misterBackaert.getSeverity());
}
}
You could also provide a constructor in Teacher that accepted values for all variables and when a new class of type Teacher is created you could just make the call to the constructor via super(). But that can be done after you understands the basics above.
Did that help at all?