• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Encapsulation and getter&setter methods

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.,
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That wasn't the error I saw, no.
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thank you Campbell Ritchie for your suggestion.,
keep me correct if i am wrong
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome I presume you know why it is a mistake?
 
See ya later boys, I think I'm in love. Oh wait, she's just a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic