Results 1 to 10 of 10
Like Tree3Likes
  • 3 Post By Junky

Thread: Confused about encapsulation

  1. #1
    007
    007 is offline Member
    Join Date
    Jun 2011
    Location
    New Zealand
    Posts
    8
    Rep Power
    0

    Question Confused about encapsulation

    I'm reading the java introduction book and i've come across on the first chapter about encapsulation. what is encapsulation? and why is a class a basic unit of encapsulation? and how are objects instances of class? what does "instance of" mean?

    just give a simple explanation with examples please. thanks. :)

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You can encapsulate a set of properties inside of a class, for example, a person would have a name, age, height, weight, etc and you would encapsulate those in a Person class, which makes the set of attributes easier to manage. You also encapsulate the information in a class in a way that limits peoples access to the variables. You dont want anyone to be able to change a persons name, right? But you do want to provide a way to do this. This is done with access control, mutators and accessors.

    A class is like a blue print for a class, not an instance of a class, let's say you create a recipe class, the recipe specifies how to make some meal, but eating the recipe would be very strange, unless you like paper. You would create an instance of the meal and eat it. (I stole this example of recipes from someone here)

    Please ask more questions if you are still unclear.

  3. #3
    007
    007 is offline Member
    Join Date
    Jun 2011
    Location
    New Zealand
    Posts
    8
    Rep Power
    0

    Default

    So what you are trying to say is that by means of encapsulate, we enclose certain set of attributes to the Person class and put in restrictions so that other people would not change those variables which are the name, age, etc.

    Quote Originally Posted by sunde887 View Post
    A class is like a blue print for a class, not an instance of a class.
    I'm not sure about what you're trying to say here. Can you emphasize on that.. But I do understand that a recipe is like a guide on how to make a meal. So a meal is an instance of following a recipe. Or in animal terms a dog is an instance of a mammal? or am I wrong.

    Thanks for the reply. :)

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Quote Originally Posted by 007 View Post
    Or in animal terms a dog is an instance of a mammal? or am I wrong.
    No this is more along the lines of Inheritance, which you haven't gotten to yet.

    Sunde says a class is like a recipe, but you have to make an instance of it otherwise you're eating paper. I like this example, and look at it like this. You're hungry and you have a recipe. Now the recipe is instructing you how to make the meal that will satisfy your hungry. You would then create the meal with
    Java Code:
    Object myMeal = new Object();
    Encapsulation acts as a filter to what you can and can't do to that object. Say I have a person, and his age is 14 but tomorrow is his birthday so I want to make his age 15. I encapsulated the age variable so now I must change it like
    Java Code:
    Person myPerson = new Person();
    myPerson.setAge(15);
    Does that make sense?
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  5. #5
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default

    You are close. Do you know what a blueprint is? It is an achitectural drawing that builders use as to how to erect a building. If you used that blueprint numerous times you can generate the same building over and over. Class is the blueprint, Object is the building. Now in Java we can use that blueprint and slightly modify it with different data. So in the case of a Person object we can create many Person objects with different names.

    Or in animal terms a dog is an instance of a mammal
    Kinda. What would be a better explaination is that the Dog class is a subclass of Mammal and Mammal is a subclass of Animal. Then Snoopy is an instance of Dog.
    Dark, sunde887 and 007 like this.

  6. #6
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    You are close. Do you know what a blueprint is? It is an achitectural drawing that builders use as to how to erect a building. If you used that blueprint numerous times you can generate the same building over and over. Class is the blueprint, Object is the building. Now in Java we can use that blueprint and slightly modify it with different data. So in the case of a Person object we can create many Person objects with different names.


    Kinda. What would be a better explaination is that the Dog class is a subclass of Mammal and Mammal is a subclass of Animal. Then Snoopy is an instance of Dog.
    That seems a bit simpler than mine.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  7. #7
    007
    007 is offline Member
    Join Date
    Jun 2011
    Location
    New Zealand
    Posts
    8
    Rep Power
    0

    Default

    A class is the blueprint and an object is a building. If I want to create 20 buildings, then I need to use the blueprint to generate 20 buildings.

    In this case I need to create 20 of this statement:

    Object myBuilding = new Object();

    So does that mean an Animal is a subclass of Object? If Snoopy is an instance of a Dog then Garfield is not an instance of a Dog unless it is a Dog named Garfield.

    Quote Originally Posted by Dark View Post

    Encapsulation acts as a filter to what you can and can't do to that object. Say I have a person, and his age is 14 but tomorrow is his birthday so I want to make his age 15. I encapsulated the age variable so now I must change it like
    Java Code:
    Person myPerson = new Person();
    myPerson.setAge(15);
    In the encapsulation example, the purpose of it is to hide the age so that age so it wont be altered by third parties or something?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Quote Originally Posted by 007 View Post
    In the encapsulation example, the purpose of it is to hide the age so that age so it wont be altered by third parties or something?
    Pretty much. In an ideal OO design you are building classes that do things. How they do those things should not be important to code that uses objects of that class.

    So a Car class has a speed attribute. I can get the speed attribute by calling the getSpeed() method, but I have no setSpeed() method. This is because the speed of my Car is determined by using the accelerate() method and the brake() method. Cars speed is encapsulated. The logic behind the accelerate() and brake methods() is unimportant to me as a user of a car, so long as they do what they say they do.

  9. #9
    007
    007 is offline Member
    Join Date
    Jun 2011
    Location
    New Zealand
    Posts
    8
    Rep Power
    0

    Default

    What you're trying to say is that, encapsulation, is to make the programmer have control over the method implementation which is the logic of the getSpeed(), accelerate() and brake() methods. So the users only have control over the user interface that is they only need to know how the getSpeed(), accelerate() and brake() methods work. So we keep the inner details of those methods hidden to the users because we don't want them messing with the code we've written.

    Also, I keep confusing what 'encapsulates' and 'instance of' means in computing terms. Can you define it and give examples please. Thanks.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default

    Not just the inner details of the methods, but also the inner details of the data stored in the object.
    Speed may be a calculated attribute. So getSpeed() may be doing a calculation, rather than simply providing a stored attribute. Unlikely in this case, but possible.

    Which is where the idea of coding against an interface rather than a concrete class comes from.

Similar Threads

  1. encapsulation in java
    By guest_user in forum New To Java
    Replies: 17
    Last Post: 05-24-2011, 04:41 AM
  2. A question about encapsulation
    By 3awash in forum New To Java
    Replies: 4
    Last Post: 04-20-2011, 12:16 PM
  3. Are abstraction and encapsulation concepts not in C?
    By hitesh_public in forum New To Java
    Replies: 1
    Last Post: 08-20-2010, 02:23 PM
  4. encapsulation & constructors
    By buckskinner1776 in forum New To Java
    Replies: 4
    Last Post: 05-10-2010, 06:35 PM
  5. java-Encapsulation
    By lenah in forum Advanced Java
    Replies: 5
    Last Post: 01-05-2009, 09:05 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •