Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Encapsulation and getter&setter methods

 
r suraaj
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
r suraaj
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



// 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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it is not a good example. I shall leave you to work out for yourself which bit is missing.
 
r suraaj
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator




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
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That wasn't the error I saw, no.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you Campbell Ritchie for your suggestion.,
keep me correct if i am wrong
 
Campbell Ritchie
Sheriff
Pie
Posts: 49733
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome I presume you know why it is a mistake?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic