-
Notifications
You must be signed in to change notification settings - Fork 0
Classes
A class is basically a blueprint of any Java program. It contains all of the elements to make a program work. A class has attributes as well. Attributes are characteristics that define an object. Attributes are the properties of the object.
An object is a concrete instance of a class. When you create and an instance of a class this is called instantiation. You have done this before when you create an instance of a variable. Think of dogs as an example. Let's say you have a dog class. This class is the blueprint for other dogs. This class has attributes such as name, size, age, owners. These attributes will be the same for each object of the class but they may be different between each object. For example, one dog may have the following attributes:
- Name: Spot
- Age: 1
- Size: 4 pounds
- Owner: Tom
Another dog may have the following attributes.
- Name: Dolly
- Age: 3
- Size: 15 pounds
- Owner: Mary
These are two completely different objects but the have the same attributes can come from the same class. A class can have an unlimited number of objects.
When you create a class it has three main parts. It has an access specifier (optional), the keyword class, and a name for your class. An access modifier is how the class will be access in the program. The most common access modifier is public. Public means that any object in the program can access the class. Public classes can also be extended. Extended classes can be used as the basis for other classes. There is also a private access modifier. A private access modifier makes it so that no other methods in other classes can access the class. Below is an example of how you may set up a student class.
public class Student {
private int stuNum;
}
As you can see the Employee class has what called stuNum. This means that any object that is created from the Employee class will have stuNum as one of its variables.
Let us say we wanted to create a freshman object from the student class. That would look something like this
Student freshman;
freshman = new Student();
Instantiating objects is similar declaring a variable first you must declaring the data type of the object. This is where the "Student freshman" line come in. It is telling the compiler that there will be a new object called freshman and it will be from the Student class. This above student object is also known as a default constructor. A constructor is what creates the object when you instantiate it. You will learn more about constructors later.
Now that we know how to instantiate objects and create methods for other classes we can now combine them to use inside of the main method. You can learn more about methods here. Let's say you have a Student class that you want to use to set the student's number. We then want to take this information, pass it to the Student class and return it to the main class. That would look like this.
Main Class
public class Classes {
public static void main(String args[]){
Student newStudent = new Student();
newStudent.setName("Thomas");
System.out.println("The student's name is " + newStudent.getName());
}
}
Student Class
public class Student {
private String stuName;
public void setName(String name){
stuName = name;
}
public String getName(){
return stuName;
}
}
Output
The student's name is Thomas
Above you can see that there are two classes, a Classes class, and a student class. For the Student class, you can see it has two private methods. These methods are known as getters and setters. They do exactly what their name says. The getters get the information from the outside class. Getters can use this data in any way. If you have a math program you can use a getter to find the sum of a number from the main class and add it to another number. Setters are used to returning the information in a certain class to the main class. For the example above we have a getter and setter for the student's name.
To use the getter and setter of the student class we first need an object from that class to take advantage of them. To do this a newStudent object is made from the Student class. Next, you need to make the getters and setters for the Student class. In the Student class, you can see that there are 2 methods, a getName method, and a setName method. For the setter method, you can see that it does not have a return type but it requires a String as an argument. This is because the setter method only gets the information. The setter method also sets the stuName variable, which is a local variable of the student class, equal to the name variable which is the name of the variable that is passed to it from the external class. Next is the setter. The getter is different from the setter because it has a return type but it doesn't require you to pass any arguments to the method. This is because all the getter does is get the value of a specified variable and returns it using the return statement.
Now that we have the getters and setters made we need to use them. To use the objects and the methods of the class together you need to call the methods using the object. So first you must set the student name using the setName method.
newStudent.setName("Thomas");
This above line is basically telling the compiler to set the name of the newStudentObject to Thomas. Now that we have the name set we can use the getter to get it back.
newStudent.getName();
This line of code outputs whatever value is stored as the name for that specific object. This is unique for each object of that class. You could create a new Student class object called oldStudent and it would not have the same name as the newStudent object.
It may seem unnecessary to use getters, setters and separate classes in general. It may be easier to just set all of this information in the main class. This is very unsafe though and violated the concept of the black box and encapsulation. Encapsulation is the concept that breaks down into two different area. The first area is that encapsulation is the enclosure of data and methods into an object. The other area is that encapsulation is meant to hide objects and data from outside sources. When you hide data of the method this is referred to as information hiding. When you hide how the methods in the class work this is known as implementation hiding. Implementation hiding is known as the black box because you may not know what happens inside of the black box but you just know that it works.
A good example of where encapsulation is neccessary is if you have an app that handles lots of personal information. You don't want this information to be public to everyone so you might store it in another class that hadles all of it and only the main class can access it. This way you reduce the risk of the information being access by outside sources such as other users.