Back to Top
Learn Java By Usama Screenshot 0
Learn Java By Usama Screenshot 1
Learn Java By Usama Screenshot 2
Free website generator for mobile apps; privacy policy, app-ads.txt support and more... AppPage.net

About Learn Java By Usama

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.

Similar Apps

Testo Recipes

Testo Recipes

0.0

Introducing the Testo food recipe app that brings you thousands of delicious...

Learn Bitcoin Cryptocurrencies

Learn Bitcoin Cryptocurrencies

0.0

Bitcoin is a decentralized digital currency that allows people to make transactions...

Iphon Development

Iphon Development

0.0

this app is for iPhone app development learning for developer step by...

Learn Java By Moiz

Learn Java By Moiz

0.0

Have you ever wondered why you should learn Java or what types...

Duas(Zikr-e-Allah)

Duas(Zikr-e-Allah)

0.0

This app contain duas for all muslims which they do in daily...

Speech to text converter shobi

Speech to text converter shobi

0.0

This app is covert your voice into text is very use...

author
Greate app easy to learn java
A Google user