Java Classes
Classes:
- A class defines a new complex variable type which can have its own variables and functions to access
- Variables defined in a class are called "member variables", and functions defined there are called "methods"
- When you create a variable of this type, it is called an Object
- An object has the variables and functions defined in the class, and they can be accessed with the object name followed by a period followed by the method/variable name
- If you replace "public" with "private" before the method or member variable, it will only be able to be accessed from within a member function definition
- If you pass an object as a function parameter value, changes made to the object within the function will remain changed out of the function
Simple Getters and Setters:
- It is best practice to keep member variables private and to modify or retrieve them through functions, typically called getters and setters because of what they do
- This allows you to add code such as to ensure that invalid data is not set, or that the value being retrieved isn't an empty variable (see next example)
- By convention, the getter and setter functions will be named with "get"/"set" followed by the function name starting with an uppercase letter
Practical use of Getters and Setters:
Basic Constructor:
- A constructor is a function that is executed when you create an object (in this case, using the "= new MyClass()" function )
- It must have the same name as the class
- It is useful for setting the start values for the member variables, especially complex variables that can't get a value simply with the "=" operator
- "this." means that it is referring to the member variable in this class (so as not to be confused with other variables with the same name)
Alternative Constructors:
- You can make alternative constructors distinguishable by receiving different parameters
- In the example above, the second constructor would be used by a call with an integer passed, e.g. MyClass myObj = new MyClass(12);
- If you define a constructor with parameters and want to also be able to access a constructor without parameters, you must also define the constructor without parameters (which otherwise would have existerd by default)
Copy Constructors:
- Copy constructors allow you to create a new object with the same values of an existing object that gets passed as a parameter
- When making a copy of an object, do not try to give it values with the = operator (e.g. myObj1 = myObj2;), that will cause both objects to reference the same data (so modifying one would modify both)
Imported Classes:
- For a class available from the internet (or for your own classes defined in a different package), you must import them in order to use them
Challenge
Create a class called "Dog" that stores its name, weight, breed, and whether it
is neutered; create getters and setters for its variables, and create a single
function that will display all of the details. Create a constructor that takes
parameters for each of the variables, and a constructor that takes none. If a getter
is called for data that is not filled, return a default value of 0, false, or "none".
In the main
class, create two instances/objects of this class (one with the parameters and one without).
For the object created without parameters, call a getter to ensure that the default value is returned,
then set the values with the setters, and print the values accessed with the getters.
Finally, run the display function for the object created with parameters.