[OOP 1] Introduction & Concepts - Classes, Objects

[OOP 1] Introduction & Concepts - Classes, Objects

- in JAVA โ˜•

ยท

7 min read

๐Ÿ“—Introduction

๐Ÿค” Imagine you have a box ๐Ÿ“ฆ that contains information about any living being, such as their gender, number of eyes, nose, legs, and other characteristics. This box could hold details about a human, an animal, or even a fictional creature! ๐Ÿฆ„๐Ÿต๐Ÿ‘ฉ

By organizing this information into a box, we can think of it as a Class in Object-Oriented Programming (OOP) ๐Ÿ’ป. Each object, or instance of the Class, would have its own unique properties and behaviours.

For example, we could create a Human Class with properties like gender, hair colour, and height. We could also add methods to the Class, such as walk() or talk() ๐Ÿšถโ€โ™€๏ธ๐Ÿ’ฌ.

And me` Amigos this is OOPS๐Ÿซก. OOP provides a powerful way to organize and manipulate data in our programs. By breaking down complex systems into smaller, more manageable pieces, we can create more efficient and scalable software ๐Ÿš€

๐Ÿ“—What are Classes and Objects?

A Class is like a blueprint ๐Ÿ“ for creating objects that share similar properties and behaviours. In this case, we could create a LivingBeing Class that has properties like gender, number of eyes, nose, and legs, and functions like sound() or move(). ๐Ÿšถโ€โ™€๏ธ๐Ÿ‘ƒ๐Ÿ‘€๐Ÿฆต

--> Now๐Ÿค”, note that you can create any living being using these properties ๐Ÿ‘€๐Ÿ‘ƒ๐Ÿฆตetc.

Think of the LivingBeing Class as a ๐Ÿ“ template for creating different objects that are all living beings. Each object would have its own unique set of values for these properties, such as male or female for gender ๐Ÿ‘ฉ๐Ÿ‘จ, or two eyes๐Ÿ‘€ versus four eyes ๐Ÿ‘€๐Ÿ‘€๐Ÿ‘€๐Ÿ‘€.

By creating a LivingBeing Class, we can create as many objects as we need, and they will all share the same properties and behaviours. This makes our code more organized, reusable, and easier to maintain. ๐Ÿš€๐Ÿ‘จโ€๐Ÿ’ป

So, for example, we could create a Human object from the LivingBeing Class, and it would have its own unique set of properties and behaviours. We could also create an Animal object from the same Class, and it would have its own unique set of properties and behaviours. ๐Ÿง‘๐Ÿถ

Now, Here's something technical

๐Ÿ”นWhen you declare an object of a class, you're basically creating an instance (Visualise it like: Something in reality, physical reality) of that class. This means that you're creating a specific livingBeing, with its own unique structure, gender, and organs.

๐Ÿ”นSo, a class is a logical (non-real) construct, while an object has physical reality. In other words, a class is like a recipe for a cake, and an object is like the actual cake that you can see and touch.

๐Ÿ”นAnd just like how you can use a recipe to make multiple cakes, you can use a class to create multiple objects. This is what makes classes and objects so powerful in Java, and why they're used so extensively in software development.๐Ÿ˜Š

โœ…Now, Let's Create a Class

You can use the keyword class to create a new class.

public class LivingBeing {
  int eyes;
  int nose;
  int legs;

  String soundType;

  void sound() {
    System.out.println("I make this " + this.soundType + " sound!");
  }  
}

โœ…Now, Let's Create an Object

In Java, you create an object from a class. Since we've already created a class called LivingBeing, we can use it to make objects. To create an object of LivingBeing, you just need to specify the class name (LivingBeing), followed by the object name, and then use the keyword new.

public class Main {

    public static void main(String[] args) {
        LivingBeing human = new LivingBeing();
        human.eyes = 2;
        human.nose = 1;
        human.legs = 2;
        human.soundType = "Tamil";

        human.sound();
    }
}

๐Ÿ–๏ธ Let's talk more about creating objects in Java! In Java, to create an object, you must first declare a reference variable. A reference variable is a variable that holds the memory address of an object. You can declare a reference variable by specifying the class type followed by the variable name.

LivingBeing human = new LivingBeing();

The class name followed by parentheses specifies the constructor for the class. A constructor defines what occurs when an object of a class is created.

Now, Let's look at another example,

LivingBeing human1 = new LivingBeing();
LivingBeing human2 = human1;

๐Ÿ”นHave you ever wondered what happens when you assign one object reference variable to another object reference variable in Java?

๐Ÿ”นWell, when you assign one object reference variable, let's say human1, to another object reference variable, let's say human2, they will both refer to the same object in memory. It's important to note that this assignment does not allocate any memory or copy any part of the original object. It simply makes human1refer to the same object as human2.

๐Ÿ”นSo, any changes made to the object through human2will affect the object to which human1is referring since they are both the same object. That's why it's crucial to understand that when you assign one object reference variable to another, you're not creating a copy of the object. You're just making a copy of the reference.

๐Ÿ”นIt's essential to keep this in mind while working with object references in Java.

๐Ÿ†—! Now, Let me explain how the dot operator works in Java, and what the new keyword does.๐Ÿค”๐Ÿ’ญ

๐Ÿ”นThe dot operator is used to link the name of an object with the name of an instance variable. It's kind of like a connector ๐Ÿ”Œ that allows you to access the properties and methods of an object.

๐Ÿ”นNow, even though it's commonly referred to as the dot operator, the formal specification for Java actually categorizes it as a separator. But either way, it serves the same purpose: to connect the object with its variables and methods.

๐Ÿ”นWhen you use the new keyword in Java, it dynamically allocates memory for an object at runtime. This means that the memory is allocated as the program runs, rather than being reserved in advance. ๐Ÿ’ป๐Ÿ‡

๐Ÿ”นThe new keyword then returns a reference to this object, which is essentially the address in memory of the object that was allocated. This reference is stored in a variable, which can then be used to access and manipulate the object. ๐Ÿ”—

๐Ÿ”นIn Java, all class objects must be dynamically allocated, which means that you must use the new keyword to create objects of that class. This is an important concept to understand in Java programming. ๐Ÿง

๐Ÿ‘‹ Have you ever wondered why you don't need to use the new keyword when creating variables of primitive data types, like integers or characters, in Java? ๐Ÿค” Well, that's because Java's primitive types are not implemented as objects, unlike classes, which are implemented as objects.

Primitive types are implemented as "normal" variables, which makes them more efficient to use. On the other hand, when you use the 'new' keyword, Java allocates memory for an object during runtime. This memory allocation is essential for creating instances of classes, but for primitive types, it is not necessary.

So, the bottom line is that the new keyword is not required when working with primitive types in Java, but it is crucial for creating objects from classes. Hope that clears things up! ๐Ÿ˜Š

๐Ÿ†—! Now, a few more things to know!

You can create multiple objects of one class:

public class Main {

    public static void main(String[] args) {
        LivingBeing human1 = new LivingBeing(); //human 1
        LivingBeing human2 = new LivingBeing(); //human 2

    }
}

๐Ÿ“—Summary

In Java, everything is associated with classes and objects, where a class serves as a template for creating objects with attributes and methods. When you declare an object of a class, you are creating an instance of that class, and the new keyword dynamically allocates memory for the object and returns a reference to it. In Java, all class objects must be dynamically allocated. The dot operator is used to link the name of the object with the name of an instance variable, and Java's primitive types are not implemented as objects for efficiency reasons. When you assign one object reference variable to another, you are not creating a copy of the object, you are only making a copy of the reference. Understanding these concepts is essential for effective Java programming.

Thank You Soo Much for your valuable time.๐Ÿ˜Š๐Ÿฅณ๐Ÿ‘‹

๐Ÿ‘‹ Hi there! Let's connect and collaborate!

Here are some ways to reach me:

๐Ÿ”น GitHub: github.com/mithindev

๐Ÿ”น Twitter: twitter.com/MithinDev

๐Ÿ”น LinkedIn: linkedin.com/in/mithin-dev-a-397983247

Looking forward to connecting with you!

ย