Introduction: Classes and Objects, what are they and for what they serve



The concept of object orientation is, undoubtedly, the most important in Java programming language.
And that is why we will teach from the beginning, in our Java course.

Being a somewhat abstract concept, it is normal to take a while to understand everything. And to get it right, it will take longer.
However, we teach over several tutorials, divided into various sections, such as important details.

In this part, as we have seen only the basics of Java programming and loops, we only teach how to declare classes, instantiate objects (with and without constructors) and how to use conditional tests within the classes we create.
This will give an idea of ​​what are classes and objects, and it is important you get used to these ideas.



What Classes and Objects are in Java


As we mentioned in our article with explanations on the first program that we created in Java, classes can be seen as abstractions or more definitions of things and object is already something more real, more concrete, is an element of that type or class.
Using these definitions, it is really hard to understand the concepts, so we'll use examples.

For example, we see "Person" as a class. It is a class that represents humans, having head, heart, brain, etc. It is a generalization greater.
We can declare you, dear reader, as an object of this class, with your name and the specific characteristics of a "person." You belong to class "Person". I also, as we have head, heart, brain etc..
We have these general characteristics, all class "Person" feature. That is, in Java, we say we are instances of the class "Person".

Usefulness of Classes and Objects in Java

This kind of representation will help us a lot in Java programming.

For example, imagine you've been hired to create an application for a company - in Java, of course.
You have to register thousands of company employees.
Of course you will not declare thousands of strings to store the name of each person, neither of integers to store its ID numbers nor floats to store their earns.
Is humanly impossible to do that.

Group similar things
Now you will enter in the advantage of object-oriented programming. We can see all these employees the same way: as members of class Employee.

What the class "Employee" have in common?
It has a name, age, date of hire, a paycheck, a sector in which they work and other things specific to the company.

Ready.
You can see this class as a data type.
Just like 'int' or 'float', there is now the type 'Employee'. Every time we get someone new in the company, you represent that element as part of type 'Employee'. In other words, you are creating an object of that class.
The object, different from class, is something more specific, he will have the personal information of each employee.

Create once, use many times you want
The great advantage of this kind of 'vision', is when you declare the a class, you declare types inside it: string, float, int etc, which are within the class.

Then, when you create an object, this data will be created automatically!
See the beauty of Java and object orientation programming. It is powerful, very useful and practical. It is a new way of thinking and seeing the world. We say that is a kind of different programming paradigm.

Change one part of the code, and the change will propagate throughout the code
Okay, you created your application using the "Employee" class.

However, the person who hired you - who are often not the IT field - forgot to inform you that the staff should have provide another information on they registration: If each one have a car or not.
And now? Change everything? Start from the beginning?

Of course not. Just go and put this in the class Employee attribute (information), and automatically all objects will have that data, "car".
So just ask the staff to fill this information in his application for registration.

Class apart, life apart
One interesting thing that object-oriented programming gives us is the division of parts of the program. Two programmers can program two classes completely independents and make them work perfectly.
This kind of feature in other programming paradigms is almost impossible.

For example, you created the class "Employee".
In this class you need salary information for each employee, but you do not have access to the financial details of the company. But you don't need it, you're programmer, and its kind of information are more restricted to executive employees.

Another programmer, responsible for finance, may have created the class "Salary" that receives the data of each person, their position in the company, bonus, overtime etc etc, and gives you only the end result: the number. This information is what you will use in your class "Employee".

This can all be done in a fully efficient, safe, independent way by you and the ther programmer.
The only exchange of information is that you receive a float from him and he takes the name of employee or his company's code.

The most important thing about this is: at no time was needed to see the code of the other!
In time to work, it tells us a lot in terms of income!

How to know when to use Classes and Objects in Java

As you can see, through the examples of cars, people and employees, Classes are nothing but a bunch of information. Whenever you want to use this information, declare an object of that class.
That is, whenever you want to generalize or create a group with similar characteristics, have no doubt, use Class and Objects.

In a game, Worms Armageddon or Counter-Strike, for example. Several players. Now they look alike, have similarities and common features. Do not waste time declaring their attributes individually, use classes and objects.

In fact, in Java, all objects are classes and then there is no this concern about when create classes.
Our main program, which contains the 'main ()' is a class. To receive data, use the Scanner class.
In C, there is no Object Orientation.
In C + +, if you choose to use or not.

Further, in our online and free Java course, we find that a class might look like over and over, get 'borrowed' from other detail, implement or extend other.
There are several resources to be used in classes, including security, that allow some objects have access to some data, others do not.

Graphics: for example, Java application windows, buttons, menu and everything are classes.
As we said, revolves around Java object orientation, and Java is a programming language rich and unlimited. So what can be done with classes and objects is also quite wide.

No comments: