CIS 160 - Designing objects

Objective

  • Design classes and objects in Java

Overview

  • Classes in Java are usually blueprints for creating objects.
  • Classes may also be used as a convenient place to store related publicly accessible data and methods. One example of this is the Math class. You do not create Math objects, but you can use public static methods of the Math class, such as Math.sqrt and Math.pow.
  • Objects are used to represent an entity, such as a person, place, thing, or event.
  • One of the main goals of object-oriented design is to figure out the structure of your classes and the objects they create.

Local vs. class-level variables

  • Class level variables are declared inside a class, but outside any methods. These should only be used for variables that must persist beyond a method and represent something about the class or object.
  • Data that refers to the class as a whole should be declared as static. The class and all objects of the class share that variable.
  • Data that refers to a specific instance (object) of a class are instance variables. By not using the static keyword, a separate copy is created for each object created. The class itself gets no copy.
  • If a variable does not need to persist or be shared, then it should only be declared in the method where it is needed. That is a local variable.

Access modifiers

  • If every other class should have access to a variable or method, then declare it as public.
  • If no other class should have access to a variable or method, then declare it as private.
  • If a private variable should be accessible to a subclass, then decalre it as protected. In Java, this also means other classes in the same directory have access.
  • If you want a class level variable to be read-only by other classes, then make it private and create a public accessoror (get) method for it.
  • If you want a class level variable to have error checking when being set, then make it private and create a public mutator (set) method for it. The error checking goes in the mutator method.
  • Subclasses inherit all the data and methods from the class they extend (but not constructors). Keep in mind that you can inherit a private variable and still not have access to it because it is private.

Constructors

  • Every class you write must extend one other class. If none is specified, then the class automatically extends the Object class.
  • A constructor looks like a method, but it has no return data type and has the same name as the class.
  • The default constructor is the constructor with no arguments.
  • Every class must have a constructor. A default constructor is written for a class if none is specified.
  • If you write any constructor, then Java does not add the default constructor.
  • The first statement in every constructor is a call to the superclass constructor. If you do not write that call, then Java will automatically insert a call to the superclass default constructor.
  • The superclass is not referred to by name in methods. It is referred to as super.
  • Here is a call to the superclass default constructor: super();
  • Here is a call to the superclass constructor which has two int parameters: super(5, 2);
  • Superclass methods can be called by prepending super. to the method call. Example: super.toString()
  • The primary purpose of most constructors is to make sure the object's instance variables are initialized.

Class hierarchy

  • If you have two classes that are similar, such as Employee and Customer, then you might find it better to design a basic Person class, and then have Employee and Customer extend Person.
  • Keep in mind that all the rules apply to all the classes. If you do not write a constructor for Person, then a default constructor will be written automatically.
  • If you write a non-default constructor for Person, then no default constructor is automatically provided. If you then write a constructor for a subclass, such as Employee or Student, which does not include a call to a superclass constructor as the first statement, then a call to the default superclass constructor will be added. That will cause problems since it does not exist.
  • Do NOT just add a default constructor to get rid of error messages. Make sure that default constructor is supposed to exist. It is likely that you should have written a call to a specific non-default superclass constructor.