A beginner's guide to Classes and Objects in Java

A beginner's guide to Classes and Objects in Java

Classes and Objects are one of the most if not the most fundamental part of the java programming language. It makes your code more meaningful and helps you to organize your code in an efficient manner. They are the key parts of the object-oriented programming paradigm.

It is also one of the most confusing parts I've come across when I was starting with Java and if you are finding it difficult as well, don't worry you are not alone cause if you look closely there's really not much hell of a difference between the two. So, I'll try to explain it in as simple words as possible keeping beginners in mind.

Class

A class is a specification or a logical template that an object follows. It defines the characteristics and functions of an object and makes it logical in nature. By manipulating these characteristics and functions you can create objects of different types. A class doesn't have any physical existence so it doesn't take up any memory space.

A class of something (e.g., laptops, mobile phones, books, cars) can consist of a number of objects.

To create a class we write the following line

public class className {
.
.
.
}

Object

An object is actually a physical thing that you interact with in your day-to-day life. e.g., mobile phone, laptop, car, tv, books, etc. Everything which you can see around you right now is considered to be an object. It is a real-world entity.

To create an object we write the following line which means create a new object named objName of type className. The new operator allocates memory for an object.

className objName = new className();

Consider this example

Let's say, You want to buy a laptop for starting with your programming journey. So, you start thinking about the requirements or the specifications e.g., brand, size, ram, storage, graphics, etc.

In this example consider the laptop as a class and the brand, size, storage, ram as its characteristics or attributes.

public class Laptop {
String color;
int screenSize;
String brand;
String processor;
}

What do the above lines mean?

It's just a logical template that is simply telling us that a laptop has a color, a definite screenSize, brand, processor, etc but we don't have any actual laptops yet, for a laptop to actually exist in real life we need to first create an object of type laptop and then put some values in those attributes. So, the first step will be just to create an object.

Here we create 3 objects of type Laptop

Laptop laptop1 = new Laptop();
Laptop laptop2 = new Laptop();
Laptop laptop3 = new Laptop();

As we now have memory space allocated for laptop1, laptop2, and laptop3 let's manufacture some physical copies of laptops from the factory (class Laptop) using the above-mentioned template.

laptop1.brand = "Razer";
laptop1.color = "Blue";
laptop1.size = 15.6;
laptop1.processor = "Intel";
laptop2.brand = "Macbook Pro";
laptop2.color = "Space Gray";
laptop2.size = 13;
laptop2.processor = "Apple M1";
laptop3.brand = "Lenovo";
laptop3.color = "Black";
laptop3.size = 14;
laptop3.processor = "Intel";

Add a little bit of body text.png

So by putting some values in those attributes we got three models of laptops to choose from to make our ultimate buying decision.

  1. Razer Blade 15
  2. Macbook Pro 13
  3. Lenovo Legion Slim 7i

Things to Note

  • A class is just a blueprint or a template that doesn't have any physical reality in nature.

  • An object is a physical entity or an instance of a class that actually exists in real life. It heavily depends on the class definitions. (think about it for a second if we wouldn't have known that a book has a number of pages, or color, or even say something stupid we would have never been able to create a physical copy of a book. Makes sense, Right! I hope it does).

Thank you for reading! Do share your thoughts about it.