Creating a Class: 5.1.1

  • A class defines an abstract data class
  • An object is an instance of a class
    • A variable object reference
  • A class is a blueprint which creates objects

Writing classes

  • start a class declaration with public class nameOfClass
    • The body of a class is defined in curly braces
  • You can create objects of a type using ClassName objectName = new ClassName()
public class House {
    // Define attributes and methods here
}

House myHouse = new House();
House friendsHouse = new House();
  • Objects have attributes and behaviors
    • Correspond with instance variables and methods
    • instance variables hold data
    • methods hold behaviors
  • constructors initialize the instance variables
  • a main method can run code

Instance variables: 5.1.2

  • instance variables hold the data for an object
    • also known as attributes, fields, or properties
  • In general these should be declared private
    • only code in the class can directly access the variable
  • They are usually declared right after the class declaration
import java.awt.Color; 

public class Person {
    private String name;
    private String email;
    private String phoneNumber;
}
  • Once we have declared a class, we can create many instances of it with different attribute values
  • Object-oriented programming stresses data encapsulation
    • Data (instance variables) and code acting on it (methods) are wrapped together in a single unit
    • Implementation details are hidden
    • This protects the data from harm
  • Anything outside can only interact with the object through public methods

  • When designing a class, make conscious decisions about what data to make accessible from the outside
    • The private access modifier protects data from external access

Methods: 5.1.3

  • Methods define what an object can do
    • Most are public
      • Accessible outside the class
    • Some are private if they’re only meant to be used internally
  • Methods are defined as public returnType methodName(type param0)
  • Methods can use an object’s instance variables
  • void return type means the method returns nothing
public class House {
    private Color paintColor = Color.red;

    public boolean isRed() {
        return paintColor.equals(Color.red);
    }

    public void printDescription() {
        System.out.println("This house is " + paintColor.toString());
    }
}

Object-Oriented Design: 5.1.4

  • in Object Oriented Design programmers decide what classes are needed to solve a problem
    • Also what data are needed in each class
    • What methods each class needs

Summary: 5.1.6

  • programmers use code to represent physical objects or nonphysical concepts using classes
    • Based on attributes or behaviors of the object/concept
    • In robotics, we use Java to model our robot into “subsystems” which each have their own class!
  • Instance variables define attributes (data) of objects
  • methods define behaviors and functions
  • data encapsulation is a technique which hides the implementation details from the user
    • data is private; accessible through public methods which act on the data
  • keywords public and private affect the access of classes, data, constructors, and methods
    • private restricts access outside of the class
      • instance variables are usually private
    • public allows access from outside the class
      • most methods should be public, unless they aren’t meant to be externally accessed