Good day to every reader,
Today I am going to introduce you to a new programming language namely "Java". It was first developed by Sun Microsystems, which was acquired by Oracle in 2010. Java is vastly used all around the world by most programmers, especially by those who seek portability and security in programming.
Basically, Attributes are what make up an Object (or a Class). You can think of them as the ingredients of a recipe — The recipe itself is a Class, while the food cooked based on it is an Object.
As you can see above, there is no way for the user to set the balance of the account because there is no such Method provided.
Today I am going to introduce you to a new programming language namely "Java". It was first developed by Sun Microsystems, which was acquired by Oracle in 2010. Java is vastly used all around the world by most programmers, especially by those who seek portability and security in programming.
JAVA AND OOP
Java is an OOP (Object Oriented Programming) language, meaning it concerns more on objects and their attributes as well their methods than on the procedure of how the program runs — like in the C Programming Language which is also known as the procedural programming language.JAVA vs. C
Despite the differences between the two languages, knowledge in C Programming is preferably required because the syntax of a Java code is pretty much similar to that of a C code. Some new keywords I will explain today are: Class, Object, Attribute, and Method.DEFINITIONS
Classes and Objects
I will explain these two terms with an illustration:
Let's say you have a Printer. We all know that there are many manufacturers of printers. There are Canon printers, HP printers, Xerox printers, and so on.
When you talk about a Printer, you are actually talking about printers in general, which is abstract.
But when you talk about a Canon Laserjet Printer, you talk about a real thing, something that exists.
In this case, Printer is a Class and a Canon printer is an Object created based on the Printer Class.
To sum up, a Class is a blueprint of an Object, and an Object is a physical representation of a Class.
Attributes
An Object (or a Class, which is actually the same, because an Object is created based on a Class) has certain attributes.
Examples:
A box has width, height, depth, surface area, and volume.
A phone has price, manufacturer, type, dimensions, weight, etc.
A car has maximum speed, price, manufacturer, type, color, number of doors, number of tires, etc.
Basically, Attributes are what make up an Object (or a Class). You can think of them as the ingredients of a recipe — The recipe itself is a Class, while the food cooked based on it is an Object.
Note:
The Attributes of a Class (or an Object) must be declared as private, so it cannot be accessed other than through its Methods. This will be explained in the next post.
Methods
Methods are (or should be) the only way you access the Attribute of a Class (or an Object).
If not, you can imagine if someone has access to your program, he can easily set the attributes' values to those he wants.
Let's assume that we have an Object, an ATM.
We all know we can withdraw money using the ATM, provided that there is enough money in our account.
There will be Methods, supposedly withdrawMoney(), setUser(), and getBalance().
First, an ATM user must insert the card, containing their user info into the ATM.
As it is inserted, the ATM calls its Method, setUser(), to set the current user to the card holder.
Next, it will call the next Method which is to getBalance() of the account number which is already provided by the card (obtained using the setUser() Method).
Finally, the user will try to withdraw the money by inputting the amount of money he wants to withdraw. The withdrawMoney() Method will then be called to check whether the amount requested is available in the account. If such amount (or more) is available, the ATM will withdraw the requested amount of money, end the transaction, and eject the card.
As you can see above, there is no way for the user to set the balance of the account because there is no such Method provided.
As this is an introductory post, I will stop right here and continue to expose more about Java and the OOP concept in the next posts.
Happy Coding,
Cyber Frost