The purpose of this lab is to help you understand feature accessibility, method overloading, and method overriding.
Create a directories named Lab26/accessibility0 and Lab26/accessibility1 in your Java directory. Copy the files from ~labCourse/Labs/Lab26/accessibility0 and ~labCourse/Labs/Lab26/accessibility1 to your accessibility0 and accessibility1 directories.
You can get the full story from the text. We give an abbreviated version here.
First, if a class is not marked public, then the class and its features are accessible only from within its own package.
Accessibility to the features (both methods and components) of a class can be restricted in the following ways.
If a feature is labeled public, e.g.,
public void someMethod ()
then access is not restricted.
If a feature is not labeled, e.g.,
void someMethod ()
then access is restricted to the package.
If a feature is labeled private, e.g.,
private void someMethod ()
then access is restricted to the class containing the feature definition.
If a feature is defined as protected, then it is accessible from any class in the same package as the class in which the feature is defined.
A subclass inherits protected features from its parent superclass, whether it is in the same package as its superclass or not. The inherited features are accessible from the subclass.
Furthermore, a class can access a protected feature in a subclass instance, as the subclass inherits the feature through the class. For example, suppose class C has protected member p. From class C, the member p of any C instance, or any subclass of C instance, can be accessed.
Note that Lab26.accessibility0.P4Class extends Lab26.accessibility0.P3Class extends Lab26.accessibility1.P2Class extends Lab26.accessibility0.P1Class. All but P2Class are in the same package.
P2Class defines a protected feature that is inherited by P3Class, and P4Class. We are going to try to access the protected feature inherited by a P3Class instance from each class in the hierarchy.
A class can have several methods with the same name, as long as all have different signatures: that is, different number and/or types of parameters. For instance, a class could contain both these methods
public void sayHi ()
public void sayHi (String message)
since the first has no parameters and the second has one. But a class could not contain the following two methods, even though the return types are different:
public char grade (Student s)
public double grade (Student s)
Having different methods in the same class with the same name is referred to as overloading.
Overriding:
If a class extends another, it inherits all the methods of its parent class. The subclass, however, can redefine some or all of the methods it inherits. This is called overriding.
Suppose that you declare the following:
CClassList list = new CClassList();
where CClassList is a list of CClass instances, and add several CClass and CXClass instances to the list:
list.add(new CClass());
...
list.add(new CXClass());
...
Now suppose we write for some legal value of i:
list.get(i).sayHi();
A class can decide to behave like its parent by using the keyword super.
Finally, it is important to note the difference between overloading and overriding. With overloading, a class has several different methods with the same name. With overriding, a method is given different implementations in a class and its parent.
The particular method denoted by a method invocation (overload resolution) is determined at compile time by the static syntactic type or the object reference, and the static syntactic types of the arguments.
Submit the following, as directed by your lab instructor: