J.E.D.I.
With this, your source code should have the following format: [packageDeclaration]
importDeclaration classDeclaration
+
Coding Guidelines: + indicates that there may be 1 or more occurrences of the line where it was applied to.
Here is an example. package registration.reports;
import registration.processing.; imports all classes in registration.processing package
import java.util.List; import java.lang.; imported by default
imports all classes in java.lang class MyClass {
details of MyClass }
1.3.8 The Access Modifiers
The following table summarizes the access modifiers in Java.
private defaultpackage
protected public
Same class Yes
Yes Yes
Yes Same package
Yes Yes
Yes Different package
subclass Yes
Yes Different package
non-subclass Yes
Table 1: Access Modifiers
1.3.9 Encapsulation
Hiding elements of the implementation of a class can be done by making the members that you want to hide private.
The following example hides the secret field. Note that this field is indirectly accessed by other programs using the getter and setter methods.
class Encapsulation { private int secret;
hidden field public boolean setSecretint secret {
if secret 1 || secret 100 { return false;
} this.secret = secret;
return true; }
Introduction to Programming II Page 15
J.E.D.I.
public getSecret { return secret;
} }
If you do not want other classes to modify the secret field, you can set the access modifier of the setSecret method to be private.
1.3.10 Inheritance
To create a child class or a subclass based on an existing class, we use the extends keyword in declaring the class. A class can only extend one parent class.
For example, the Point class below is the superclass of the ColoredPoint class. import java.awt.;
class Point { int x;
int y; }
class ColoredPoint extends Point { Color color;
}
1.3.11 Overriding Methods
A subclass method overrides a superclass method when a subclass defines a method whose signature is identical to a method in the superclass. The signature of a method is
just the information found in the method header definition. The signature includes the return type, the name and the parameter list of the method but it does not include the
access modifiers and the other types of keywords such as final and static.
This is different from method overloading. Method overloading is briefly discussed in the subsection on the this keyword.
class Superclass { void displayint n {
System.out.printlnsuper: + n; }
}
class Subclass extends Superclass { void displayint k {
overriding method System.out.printlnsub: + k;
} }
class OverrideDemo { public static void mainString args[] {
Subclass SubObj = new Subclass; Superclass SuperObj = SubObj;
SubObj.display3; SuperclassSubObj.display4;
} }
Introduction to Programming II Page 16
J.E.D.I.
It produces the following output. sub: 3
sub: 4
The method called is determined by the actual data type of the object that invoked the method.
The access modifier for the methods need not be the same. However, the access modifier of the overriding method must either have the same access modifier as that of the
overridden method or a less restrictive access modifier.
Consider the next example. Check out which of the following overridding methods would cause a compile time error to occur.
class Superclass { void overriddenMethod {
} }
class Subclass1 extends Superclass { public void overriddenMethod {
} }
class Subclass2 extends Superclass { void overriddenMethod {
} }
class Subclass3 extends Superclass { protected void overriddenMethod {
} }
class Subclass4 extends Superclass { private void overriddenMethod {
} }
The overriddenMethod in Superclass has the defaultpackage access modifier. The only modifier more restrictive than this is private. Thus, Subclass4 causes a compiler error
because it tries to override overriddenMethod in Superclass with a more restrictive modifier private.
1.3.12 Abstract Classes and Methods