Lecture 11 : Classes and Inheritance II

Controlling Access to Members of a Class

Lets look at private and public

Private

To declare a private member, use the private keyword in its declaration. The following class contains one private member variable and one private method:

class Alpha {
private int iamprivate;
private void privateMethod() {
System.out.println("privateMethod");
}
}

Objects of other types cannot inspect or modify the iamprivate variable and can't invoke privateMethod.
For example, the Beta class defined here:
class Beta {
void accessMethod() {
Alpha a = new Alpha();
a.iamprivate = 10; // illegal
a.privateMethod(); // illegal
}
}
Beta class cannot access the iamprivate variable or invoke privateMethod on an object of type Alpha because Beta is not of type Alpha .

You will get compiler error message
Beta.java:9: Variable iamprivate in class Alpha not 
accessible from class Beta.
a.iamprivate = 10; // illegal
^
1 error
and
Beta.java:12: No method matching privateMethod()
found in class Alpha.
a.privateMethod(); // illegal
1 error

The following code is legal
class Alpha {
private int iamprivate;
boolean isEqualTo(Alpha anotherAlpha) {
if (this.iamprivate == anotherAlpha.iamprivate)
return true;
else
return false;
}
Objects of the same type have access to one another's private members. This is because access restrictions apply at the class or type level (all instances of a class) rather than at the object level (this particular instance of a class).


public 

example
public class Alpha {
public int iampublic;
public void publicMethod() {
System.out.println("publicMethod");
}
}
and

public class Beta {
void accessMethod() {
Alpha a = new Alpha();
a.iampublic = 10; // legal
a.publicMethod(); // legal
}
}

Understanding Instance and Class Members

Example:

You want to create a Student class with an instance variable name
In addition to it, you want to have an integer to record the number of students you created. What is the output of the program?
public class Student {
private String name;
private int numOfStudents;
public Student(String name) {
this.name = name;
// increase the number of students by 1
numOfStudents++;
}
public String getName() {
return name;
}
public int getNumOfStudents() {
return numOfStudents;
}

public static void main(String[] args) {
Student Amy, Ben, Charles;
Amy = new Student("Amy");
Ben = new Student("Ben");
Charles = new Student("Charles");
System.out.println(Charles.getNumOfStudents());
}
}

Output

The output of the program is 1! Not the 3 we expected.
In our program, numOfStudents is assigned to be 0 implicitly since we don't explicitly assign the value to it.
Every time you create a new Student, all its variables will initiate according to the constructor, so the variable
numOfStudents is 0++ = 1 after you create the object.

We should change the lines
private int numOfStudents;
public int getNumOfStudents();
to
private static int numOfStudents; 
public static int getNumOfStudents();
After the change numOfStudents is a class memeber and getNumOfStudents() is a class method.


New program

The output of the program is 3.
public class Student {
private String name;
private static int numOfStudents;
public Student(String name) {
this.name = name;
// increase the number of students by 1
numOfStudents++;
}
public String getName() {
return name;
}
public static int getNumOfStudents() {
return numOfStudents;
}

public static void main(String[] args) {
Student Amy, Ben, Charles;
Amy = new Student("Amy");
Ben = new Student("Ben");
Charles = new Student("Charles");
System.out.println(Student.getNumOfStudents());
}
}



static

Initializing Instance and Class Members

One more example for using static

public class MyMath {
// define a constant PI
public static final double PI = 3.14;
public static double areaOfCircle(double radius) {
return PI*radius*radius;
}
}

Here is the test program
public class MyMathTest {
public static void main(String[] args) {
double radius = 5;
System.out.println("PI is " + MyMath.PI);
System.out.println("The area of circle with radius = " + radius +
" is " + MyMath.areaOfCircle(radius));
}
}