- Object Initialization: Constructors
- Constructor - The method of the same name as the Class that is used to "Construct" the
object from the class.
- A class can have many constructors, all having the same name as the class, but with
different parameters.
- A common use of a constructor is to allow the caller to specify initializing values
for state (field) variables of the class (as in supplying X and Y for the Point class).
- The default constructor is inserted by the JVM if no other constructors are
defined in your classes code.
- It is a good practice to always include a constructor, even if one that accepts
no parameters and initializes state (field) variables to specific, known values.
- Warning: - Be careful NOT to include void as the return data type of
a constructor. Constructors have no return type and including void, out of habit, will
indicate to Java that the method is "just anothe method" and not a constructor.
- this - The keyword this refers to the object derived from the class and is
a convenient way within constructors and methods to reference (and to be sure your program
references) class state variables, methods, etc.
- Encapsulation
- Encapsulation - Hiding the implementation details of an object from the clients (callers)
of the object.
- Abstraction - Focusing on essential properties rather than inner details.
- Private Fields - State (field) variables that are marked as private are not directly accessible by client code (code within a program that declares the class as an object and then uses the object).
// encapsulate fields of Point objects
private int x;
private int y;
// Will now require accessor methods so client
// may get values of x and y
public int getX() {
return x;
}
public int getY() {
return y;
}
// Also requires a mutator method for setting x and y
public void setLocation(int newX, int newY) {
x = newX;
y = newY;
}
- Class Invariant - An assertion (or fact) about an object's state that is true for the lifetime
of the object. Note that Java has no formal mechanism in the language for maintaining assertions; rather
it is up to the programmer and the logic of the class to enforce them.
Examples:
- hours must be between 0 and 23, inclusive
- minutes must be between 0 and 59, inclusive
- dayOfTheWeek must be from the list of Strings in the set: {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}
// Point class for positive (upper-right quadrant) points
// only
// Also requires a mutator method for setting x and y
public void setLocation(int newX, int newY) {
if (newX < 0 || newY < 0) {
throw new IllegalArgumentException();
}
x = newX;
y = newY;
}
- Inheritance (Optional Chapter 9 Topic)
- Inheritance - A mechanism in which one object acquires all the properties and behaviors of a parent object. Inheritance allows there to be a base class, aka the superclass, that is
extended to make a derived class, aka the subclass.
- Superclass - The parent class in an inheritance relationship.
- Subclass - The child, or derived, class in an inheritance relationship.
- Syntax Notation for a class that inherits from a superclass:
public class <name> extends <superclass> { ...
}
- Example: Consider a program that kept track of animals in a zoo. That
program could use a class for each type of animal, including Lion, Tiger,
Snake, and Turtle classes.
- Further suppose that each of these classes had accessor methods
to determine if the animal of that class was warm blooded, whether
or not it lays eggs, and whether or not it has a tail.
- That would look like this for the Lion class:
public class Lion {
public String name;
public int numberFeet;
public Lion( String name ) {
this.name = name;
numberFeet = 4;
}
public boolean isWarmBlooded() {
return true;
}
public boolean laysEggs() {
return false;
}
public boolean hasTail() {
return true;
}
}
- However, many types of animals have things in common, for example all
reptiles are cold-blooded and lay eggs, and all mammals are warm-blooded and
do not lay eggs (is the platypus still a thing?) This is where inheritance
can help by introducing Reptile and Mammal superclasses
- The Turtle and Snake classes could be based on a superclass called
Reptile.
- Lastly, the Mammal and Reptile classes could also be derived from
a superclass called Animal.
- Lion as inherited from Mammal which is inherited from
Animal:
// Animal base class
public class Animal {
public String genus;
public String name;
public int numberFeet;
public Animal( String genus, String name ) {
this.genus = genus;
this.name = name;
numberFeet = 0; // overridden by derived class
}
// all methods below will be overridden by derived classes
public boolean isWarmBlooded() {
return true;
}
public boolean laysEggs() {
return true;
}
public boolean hasTail() {
return true;
}
}
// Mammal class extends Animal
public class Mammal extends Animal {
public Mammal( String genus, String name ) {
super( genus, name );
}
@Override
public boolean isWarmBlooded() {
return true;
}
@Override
public boolean laysEggs() {
return false;
}
}
// Lion class extends Mammal
public class Lion extends Mammal {
public Lion( String name ) {
super( "Lion", name );
numberFeet = 4;
}
@Override
public boolean hasTail() {
return true;
}
}
- @Override - Note the use of the @Override directive before any methods
in the subclass class that will override those from the superclass. This directive
must be included for the override (aka "specialization") to occur.
- Sample: Take a look in:
/home/grader/rogers161/Public/Zoo
and in:
/home/grader/rogers161/Public/Zoo2
for examples that show no inheritance and inheritance through the Reptile, Mammal and Animal classes, respectively.