Learn Java By Usama

Learn Java By Usama

NSOFT
Sep 10, 2019
  • 5.0 and up

    Android OS

Learn Java By Usama के बारे में

Lear Java प्रोग्रामिंग लैंग्वेज इजी वे ...

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.

अधिक दिखाएं

What's new in the latest 1.0

Last updated on Sep 10, 2019
Minor bug fixes and improvements. Install or update to the newest version to check it out!
अधिक दिखाएं

वीडियो और स्क्रीनशॉट

  • Learn Java By Usama पोस्टर
  • Learn Java By Usama स्क्रीनशॉट 1
  • Learn Java By Usama स्क्रीनशॉट 2
APKPure आइकन

APKPure ऐप के माध्यम से सुपर तेज़ और सुरक्षित डाउनलोडिंग

एंड्रॉइड पर XAPK/APK फ़ाइलें इंस्टॉल करने के लिए एक-क्लिक करें!

डाउनलोड APKPure
thank icon
We use cookies and other technologies on this website to enhance your user experience.
By clicking any link on this page you are giving your consent to our Privacy Policy and Cookies Policy.
Learn More about Policies