What is a Class
To run our java programs we had to create something called a class. Inside of this class we had something called a method. Up until this point we have been using the rebuilt class and method that eclipse created for us. Now we will learn to create our own.
A class can be seen as blueprint for an object. When we create an object such as a String we are creating what's known as an instance of the class String. Somewhere in Java there exists a class called String that we used to create a new String object. The same can be said about other objects like an ArrayList. When we create a new ArrayList we are creating an instance of the class ArrayList and somewhere in Java there is a class called Arrayist.
Essentially a class defines the properties and attributes associated with objects that belong to it. Objects of the Integer class can be added/divided/multiplied etc. While objects of the Scanner class can be used to read in input. Each class has different properties.
So up until now we have been using classes without even knowing it! These classes are built into Java and were coded by other people.
Now we will work on creating our own classes and objects.
Eclipse makes it fairly easy to create a class, we can do so by right clicking on our package and clicking New > Class.
When we create a class we need to follow a few rules.
1. The class name must begin with a capital (Ex. Valid Class: Person, Invalid Class: person )
2. The class name must match the name of the file it is written in (Ex. Class Name: Tim, File Name: Tim.java)
In java we have something called access modifiers. These are words like: public, private and protected that allow you to define what scope a specific method, class or variable is accessible in.
Private: For methods and variables this means they are only accessible in the current class. In other words, they cannot be modified from outside of that class. A private class however means it is only accessible within the current package (not super important for our purposes).
Public: For methods and variables this means they are accessible from inside and outside of the class. Meaning you can change them from anywhere! A public class is accessible anywhere (kind of).
Protected: This is a special access modifier that we will talk more about later. It's very similar to private but allows for whats known as a child class or a derived class to access the parents class attributes or methods.
Most of our classes contain something called attributes, you may also hear these referred to as instance variables. These are variables that are specific to an instance of the class or an object of that class type. We define them using the private access modifier at the top of our class definition. You can think of these as the information associated with each object. They will be different for each instance of the class.
In the example below you can see an example of two attributes.
Notice that I don't declare the values of these attributes but I simply define them.
The constructor is a special kind of method that is called immediately upon creating a new object or instance of the class.
The constructor is where we construct or setup the object so it is ready to be used. We typically set all of our attribute values and call any necessary methods. To create a constructor we create a public method that is the same name of our class. We then can choose any necessary parameters that must be passed to our constructor. This will be the information that we need to pass every time we create a new instance of the class.
Now it's time to talk about this. The keyword "this" is what we used to access our attributes. You can see that in the above code we are setting "this" objects attributes to be the passed values.
A method is like a function. It is something that can be called on an instance of a class. When we create a method we need a few things.
- The access modifier
- The name of the method
- The return type
- The parameters
In the below example these values are as follows:
Access Modifier: public
Method Name: speak
Return Type: void
Now that we have created a simple class it's time to create some instances of it (otherwise known as objects). We are going to do this from our static void main method inside are other file.
Since we added two parameters to our Dog class constructor we need to pass two values whenever we create a new Dog.
If we call the methods on our dogs we can get some information to print to the console when we run the program.
Getters and Setters
Now that we've seen a basic example of methods lets talk getters and setters. Our last method simply printed our some information to the screen. This is great but sometimes we actually want to use a method to change or get the attributes associated with our object. To do this we need to create the following:
Getters: These are used to GET a value from our object. For example we want to get the age of our dog, so we create a getter method to do that.
Setter: These are used to SET or change a value associated with our object. For example we many want to set our dogs age.
Below you can see some examples of getters and setters on our dog class..