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