Java / Android Developer, Trainer, Writer, Speaker

Thursday, February 5, 2015

On 12:26 AM by M Rubel in ,    12 comments

Like the article?

Hi to All!
Today we are going to learn another JAVA OOP concept which is Encapsulation. Let's Start:

Encapsulation in JAVA

Let's go through the files:


1. myEncapsulation.java Class


/*
 What is Encapsulation?
 Ans: Encapsulation is something like hiding data. 
 Encapsulation is a system by which we can keep things in a class secret( private )
 and access those through public methods from other class.
 
 If anything in a class declared private, that can't be accessed outside the
 class. More or less it's like reducing the dependency of our encapsulated class.
 Advantage of Encapsulation  is, if you need to change the codes, as because of
 less dependency you don't have to work much for modification.
 
 */

public class myEncapsulation {

 public static void main(String[] args) {
  
  // Now we will create object of our EncapsulationMethods class.
  EncapsulationMethods bankDetails = new EncapsulationMethods();
  
  /*
   I'm going to access the encapsulated class through creating object
   and the fields which are kept private in that class like 
   bankBalance,accountNo.
   */
  bankDetails.AccountHolderByName("Mosharrof Rubel");
  bankDetails.FindAccountByName("Jakir Hossain");
  bankDetails.BalanceCheckByPin(1234);
 /*
 The printed output are:

 Name of the account holder is: Mosharrof Rubel
 Jakir Hossain is the account holder & Encapsulated account no is: 420
 Pin is: 1234 and Encapsulated balance is: 119273.78 BDT


 
  */
  
 }

}




2. EncapsulationMethods.java Class


public class EncapsulationMethods {

 // declaring some private variables
 private String person;
 private double bankBalance;
 private int accountNo;
 
 // declaring some public methods
 
 public void AccountHolderByName(String name){
  person = name;
  System.out.println("Name of the account holder is: "+person);
 }

 public void FindAccountByName(String name){
  accountNo = 420;
  System.out.println(name+" is the account holder & Encapsulated account no is: "+accountNo);
 }
 
 public void BalanceCheckByPin(int pin){
  bankBalance = 119273.78;
  System.out.println("Pin is: "+pin+ " and Encapsulated balance is: "+ bankBalance+" BDT");
 }
 
  
 
}



Read More: What are actually taught in computer science and Engineering ( CSE ) ?

12 comments:

  1. This blog is very well done with very well impressive information, i love those information it is really helpful for me to know about many more things.

    Digital Marketing Company in Chennai

    ReplyDelete
  2. Thank you for taking the time to provide us with your valuable information. We strive to provide our candidates with excellent care and we take your comments to heart.As always, we appreciate your confidence and trust in us
    SEO Company in India
    Digital Marketing Company in India

    ReplyDelete
  3. I will bookmark your blog and take the feeds additionally? I’m satisfied to find so many helpful information here within the put up, we want work out extra strategies in this regard, thanks for sharing..
    Germany Education Consultants in Chennai

    ReplyDelete
  4. Truly a very good article on how to handle the future technology. After reading your post,thanks for taking the time to discuss this, I feel happy about and I love learning more about this topic
    Manpower Consultancy in Bangalore
    Hr Consultancy in bangalore
    Recruitment Consultancy in Bangalore

    ReplyDelete
  5. Excellent goods from you, man. I’ve understand your stuff previous to and you’re just too excellent. I actually like what you’ve acquired here, certainly like what you are stating and the way in which you say it. You make it enjoyable and you still take care of to keep it sensible. I can not wait to read far more from you. This is actually a tremendous site..
    PHP Training in Chennai
    Salesforce Training in Chennai
    SAS Training in Chennai

    ReplyDelete
  6. Its fantatic explaintion lot of information gather it...nice article....
    seo company in Chennai

    ReplyDelete
  7. SEO Training in Chennai provides fast track classes and our institute faculty was very excellent.
    We are No 1 SEO Training institute in Chennai.



    SEO Training in Chennai

    ReplyDelete