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
Help on Getters and Setters in Java
Re: Help on Getters and Setters in Java
Very good question. Your understanding on Getters and Setters is correct.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.
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;
}
}
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 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);
}
}
Here is the program.
Code: Select all
Product bread;
Number tax;
bread.setPrice(50.00, 15.0);
tax = bread.getTax();
I think you have a better understanding now.
Have you see the following links that has BIT questions and solutions. Have a look.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.
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
Re: Help on Getters and Setters in Java
Thanks Friend.
but I don't know about 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
but I don't know about
Code: Select all
this
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
Re: Help on Getters and Setters in Java
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.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.
Re: Help on Getters and Setters in Java
Thanks Dude.
You Always Give Me Kicks
You Always Give Me Kicks