Help on Getters and Setters in Java

Java programming topics
Post Reply
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Help on Getters and Setters in Java

Post by Nipuna » Tue Apr 26, 2011 9:10 am

Hello Friends I am Back After a While :)

I am leaning Java From Head first java Book and with helps of a friend of mine Who is in UCSC.

First I didn't have any knowledge in OOP and Now i am Getting Better with it.

I found a Thing called Getters and Setters in java. I think they are Related to Encapsulation in Java which use to Hide data from the users to prevent unnecessary changes. Like Hiding Variables not to change them into unacceptable values,
Am I right( I mean Encapsulation Thing Have i got it Right? ) ?

But I am not clear with Getters and Setters in java.

But I saw there are two Methods one is a Setter the other is a Getter. And One Method returns a Value and The other one take Arguments via Method's Parameters.

I Used Google but those Examples didn't Suit me Well. I think you Guys will fill me Up Very well about this.


Please Someone Explain them with Examples.

And another thing Can some one give me a Link or a ebook that have Java exercises with Answers? I Googled for that too but no good ones either.


Thanks
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Help on Getters and Setters in Java

Post by Neo » Tue Apr 26, 2011 8:56 pm

I found a Thing called Getters and Setters in java. I think they are Related to Encapsulation in Java which use to Hide data from the users to prevent unnecessary changes. Like Hiding Variables not to change them into unacceptable values,
Am I right( I mean Encapsulation Thing Have i got it Right? ) ?

But I am not clear with Getters and Setters in java.

But I saw there are two Methods one is a Setter the other is a Getter. And One Method returns a Value and The other one take Arguments via Method's Parameters.

I Used Google but those Examples didn't Suit me Well. I think you Guys will fill me Up Very well about this.
Very good question. Your understanding on Getters and Setters is correct.

However in Java, there isn't any special way to define getters and setters like in some other languages. See following VB6 example. Notice the 'get' and 'let' keywords that defines the getter ans setter respectively. In Java, we use general methods to do so.

Code: Select all

  'Global variable inside the class
  private m_Name as string

  public property get Name() as string
    Name = m_Name
  end sub

  public property let Name(sval as string)
    m_name = sval
  end sub

Also, note that every getter and setter in your code represents a failure to encapsulate and creates unnecessary coupling. Encapsulation is used to hide unnecessary information from user. Consider the following Java example.

Code: Select all

class Product
{
    private Number price;
    private Number tax_rate;

    Number function getPrice()
    {
        return this.price;
    }

    Number function setPrice(Number cprice)
    {
        this.price = cprice;
    }

    Number function getTaxRate()
    {
        return this.tax_rate;
    }

    Number function setTaxRate(Number ctax_rate)
    {
        this.tax_rate = ctax_rate;
    }
}
Now we have getters and setters defined for two private variables that we have defined. So in our program, we can do something like this.

Code: Select all

Product bread;
Number price, tax_rate, tax;

bread.setPrice(50.00);
bread.setTaxRate(15);

price = bread.getPrice();
tax_rate = bread.getTaxRate();

tax = price * tax_rate;
Now you can see we had to use both getters and setters for each private variable to calculate tax.

Now consider the following version of the class.

Code: Select all

class Product
{
    private Number price;
    private Number tax_rate;

    void function setPrice(Number cprice, Number ctax_rate)
    {
        this.price = cprice;
        this.tax_rate = ctax_rate;
    }

    Number function getTax()
    {
        return (this.price * this.tax_rate);
    }
}
Notice that both variables are now set from one method.

Here is the program.

Code: Select all

Product bread;
Number tax;

bread.setPrice(50.00, 15.0);

tax = bread.getTax();
Can you see, here we get the tax calculated from the Product class so we don't need getters. So we fullfill the purpose of Encapsulation by hiding access to price and tax private variables from outside the class.

I think you have a better understanding now.
And another thing Can some one give me a Link or a ebook that have Java exercises with Answers? I Googled for that too but no good ones either.
Have you see the following links that has BIT questions and solutions. Have a look.

BIT Examination Papers - Semester 1
BIT Examination Papers - Semester 2
BIT Examination Papers - Semester 3
BIT Examination Papers - Semester 4
BIT Examination Papers - Semester 5
BIT Examination Papers - Semester 6
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: Help on Getters and Setters in Java

Post by Nipuna » Tue Apr 26, 2011 9:50 pm

Thanks Friend.

but I don't know about

Code: Select all

this
Keyword. And I don't search about it until i meet this keyword in the book.

Thanks Anyway friend.

I figured out More than before i Posted this Topic by reading the chapter of the book again and again. Now I have some Understandings.

I will read more and Gain my Knowledge more.

Thanks for the Helps Friend.

But I Want more Questions than BIT ones. Like the ones you gave me for C++. I think you remember those.

And what do you think about my OOP knowledge Now? :) I know i have more to learn.
Now I know what are objects what are classes and how to inherit. And Override a method and Some more things.





Thanks
User avatar
Neo
Site Admin
Site Admin
Posts: 2642
Joined: Wed Jul 15, 2009 2:07 am
Location: Colombo

Re: Help on Getters and Setters in Java

Post by Neo » Wed Apr 27, 2011 12:56 am

And what do you think about my OOP knowledge Now? :) I know i have more to learn.
Now I know what are objects what are classes and how to inherit. And Override a method and Some more things.
It is always happy to see you are getting more and more knowledgeable and I already know you are a very talented young man. If you give your fullest, I'm sure you'll reach your target without any problem.
User avatar
Nipuna
Moderator
Moderator
Posts: 2729
Joined: Mon Jan 04, 2010 8:02 pm
Location: Deraniyagala,SRI LANKA

Re: Help on Getters and Setters in Java

Post by Nipuna » Wed Apr 27, 2011 8:55 am

Thanks Dude.

You Always Give Me Kicks :)
223009-Royalty-Free-RF-Clipart-Illustration-Of-A-3d-Teeny-Person-Kicking-Someone-In-The-Butt.jpg
223009-Royalty-Free-RF-Clipart-Illustration-Of-A-3d-Teeny-Person-Kicking-Someone-In-The-Butt.jpg (35.22 KiB) Viewed 4268 times
Post Reply

Return to “Java Programming”