Module 01 - Java Class Design
Module 01 - Java Class Design
John Yeary
Module
1
-
Objectives
Use
access
modiers:
private,
protected,
and
public.
Override
methods.
Overload
constructors
and
other
methods
appropriately.
Use
the
instanceof
operator
and
casting.
Use
virtual
method
invocation.
Override
methods
from
the
Object
class
to
improve
the
Access
Modiers
public, protected, private, and package (default). public members (variables and methods) are visible to any
classes in the same package, or sub-classes of the declared class. is no access modifier called package. These members are only visible to classes in the same package. within the class.
instance variables and methods. The class will not compile if applied to a local variable.
It is a best practice to make instance variables
private, or protected (if you expect sub-classes) and provide accessors (getters) and mutators (setters).
Overriding
Methods
A
method
must
be
implemented
(overridden)
if
one
these
conditions exist The class is implementing an interface. If the method is not overridden, the class must be declared as abstract. The class is extending an abstract class, and implementing abstract methods. If all of the methods of the abstract class are not implemented, the class must be declared abstract. specialization in the subclass.
A method of a superclass may be overridden to provide additional A class which contains a final method can sub-classed, but its
It must have the same arguments, and return type. The return type may be covariant. This means that the return type may
It may throw any unchecked (runtime) Exception which may not be It may throw more specific exceptions than declared in the superclass,
e.g., the super-class throws Exception, and the sub-class throws FileNotFoundException.
Constructor
Overloading
Every class implicitly has a default no-name constructor if no
must provide a constructor that takes the same arguments, or call super() with the same arguments. with the same argument list to daisy chain construction.
Constructors may take any number of arguments, and may call this()
constructor.
super() is called implicitly if not defined. Only one call to super(), or this() may be in any constructor.
Method
Overloading
Overloaded methods must change the argument list It can have the same, or different return types. It can different access modifiers It can declare new, or broader exceptions. It may be overridden in the same class, or sub-classes. The reference type determines which overloaded method is
instanceof
Operator
It will cause a compiler error if the comparison is done with
Casting
In order to use an object where it is hidden by a super-
are static.
determining which method to invoke based on its class hierarchy. For example, If you call eat(); on the Animal interface. The concrete implementation of the method is called. It may be Dog.eat(); or Cat.eat();
overridden:
clone();
equals();
hashCode(); toString();
enums. This may include ancillary classes which are used by the public API.
import statements are used to dierentiate classes which Classes in the same package do not need to be imported. Classes in the java.lang.* package do not need to be
may have the same name, but may have dierent methods.
imported.7.3
References
Java
Language
Specication,
Java
SE
7
Edition,
James
Gosling,
Preparation for Java Programmer Language Certication Sun Certied Java Programmer for Java 6 Study Guide, Kathy