๐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 human1
refer to the same object as human2
.
๐นSo, any changes made to the object through human2
will affect the object to which human1
is 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!