OCA 1: Classes and Objects

I’ll be talking about the basics, but still, I assume you are aware of basic terminology needed. I still will be explaining a few of the terms along, but I’d recommend you to read a book for beginners or any good online course like the one by John Purcell on his website. If you’ve been around Java for a while now and want to get certified, this would be a good place to come around time to time.

So let’s talk about the two most important terms in an object oriented language, Classes and Objects.

Class

In Java, classes are the building blocks. They are like the blueprint of an ‘object’ (an object is just an instance of a class, we’ll be talking about this later). A class defines the behavior and state of an object. For example, everyone has a mobile phone. Mobile phones basically all have similar functionalities like calling, texting and clicking pictures (every phone nowadays has a camera right? A person reading this, having a phone with no camera has no reason to exist) etc. Now these are the behavior of the mobile phones. Every mobile phone has a manufacturer name, a model number, IMEI number and all. This information is specific to a given mobile phones. Different mobile phone ‘objects’ will have different manufacturers and model numbers (well, they can be same but still…) and certainly different IMEI numbers. This all represents the ‘state’ of the mobile phone object.

So getting back to Java, the mobile phone is the class. The iPhone 7 is the object (Yes I know it’s a bad example, but can’t think of anything else). iPhone 7 can make calls, send texts and click pictures because all this behavior is defined in the mobile phone class.  iPhone 7 is manufactured by Apple, and the device name is iPhone and model is iPhone 7 and each device will have a different IMEI number. This is the state of the object. In Java, behavior is defined by methods (often called functions or procedures) and state is defined by fields (often called variables). These two together are called members of a class. Variables hold the state of the object and methods operate on the state (or we can say give behavior to the object).

Object

An object is an instance of a class. Like, a specific iPhone 7 mobile device is an object of iPhones. It has state and behavior defined by the class. Each object has a type, i.e. each object is related to a class. The class an object is made up of is the type of that object. Objects have their own set of instance variables (unless they are class variables i.e. static variables, we’ll talk about static later).

Software objects are quite similar to real world objects. Real world objects also have a state and behavior. My neighbors have a Golden Retriever named Ceaser. He is 5 years old. Now his name, his age and his breed define the state of that dog. Every normal dog can bark, can run, can swim and do all kinds of stuff dogs normally do. This is the behavior of dogs. So our class dogs would have a name, age, and breed as fields and bark, run and swim as methods which define what an object can do. The dog object will have its own name, its own age and its own breed. The dog object will be able to do everything a dog does by calling the methods on the object.


Now let’s see this in code. Let’s make a class Person. Each person has a name and an age. Our Person can tell his name and tell his age. So we define a class Person with name and age as fields and tellName and tellAge as methods.

public class Person{
    String name;
    int age;
 
    public void tellName(){
        System.out.println("My name is " + name);
    }
 
    public void tellAge(){
        System.out.println("I am " + age + " years old");
    }
}

Now that we have given the blueprint for a Person, we can now create Person objects in our main method.

public static void main(String[] args){
    Person person1 = new Person();
    person1.name = "David";
    person1.age = 32;
 
    Person person2 = new Person();
    person2.name = "Jack";
    person2.age = 27;
 
    person1.tellName();
    person1.tellAge();
 
    System.out.println("-------------------");
 
    person2.tellName();
    person2.tellAge();
}

The output of this will be:
My name is David
I am 32 years old
——————-
My name is Jack
I am 27 years old

We use the keyword new to create an object in Java. We made two Person objects, person1 and person2. Each object has its own set of variables and can call the methods defined in Person class. The type of person1 and person2 is Person.

This tells all the basics for classes and objects. There are a few more things that everyone should know.

  • There can be only one public class per source file with any number of non-public classes and the filename of the source file should be the same as the public class defined (If no public class is defined, the file name can be anything).
  • We use CamelCase convention to name classes and camelCase convention to name methods and variables.
  • Classes can only have two access levels, default and public. (Inner classes can have other access levels too)
  • Classes can be abstract or final, but cannot be both at the same time. (Will talk about this in coming posts)

This basically wraps up the topic classes and objects. There are a few things we haven’t talked about because we need to have knowledge of a few more topics before we can talk about them. We’ll cover that along the way.

Feel free to ask questions and to give ideas. I’d be glad of you can correct me somewhere. Just write it up in the comments section below.

Share this:
Share
«
»

Leave a Reply