- A class defines a new data type (a classification). It is the formal implementation, or blueprint, of the attributes and behaviors of the objects of that class.
Example: A class `Car` can define attributes like color, model, and speed, and behaviors like accelerating or braking.
- An object is a specific instance of a class with defined attributes. Objects are declared as variables of a class type.
Example: `Car myCar = new Car();` creates an object of type `Car` named `myCar`.
- Objects are values created by constructing an instance of a class.
Example: `myCar` is an instance of the `Car` class, created with `new Car();`.
- Each class defines the attributes its objects have (the properties or what each object knows about itself) and the behaviors (what each object can do).
Example: The `Car` class could have attributes like `color` and `model`, and behaviors like `accelerate()` and `brake()`.
- The attributes are written as instance variables in the class, and the behaviors are written as methods.
Example: In `Car`, the attribute could be `String color;` and the behavior could be `void accelerate() {...}`.
- Constructors initialize the attributes in newly created objects. They have the same name as the class.
Example: `public Car(String color, String model) { this.color = color; this.model = model; }` is a constructor for the `Car` class.
- A constructor signature is the constructor name followed by the parameter list which is a list of the types of the parameters and the variable names used to refer to them in the constructor.
Example: `Car(String color, String model)` is the constructor signature for a `Car` object.
- Actual parameters are the values being passed to a constructor. The formal parameters are set to a copy of the value of the actual parameters.
Example: In `new Car("red", "Toyota");`, `"red"` and `"Toyota"` are the actual parameters passed to the constructor.
- Formal parameters are the specification of the parameters in the constructor header. In Java this is a list of the type and name for each parameter (World(int width, int height)).
Example: `public Car(String color, String model)` has `String color` and `String model` as formal parameters.
- Call by value means that when you pass a value to a constructor or method, it passes a copy of the value.
Example: When you call `new Car("red", "Toyota")`, the constructor receives copies of `"red"` and `"Toyota"` as parameters.
- A new object is created with the new keyword followed by the class name (new Class()). When this code executes, it creates a new object of the specified class and calls a constructor, which has the same name as the class. E.g. World habitat = new World();
Example: `Car myCar = new Car("blue", "Honda");` creates a new object of type `Car`.
- There can be more than one constructor defined in a class. This is called overloading the constructor. There is usually a constructor that has no parameters (nothing inside the parentheses following the name of the constructor) like the World() constructor above. This is also called the no-argument constructor. The no-argument constructor usually sets the attributes of the object to default values.
Example: `public Car() { color = "black"; model = "Unknown"; }` is a no-argument constructor setting default values.
- There can also be other constructors that take parameters like the Turtle(habitat) constructor call above. A parameter (also called actual parameter or argument) is a value that is passed into a constructor. It can be used to initialize the attribute of an object.
Example: `public Car(String color, String model)` allows you to set the color and model during object creation.
- You can also declare an object variable and initialize it to null (Turtle t1 = null;). An object variable holds a reference to an object. A reference is a way to find the object in memory. It is like a tracking number that you can use to track the location of a package.
Example: `Car myCar = null;` declares `myCar` but doesn't create an object yet.
- The parameter list, in the header of a constructor, lists the formal parameters, declaring the variables that will be passed in as values and their data types.
Example: `public Car(String color, String model)` declares `color` and `model` as formal parameters.
- When a constructor like Date(2005,9,1) is called, the formal parameters, (year, month, day), are set to copies of the actual parameters (or arguments), which are (2005,9,1). This is call by value which means that copies of the actual parameter values are passed to the constructor. These values are used to initialize the object’s attributes.
Example: `Car myCar = new Car("red", "Toyota");` passes `"red"` and `"Toyota"` to initialize `color` and `model` in the constructor.
- Procedural abstraction allows a programmer to use a method and not worry about the details of how it exactly works. For example, we know that if we hit the brakes, the car will stop, and we can still use the brakes even if we don’t really know how they work.
Example: You can call `brake()` on a `Car` object without needing to know how braking works internally.
- Use dot notation to execute an object’s method. This is the object’s name followed by the dot (.) operator followed by the method name and parentheses: object.method();
Example: `myCar.accelerate();` calls the `accelerate()` method on `myCar`.
- A NullPointerException will happen if you try to call an object method on an object variable whose value is null. This usually means that you forgot to create the object using the new operator followed by the class name and parentheses.
Example: `myCar.accelerate();` will cause a `NullPointerException` if `myCar` is `null`.
- An object method or non-static method is one that must be called on an object of a class. It usually works with the object’s attributes.
Example: `public void accelerate()` is a non-static method that works with the object's `speed` attribute.
- A static method or class method is one that doesn’t need to be called on an object of a class.
Example: `public static int getTotalCars()` could be a static method that returns the total number of cars created, without requiring an object instance.
- Methods define the behaviors or functions for objects.
Example: `public void accelerate()` defines the behavior for accelerating a car.
- Some methods take parameters/arguments that are placed inside the parentheses object.method(arguments).
Example: `public void setSpeed(int speed)` takes an `int` parameter to set the car's speed.
- Values provided in the parameter list need to correspond to the order and type in the method signature.
Example: `public void setSpeed(int speed)` expects an `int` value for `speed` in the method call.
- A void method doesn’t return any value, the only point of calling one is because it does something that can be observed by the user or by other code.
Example: `public void accelerate()` changes the state of the car but does not return a value.
- Methods with a return type of anything other than void are called non-void methods. These methods return a value that the code calling the method can use. And because methods are called on an object, these methods can be used to return values that tell us things about an object’s internal state and Non-void methods typically do not have effects, and are called purely for the value they return.
Example: `public int getSpeed()` is a non-void method that returns the current speed of the car.
- It is up to the caller of a non-void method to do something with the return value, such as assigning it to a variable or using it as part of an expression.
Example: `int currentSpeed = myCar.getSpeed();` assigns the return value of `getSpeed()` to `currentSpeed`.
- The value returned by a method has to match the declared return type of the method. Thus it can only be used where a value of that type is allowed, such as being assigned to a variable of that type. The data type must match the return type of the method.
Example: `int currentSpeed = myCar.getSpeed();` works because `getSpeed()` returns an `int` value.
- Class names in Java, like String, begin with a capital letter. All primitive types: int, double, and boolean, begin with a lowercase letter. This is one easy way to tell the difference between primitive types and class types.
Example: `String name;` vs `int number;` — `String` is a class type, and `int` is a primitive type.
- Every class knows its parent class. But, in Java, a class can only have one parent. A class can inherit object fields and methods from a parent class.
Example: `class Car extends Vehicle { ... }` means `Car` inherits from `Vehicle`.
- Strings can be added to each other to create a new string using the + or += operator. This is also called appending or concatenating. You can also add any other kind of value to a String with + or += and the other value will be converted to a String automatically.
Example: `String fullName = firstName + " " + lastName;`
- Note that spaces are not added between strings automatically. If you want a space between two strings, then add one using + " " + and variables are never put inside the quotes ("") unless they are intended to be treated as part of a string.
Example: `String message = "Hello, " + name + "!";`
- You cannot create a new variable inside of a method with the same name as an already existing variable. Doing this would result in a naming collision.
Example: `int number = 5;` cannot have another `int number = 10;` inside the same method.