Classes

In programming, classes are the templates/blue prints for all objects. All objects are created based on their blueprints/framework. Imagine we want to create eyeglasses; we must first look at the basic template of glasses. The basic structure of glasses are a frame and two lenses. This is all we need to create glasses. We can create different types of glasses by changing its frame color/type, lens power, shade etc. These are all changeable attributes of glasses, but no matter how many changes we make the glasses framework will stay the same. Glasses will always have a two lenses and a frame.
The same concept applies to classes in programming. Classes are the framework used to create objects, and once the object is created, it can be modified.

Usually each class is a separate file on your computer, but for the sake of simplicity, we will put all the classes we create in one file.

A basic class consists of just a class name and data fields,

class Box{          //The name of the class is Box
   int length=0;    //The data fields of the box are lenght, witdh, and height
   int width=0;
   int height=0;
}

So far, we have only specified the structure of the box. To bring the box to life, we must create the Box object where it would be used.
This is how we create an object: Class_Name variable_name = new Class_Name(); Ex.

Box mySimpleBox = new Box();
Box myOtherBox = new Box();  //Similar to Scanner input = new Scanner();

This creates 2 boxes that have the same properties as the Box class. mySimpleBox and myOtherBox have a length width and height variable, just like the Box class. We can easily access any of the variables by using a dot ‘.’

To assign a value to a variable We can simply access it like this;

mySimpleBox.width = 5; or mySimpleBox.length = 8;

We can also easily print the values like this;

System.out.print( mySimpleBox.width );

Program: Write a program that compares two boxes then determines whether box1 will fit in box2.
(hint – If the biggest dimension(length/width/height) of box1 is smaller than the smallest dimension of box2, then box1 fits in box2)

Right now, our box class does nothing interesting; to make it interesting we must add methods to the class.

Let us create two methods. Our first method will tell us the amount of space in a box (box’s volume). Our second method tells us the amount of cardboard we will need to make the box (box’s surface area).

Now in our main program, we can compare two boxes to determine which one is bigger (has more space).

After running this program, we will see that with the same amount of cardboard, we can create boxes with different volumes/sizes.
The output of this program will be

Amount of space in box1 is 120
Amount of space in box2 is 88
Amount of surface area for box1 is 148
Amount of surface area for box2 is 148
Notice that the code for the main program is always in a method that starts with “public static void main (String[] args)”. The computer will always look for this method and go to it first

.

Given the following class use the dot operator to access the class’ data

class Phone{
   String brand;
   String model;
   double osVersion;

   public static void outdatedOrNot(){
      if(osVersion<10.1){
         System.out.print("Outdated");
      }
   }
}

Suppose the phone object we create is called ‘myPhone’, to set the brand we can write myPhone.brand = “apple”;
Set the phone’s model to iphone 7
Set the phone’s OS Version to 9.3
check whether the phone is outdated

Constructors

A constructor method put simply, is a method that is ran once when you create an object. Any instructions needed to setup your object should be added to this method.

Imagine you decide to purchase a house, you can either purchase a house that already has the furniture / decorations, or you can purchase an empty house and decorate it later. A class with no constructor is like an empty house, it contains no data until you add it later. An already furnished house is like a class with a constructor, data is given to the object during the creation period, so that the object will contain the necessary data after it is created.

We can use a constructor in the box class to give the box its dimensions while it is being created, not after.

A constructor method follows a specific rule;

  • The name of the method must be the exact same name as the Class name.
  • Also the method cannot have a return type (int/String/void/boolean…)


As you can see here, the constructor method for Box requires 3 integers. Those integers are then assigned to the object’s data field.
To bring a class with a constructor alive (create object) we just have to include the parameters like this.

To show a different example, we will create a bank account Class

In this case, when a person creates an account, an initial amount is required. The person cannot open an empty account.

Given the following class use the constructor to create an object

class Phone{
   String brand;
   String model;
   double osVersion;

   public Phone(String brandInput, String modelInput, double osInput){
      brand = brandInput;
      model = modelInput;
      osVersion = osInput;
   }
}

Suppose the phone object we create is called ‘myPhone’. Ex Phone myPhone = new Phone (x,…);
Create a phone that has a Samsung brand, Galaxy 8 model and 7.1 OS

Program: Write a program that uses the Box class. The program must ask the user for a box’s dimensions then create a box with the user’s dimensions. The program should ask for dimensions again to create a second box. After both boxes are created, the program should display the volume of both boxes.

Ex. output: 
Enter box1 width: 1 
Enter box1 length: 4
Enter box1 height: 2

Enter box2 width: 2 
Enter box2 length: 5
Enter box2 height: 2

Box #1 has a volume of 8
Box #2 has a volume of 20
Back to Course Menu
What do you think about this topic? Share your suggestions / comments