-
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 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 an 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 know 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. 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;
private int stuNum;
private int stugrade;
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