Learn Java By Usama icon

Learn Java By Usama

★★★★★
★★★★★
(0.00/5)

1.0Free4 years ago

Download Learn Java By Usama APK latest version Free for Android

Version 1.0
Update
Size 3.39 MB (3,557,018 bytes)
Developer NSOFT
Category Apps, Education
Package Name com.uzziqam.javabooks
OS 5.0 and up

Learn Java By Usama APPLICATION description

Lear Java programming Language Easy Way...
One of the fundamental concepts in object-oriented programming is encapsulation. Encapsulation is the process of wrapping data and methods into a single unit, in our case a class. In other words, encapsulation is a way to make your programs more secure by preventing unauthorized classes and objects from accessing certain variables and methods. Now why would you want some objects not to access certain variables? Think of your bank account. You aren't allowed to withdraw money from your account unless you have money.

You can't deposit a negative amount, and you shouldn't have direct access to change your balance. Let's take a look at this type of example in code. Here we have a bank account class. We have two private properties: account number and account balance. Then we have a constructor where we give it the account number and account balance and set those properties appropriately and then print out the fact we've created an account. Next on line 12 we have a method called deposit, and this allows us to deposit money in our account.

You might notice we have some conditions here to make sure we don't deposit a negative amount. And so here we have deposit(int addMoney). If addMoney is negative, then we print out you can't deposit a negative amount because you can't do that in the real world. Otherwise, if it's a positive number, then we go ahead and add that money to the account balance and then print out the fact we've added that money. We also have a method called withdraw here on line 24. And here we make sure that we're not removing more than we have in our balance.

And if what we want to remove is more than our account balance here on line 25, then we just print out the fact you can't remove more than what's in your account. If removeMoney is equal to or less than what we have in our account, then we go ahead and subtract it here in this else statement on line 28, and then print out the fact that we've withdrawn that amount of money from your account. Now where does encapsulation come into this? Well, if you scroll back to the top here we have these private and public keywords.

We don't want everyone to have access to account balance and account number. And so here we've made them private and then we've allowed for error checking inside of our public methods. And so the only way that the user can access our private account number and our private account balance is through these methods that are public which are deposit and withdrawal. This allows us, as software developers, to control how the user is using the account number and the account balance because they don't have access to them from the main method.

Let's try building an instance of this bank account class. So we'll go back to our main method here and we're going to write BankAccount, myBankAccount is what we're going to call it. And we misspelled Account and so we'll put that in. And then the way we're going to create it is we're going to use the constructor with new BankAccount and then the first number we're going to put in is the account number, so we'll call it 4141, and then the amount of money that the account is going to have, the account balance, and that's going to be 100.

Now let's see if we can access myBankAccount's account balance. To do that we could go System.out.println and this would print it to the console and what do we want to print out? myBankAccount.account_balance cause that's what the variable is called. And we get an error. And then here the error says account_balance has private access in BankAccount. So because it has private access we can't directly access it here in the main method.
↓ Read more
Learn Java By Usama screen 1 Learn Java By Usama screen 2 Learn Java By Usama screen 3