top of page

Classes in Java

You can think of classes in Java as the files that hold code. Classes or Java class files can be the blueprint for objects or can just simply be a space to execute code.

 

Objects in Java are all made of / instantiated from the class they belong to. Basically the class of an object has the details about the object's state and behavior.

image.png

Take the code snippet above showing the class for Dog objects.
1. These are called private instance variables, these are the attributes of our objects. In this instance the Dog has name, breed, color and weight.

​

2. This is called the constructor of the class which is where the object is created. The arguments are passed in via the user/client and are then stored in the private instance variables

​

3. These are behavior methods, which are unique methods that the object does. In this instance an Object of Dog class barks and howls.

​

4. These are called accessor and mutator methods or getter and setter methods. Which allows the client to retrieve and change values of the private instance variables.

​

5. This is called the toString() , which essentially prints out the details about the specific object when the client wishes to "print the object".

The bare minimum for a class is the private instance variables, constructor and the toString

Accessor & Mutator methods

Accessor and Mutator methods (also called getter and setter methods, respectively) are methods defined in the class, as shown in the example above (#4) , which retrieve and alter private instance variables from the object class. This is important because as the name suggests, private instance variables are private, which means that the values cannot be accessed from the client side and only from the class side. 

image.png

Accessor method: The accessor method or getter method simply returns the private instance variable. Accessor methods in the class do not have any arguments or parameters. Proper coding conventions call for the getter method to be called getVarName().

Mutator method: The mutator method or setter method is for when the client wants to alter the values of private instance variables. Mutator methods in the class  do have arguments or parameters, since we need to pass in the value we need the new value the piv will be set to. Proper coding conventions call  for the setter method to be called setVarName().

Objects

Objects as we know have state and behavior, which is defined by the class it belongs to. Object types almost always start with a capital letter and are only one word, otherwise if there are two words they are spaced using an _ underscore.

 

We declare objects using the keyword new

A common type of Object that we know already is String, which starts with a capital and has methods such as .equals .concat .length .substring etc.

loader,gif
image.png

In the code snippet above is the client for the Dog class, which shows the instantiation of two objects of type dog.

​

  • By calling the = new Dog("Benji" ,"labradoodle", "white/brown", 12.40) we call upon the constructor of the Dog class and pass the arguments into the constructor to make the object

  • To have an Objects call their methods simply just follow objName.methodName(//arguments here//) as shown above

  • As shown above, when we want to print an object it calls upon the toString() of the object class

Extend this subject in Polymorphism page

© 2024 by Samuel Wong.
Powered and secured by Wix

Call

(1) + 437-216-9455

Contact

@Brebeuf STEAM

  • Twitter
  • Instagram
bottom of page