Home / java / Java Encapsulation

Java Encapsulation

Hi, guys in this article we are going see the “Java Encapsulation” Topic, which is pretty simple to understand and to implement.

We Call Functions as Methods in Java.

class Calcultor{
public float addition(float a ,float b){
return a+b;

class TestCalcultor{
public static void main(String args[]){
System.out.println("Testing Calcultor");
/*addition of 2 numbers*/
int a = 10;
int b = 20;
int result= a+b;
System.out.println("Result is : "+result);

/*addition of 2 number by using same Class function */

TestCalcultor tc = new TestCalcultor();
result = (int)tc.addition(2,2);
System.out.println("Result is : "+result);

/*adition of 2 number by using an Another Class function */
Calcultor cal = new Calcultor();
result = (int)cal.addition(2,2);

System.out.println("Result is : "+result);


public int addition(int a ,int b){
return a + b;


Important Notes:

If you take a look at the documentation, it says, that expectsMath.pow() two doublesand returns a double. When you pass ints to this function, it means no harm, because casting (converting) an int to double means no loss. But when you assign the value to an int, it means, it can lose precision.

Simply do this:

int x = (int)Math.pow (2,4);


double x = Math.pow (2,4);

About Mohammad Fareed

Software Engineer @ Tekzenit.

Check Also

Object Creation in Java

1)How many ways are there to create an object? 1. How many ways to create …


  1. Howdy would you mind stating which blog platform you’re working with?
    I’m going to start my own blog in the near future but I’m having a hard time selecting between BlogEngine/Wordpress/B2evolution and Drupal.

    The reason I ask is because your design and style seems different then most blogs and I’m
    looking for something unique. P.S Sorry for being off-topic
    but I had to ask!

    • No Problem if you don’t have time then you can go with WordPress, which is Pretty Simple to Setup and use, we have a wide range of Themes and Plugin for our Application.
      If you need my assistance in that drop me an email to fareed543 [at] gmail.com

  2. I am not sure where you’re getting your info, but good
    topic. I needs to spend some time learning more or understanding more.
    Thanks for excellent info I was looking for this info for my mission.

  3. This is my first time pay a quick visit at here and i am
    genuinely pleassant to read everthing at alone place.

  4. Aw, this wаs a reɑlly good ρost. Taking a few minutes and actual
    effort to create a vеry good article… bᥙt what can I
    say… I put things off a whߋle lot and nwver seem too get anything ɗone.

  5. I came across your blog and loved it. I shared the post on my Facebook
    and Twitter pages. My followers enjoyed your post. I went a head and bookmarked your blog and will visit
    again. Again, nice post!!!
    Nice blog. I shared your post with my followers on my facebook and twitter followers.
    Your post was spot on and I enjoyed it.
    Thanks for the posts. I love the article to where, I shared it with my followers on Facebook and twitter.

Leave a Reply

Your email address will not be published. Required fields are marked *