Class and Objects

Class and Objects

The objective of this blog will be to drive you through one of the important concepts in our coding journey, so the topic is Object Oriented Programming(OOPs). Before moving forward make sure you know the basics of Java like data types, control flow, etc.

Let’s start with our topic. We all have been used to applying basic data types in Java. Suppose someone asked us if we need to store the amount value so what data type will you use, we will go for either int, double or float. Let’s take another scenario, where you’ve been asked to store the Account holder name, so we will suggest storing it in String, and if asked to store 10 names of account holders, we can say use an Array of type String to store names.

But what if someone asked you to store ‘Account Details’ of 100 or let's say 1000 people, with Account Number, Account Name and Balance, this is the point when you will scratch your head and will look out for a solution on the internet and you will land on Object Oriented Programming concepts.

So to achieve the given target let us learn about classes and objects.

What is the class in OOPS?

Class is a named group of properties and functions. So we can create a class of whatever properties and functions we want, in our case we want a class to represent Account Details, so our class will look like

class Account
{
    String accountName;
    int accountNumber;
    float balance;
}

Note that, the class name starts with a Capital letter. Like our class 'Account' starts with capital A.

Now we want to store one account details, we can write it as,

Account John = new Account();

we will discuss the syntax after the equals sign, i.e new keyword and again class name later. But this line will create an object John with an account name of type String, account number of type int, and balance of type float.

What is an Object?

Above we created an object named John of class Account which will have its properties of class the accountName, accountNumber and balance.

So we can say that object is the instance of class. To simplify we can assume the class is like a template and its object has real values.

Like John, we can now have 1000 objects each with a different accountName, accountNumber, and balance.

From the above picture, we can see a class Account has multiple instances/objects with each different names and different values of class properties.

We can also see it as a class account is our data type and the object of that data type holds real values.

How to create Objects?

To create the instance of a class, we should write the class name and object name/variable name. see the example below.

Account John; //declaring object of class Account

Above we have just declared the object John. so you will get an error "variable 'john' might not have been initialized". so to create an object we should use the "new" keyword followed by the class name. The new keyword will dynamically allocate memory for the object i.e during run time and returns a reference to it in the variable.

Account John = new Account();

Note: All class objects in java must be allocated dynamically.

Now John is a reference variable to the created object. we can access the properties of john i.e accountNumber etc. We use "dot" operator.

System.out.println(John.accountNumber);

For example, in the above example to access john's account number, we will write ad variable_name.property_name.

So we have reached the end of our goal i.e understanding classes and objects. In the next blog, we will learn about constructors and 'this' keyword.

Subscribe to my newsletter to get updated about the next blog.

Did you find this article valuable?

Support Abhishek Madankar by becoming a sponsor. Any amount is appreciated!