Introduction
Java is object-oriented. What does that mean? confusing? Let me explain clearly. Unlike other languages, the object-oriented language is mainly focused on data. In, object-oriented language, you use classes and objects to organize your data. Class is the kind of idea behind a certain thing. An object is an instance of the class.
Class
Classes are the blueprints to create objects. Before, deep dive into classes let's see the problem that we faced. Assume, we are going to write a program about Cars. A Car had many attributes like the number of seats, price, horsepower, mileage etc.
In this example, if you would like to create another car type like Tesla, you should copy the whole code and rename the variables its a little bit complicated. These are the problems the class can solve. It also provide class-related methods. The class had two main elements:
States (fields) - the state can be anything represented to data. eg. number of seats, price and so on.
Behaviours - methods like startEngine(), drive(), getColor() and so on.
How to define a class?
To define a class you can start with the access modifier but it's optional then the class keyword and class name. Inside the class has variables and methods.
The class can declare once and it doesn't allocate memory when it's created.
Objects
The object is an instance of the class. Objects can create many times. In the case of Tesla, the objects can be redTesla, whiteTesla and so on. Object creation is similar to creating a reference data type variable. First, you need to type className then object name and equal to symbol, a new keyword at last class name(parameters).
How to access it?
To access object variables, you have to use objectName then dot and variableName. Next, if you want to access the method is very similar to object variables (i.e.) you have to use objectName then dot and methodName.
Conclusion
Thank you, This blog explains Classes and Objects. Through my blog posts, I aim to provide valuable information and practical tips. Feel free to leave comments, share your thoughts, and connect with me. For more future blogs follow me and also connect with me on LinkedIn.