aspose file tools*
The moose likes Beginning Java and the fly likes Encapsulation and getter&setter methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Encapsulation and getter&setter methods" Watch "Encapsulation and getter&setter methods" New topic
Author

Encapsulation and getter&setter methods

r suraaj
Greenhorn

Joined: Oct 26, 2008
Posts: 16
hi.,

i have studied that " getter and setter methods in real world programming are the best examples for encapsulation ".,

what it actually means??

"as private member variables can't be accessed outside of our class, so with the getter and setter methods we can assign them values and access them " i am thinking in this way., am i correct?

can anyone provide me example to explain clearly explaining Encapsulation (which is an important feature of obj oriented prog) with and example and with setter and getter methods supporting that feature.,

waiting for your reply...

thanks in advance to you.,
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Encapsulation is this:


This is just a simple example. In a more complex program you could validate the name parameter in setName if it matches to certain criteria.
Or you could hide certain properties of an object and make others accessible.


"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
r suraaj
Greenhorn

Joined: Oct 26, 2008
Posts: 16
public class Speed
{
int speed;

public void setSpeed(int s)
{
if (s<= 0)
{
System.out.println("sorry..! you can't assign a null or negative value as speed ");
}

else
{
speed = s;
}
}

}
public class SpeedTest
{
public static void main(String [] args)
{
Speed spd = new Speed();

spd.setSpeed(25);

}

}

// can it be the valid code for explaining as an example for encapsulation in the java technical interviews?
// please reply me and suggest me .,

thanks in advance
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Use Code Tags!

Yes that is a good example although I would throw an IllegalArgumentException
instead of writing a line to the console.
r suraaj
Greenhorn

Joined: Oct 26, 2008
Posts: 16



// can it be the valid code for explaining as an example for encapsulation in the java technical interviews?
// please reply me and suggest me .,

thanks in advance
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
No, it is not a good example. I shall leave you to work out for yourself which bit is missing.
r suraaj
Greenhorn

Joined: Oct 26, 2008
Posts: 16




previously i have forgot to write the code for getter method.,
still it is not a better example to answer?
if yes., please provide me the better code to satisfy the technical interviewer
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
That wasn't the error I saw, no.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
I previously wrote:That wasn't the error I saw, no.
It was failing to mark the int field private that I noticed.
r suraaj
Greenhorn

Joined: Oct 26, 2008
Posts: 16
thank you Campbell Ritchie for your suggestion.,
keep me correct if i am wrong
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
You're welcome I presume you know why it is a mistake?
 
 
subject: Encapsulation and getter&setter methods