Over my Head, Part I: Encapsulation

encapsulation

'encapsulation' by Flickr user Swiv

Of course I fully expect that over the course of reading Part two of Beginning Ruby there will be frequent “Huh?” moments.

I just had one in Chapter 6 “Classes, Objects , and Modules.

Encapuslation.

Encapsulation is the ability for an object to have certain methods and attributes available for use publicly (from any section of code), but for others to be visible only within the class itself or by other objects of the same class.

Ok, I think maybe typing that out helped me understand the concept but the code examples that followed did not make much sense to me.

I could stick around and try and get it or I could move on. I’ll move on for now. If it’s that important I’m sure I’ll encounter it again and its meaning will become clearer.

N.B. The Flickr pic above is one of the first Creative Commons images I found doing a search for ‘encapsulation’. It’s of St. Andrew’s Cathedral, which is near where I’m from. Hurrah!

Advertisements

Tags: , ,

5 Responses to “Over my Head, Part I: Encapsulation”

  1. Learning concepts and building dungeons « From Noob to Ninja Programmer Says:

    […] 6 covers, amongst other things, Local/Instance/Global/Class variables, Encapsulation, Polymorphism, Module, Namespace, Mix-in, Enumerable and Comparable. At the end of Chapter 6 is a […]

  2. Peter Cooper Says:

    Encapsulation is so unimportant at the early stages of programming that some languages attempt to skip the feature entirely.. Python for example. I only mentioned it for completeness but it’s not really *that* important until you’re doing high end work, working with a team of developers, and so forth.

    The formal definition of encapsulation is “the process of compartmentalizing the elements of an abstraction that constitute its structure and behavior; encapsulation serves to separate the contractual interface of an abstraction and its implementation.” I think you’ll agree that doesn’t help very much at this stage 😉

    To boil it down to its essence, the point of encapsulation is to be able to “hide” functionality within a class so that its outward exposed interface isn’t radically changed or adjusted, even if the class is internally extended to do a lot more over time. That is, the public methods you make available then use lots of other internal “private” methods that can be changed, renamed, etc, but to an external user of the class, it doesn’t affect their day to day use of your class.

  3. Andrew Rodriguez Says:

    Encapsulation basically means the ability to have *both* methods (what an object can do) and attributes (data) together in one object.

    Now, some methods can be accessed externally and some are *only* available within the class itself – also – as the text points out – by the same type of class.

    So, if you have an object that only has methods – then you don’t have encapsulation.

    Does that help?

  4. Geoff Ford Says:

    By way of an analagy:

    A car has a motor. A car has a certain number of seats. A car has other features such as CD player, A/C etc.

    Now a Holden Commodore (yes I’m an Aussie) encapsulates certain combinations of the above – i.e. 5 seats, CD Player, A/C, a V6.

    Encapsulation is the process of hiding the details – if I know that all cars have a motor, I don’t necessarily need to know that the motor is a V6 or V8 most of the time.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: