Object and Classes

1. Creating a Class!

Java is an object-oriented programming language, which means everything in Java is associated with an object and a class (the blueprint for the object).

For starters, the line of code that we used to print statements from activity 1 is actually a method call from a predefined class called System!

System.out.println("Hello World");
  1. System is a class that deals with user input and outputs.
  2. out is a variable in class System of the class/type PrintStream.
  3. println() is a method defined in the class PrintStream.


Another class we have interacted with in the previous exercises is String. The String class defines a set of rules on how a list of characters should behave.

With following line of code, we created a String object called name using the rules defined in the String class:

String name = "Patrick";


System, PrintStream, and String are predefined classes in Java. However, we are not limited to these predefined classes, and we can actually create our own data type by writing a class! This is useful for coders to create specific objects to have certain attributes and behaviors. Having access to these user-defined types allows us to build distinct programs.

Let’s learn about the different parts in a class below:

Class

A class is a blueprint or prototype of a new type of object. In general, a class contains three important parts: (1) data fields/instance variables (2) constructor (3) methods.

  1. data fields/instance variables: These are the variables an object of this class has access to. For example: A Car class may have the field tire of class Tire.

  2. constructor: This is a method we call to create an object of this Class.

  3. methods: These are methods an object of this class will be able to call to perform certain tasks.

For example:

public class SampleClass{
    // (1) data fields/instance variables
    private String name; // example

    // (2) constructor
    public SampleClass(){
        name = "example";
    }
    // (3) methods
    public sampleMethod(){}
}

2. Try it Out πŸ₯!

Let’s make a Bird class to represent Patrick πŸ₯ and all of his Bird friends by following the steps below!

First, we start by defining the class name in the format public class name.

public class Bird{}


Second, let’s declare all the fields of the Bird class: species, name, hobby, age, loveMusic.

Each field is declared in the format: private data type name ;.

  1. We declare all 5 fields as private in the class Bird. This ensures these data can only be accessed within this class.

  2. Let’s identify the appropriate type for each field:

For example, to declare species as a private field of class Bird, you would put private String species; .

  1. Finish creating the other 4 fields of the Bird class!


Third, let’s create the constructor for the class Bird.

Usually, constructor is the method that initialializes value to all the fields in a class. It has the format public class name (parameter). Since we have 5 fields in this class, the constructor will take in 5 parameter/inputs.

public Bird(String speciesInput, String nameInput, String hobbyInput, int ageInput, boolean loveMusicInput){
    // constructor body
}

In the constructor body, we need to initialize all the instance variables, by assign each variables to its initial values:

species = speciesInput;
name = nameInput;
hobby = hobbyInput;
age = ageInput;
loveMusic = loveMusicinput;


Lastly, let’s create some methods for the class Bird.

We are gonna create 6 methods for this class!

Try to write out the first 5 methods with what you learned from the previous exercise about writing methods!


After, let’s write the toString() method together!

toString() is a method that returns the String representation of the object.

We will return a String that include all fields of the Bird: name, age, species, hobby, loveMusic by calling the 5 methods you just implemented!

public String toString(){
    // initialize variable info to have empty String
    String info = "";

    // append information to the String in the format: 
    // Hi, my name is [name]. I am a [age] year old [species] who likes to [hobby].
    info = info + "Hi, my name is " + getName() + 
           ". I am a " + getAge() + " year old " + getSpecies() + 
           " who likes to " + getHobby() + ". ";

    // if the Bird likes music, append the String "I also like music very much!" to info
    if(getLoveMusic()){
        info = info + "I also like music very much!";
    }else{        
        info = info + "However, I do not like music!";
    }

    // return the full String
    return info;
}

If you correctly implemented the Bird class, you will see the following message when you click Run:

Congratulations! You correctly implemented the Bird Class :)

If there is still something incomplete or incorrect when you click Run:

  • You might get an error such as Main.java:3: error: cannot access Bird.
  • Or, you will see the message, Something is still not quite right!.

3. Creating Birds πŸ₯!

Congratulations! You just wrote you first Java class Bird class. Now let’s learn to write a program that uses Bird objects!

We defined the Bird class to have the following attributes (instance variables) and behaviors (methods):

To create a new object of a particular class, we call the constructor of that class in the format class name variable name = new constructor call.

Recall that the constructor of Bird class is the following:

public Bird(String speciesInput, String nameInput, String hobbyInput, int ageInput, boolean loveMusicInput);

Hence, we can create a Bird with these attributes (species - duck; name - Patrick; hobby - hangout with friends; age - 15; loveMusic - true) with this line of code.

Bird patrick = new Bird("duck", "Patrick", "hangout with friends", 15, true);

Try it out yourself and create multiple Birds of different species πŸ¦πŸ€πŸ”πŸ§!


Next, let’s call the toString() method on these Bird objects we created to print the information of our friends. You can do either of the following:

  1. Print the return value of toString() method. (i.e. System.out.println(patrick.toString());)
  2. Directly print the Bird object, which prompt toString() to be called in the background. (i.e. System.out.println(patrick);).

Try it out and print out all the information of the bird friends you created πŸ¦πŸ€πŸ”πŸ§!