aspose file tools*
The moose likes OO, Patterns, UML and Refactoring and the fly likes Abstraction and Encapsulation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "Abstraction and Encapsulation" Watch "Abstraction and Encapsulation" New topic
Author

Abstraction and Encapsulation

Amarnath Mukherjee
Greenhorn

Joined: Jun 08, 2007
Posts: 6
Is there any relationship between abstraction and encapsulation ? Also like to know what is the true meaning of abstraction .
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
Hi, welcome to the ranch!

We like to help out, but not do the whole job for you. You'll have more fun if you dig up some answers yourself, maybe even get really confused before you figure it all out.

Have you tried Google or some programming / computer science resource sites for definitions? Tell us how you think these are different or related.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
This oldie (1971) but goldie might be useful:
D.L. Parnas: On the Criteria To Be Used in Decomposing Systems into Modules
While it doesn't use the words it expresses the ideas.

Then of course there is commonality-variability analysis.
Amarnath Mukherjee
Greenhorn

Joined: Jun 08, 2007
Posts: 6
Thanks Stan and Peer for your reply .

By definition ( I got it from java forum ) ,

Abstraction :--- Abstraction is removing some distinctions between objects, so as to show their commonalities .

Encapsulation :--- Encapsulation is hiding the details of the implementation of an object so that there are no external dependencies on the particular implementation.

From the above definition , I think a Superclass is an Abstraction of a Subclass . If this is so then why java uses a keyword " abstract " ... is it to indicate that the class actually represents "Abstraction ", an OO concept ..

A class is an example of Encapsulation .

So can I say that to implement Abstraction we need help of Encapsulation ?

Please verify and clarify ..
Ingudam Manoranjan
Ranch Hand

Joined: Jul 31, 2006
Posts: 48
I probably would like to take the meaning as below:

Abstraction: Just like a Abstract of a document. Gives you the gist. Similarly in Java / OOP, an abstract can probably define a type. For all i care, an java interface is also an abstraction.

Encapsulation: Just like the english definition, hide something through some accessibility rules. In Java achieved by the access modifiers applied to both the data and the behavior.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I like the definitions you found just fine.

One variation I like on abstraction is to "ignore" certain details so you can focus on commonalities or key points. Maybe the difference has to do with your role in things. The author of a design might remove details so they don't distract the reader; the reader might ignore details to get the big picture.

As Peer hinted, encapsulation is a direct relative of (just a new word for?) information hiding - one of the oldest concepts in the business. The less you reveal about how something works the more abstract you make it.

Java's abstract keyword makes it clear that a class or method has completely left out some details and some subclass will be required to fill them in. Interfaces are just really abstract.

Not a definition, but here's a nice quote:

The purpose of abstraction is not to be vague, but to create a new semantic level in which one can be absolutely precise. Edsger W. Dijkstra, "The Humble Programmer", October 1972
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
Robert C. Martin: "Abstraction: The amplification of the essential and the elimination of the irrelevant."

Also note that Alan Shalloway points out the different aspects of encapsulation:
  • Encapsulation of data by hiding data members
  • Encapsulation of methods
  • Encapsulation of subclasses behind a common interface or abstract class
  • Encapsulation of objects through containment and delegation

  • Amarnath Mukherjee
    Greenhorn

    Joined: Jun 08, 2007
    Posts: 6
    Thanks all .... what I understand is that Simplification is another name of Abstraction .

    But , Does Encapsulation always mean something hiding ?.... and hiding from whom ?

    For an example if we declare Class as public , is it hiding something ( but Class is an implementation of Encapsulation) ? if yes then from whom ?

    Please clarify ...
    Ingudam Manoranjan
    Ranch Hand

    Joined: Jul 31, 2006
    Posts: 48
    But , Does Encapsulation always mean something hiding ?.... and hiding from whom ?


    Encapsulation is about hiding details that should not concern the client code. Your class should only have a few public interfaces that should be exposed. And only through those interfaces are the access required. So basically you have control over your class.

    Java's Encapsulation and Inheritance are at logger heads in that respect. Inheritance gives access to sub classes certain privileges. But inheritance is very much required.
    steve souza
    Ranch Hand

    Joined: Jun 26, 2002
    Posts: 861
    Defining abstraction can be difficult because definitions are easiest when something is concrete and tangible. Conceptual definitions are often harder. For example it would be difficult to get one definition of 'love', 'emotion', or 'rock music' that helped someone that had never heard of them understand.

    To understand 'abstraction', you need to read multiple definitions and examples until the concepts sink in. By then you will know abstraction when you see it, and have a hard time defining it so someone else understands .

    A couple real world examples will help. The concept of abstraction can be abstracted beyond software ( ) and is fundamental to the way humans think.

    Math is one of our greatest abstractions. When you add numbers it doesn't matter what the numbers represent (apples, money, rocks, ideas), when you add them the math is the same. The alternative would be to have different ways to add every concrete thing (apple math, rock math etc). Abstraction allows us to treat these things as if they were the same as far as addition is concerned. Very powerful.

    Another example of abstraction is a car. At some level all cars share many qualities. For example they have tires, a steering wheel, windows, seats, and many other things. This is the abstraction of a car and it allows us at some level to treat the whole class of cars the same, ignoring spurious details like the fact that it is a honda, or chevy, an accord, or a camry, black or white, 1955, or 2007.

    The difficulty is that what is 'spurious', depends on what you are trying to do. Even many of the details I mention above such as color could be part of an abstraction if it is useful to our problem.

    In java the Object class is an abstraction of...well an Object. It contains methods that are common across all objects. Not to say abstraction is only about the methods. For example constructors (i.e. the ability to create) are also part of a Java objects abstraction.

    Also, just because you use an interface, or abstract class doesn't mean you are coding in the abstract. Things like method names even play a large part in writing abstract code. Method names should speak to 'what the code is doing', not 'how the code is doing it'. For example say you have code that takes a String and returns a checksum. The method names - stevesCheckSumForStringsLessThan5Characters(...). crc32CheckSum(...), checkSum(...) are each one more abstract than the other. checkSum(...) reveals the least about 'how' the checksum is implemented as well as its restrictions and so is the most abstract and flexible.

    Also, whenever you see a noun in a method name, that is a 'smell' that indicates your code is not abstract. Examples of this problem are carDrive(), boatDrive(), bikeDrive(). Learning these 'tricks' helps you write more abstract code.

    Because encapsulation hides 'how' your code works, it is a servant/assistant to abstraction. i.e. The more you hide 'how' you do something the more abstract your code becomes. Good abstraction shows very little of how your code is implemented and so is tightly coupled with encapsulation in a good way .

    Separating what is the abstraction in a java class from what is not can be quite difficult, and takes lots of practice. Unfortunately many things we create in software have no real world model (such as the car example did), and so we often struggle to figure out what the abstractions are. When you find them your program often becomes more flexible, and general.

    Many times I thought I had found a perfect abstraction only to find it broke down when i wanted to enhance my program in some way. That is also a key. Concrete use cases will drive your abstractions.

    [ June 14, 2007: Message edited by: steve souza ]
    [ June 15, 2007: Message edited by: steve souza ]

    http://www.jamonapi.com/ - a fast, free open source performance tuning api.
    JavaRanch Performance FAQ
    Garrett Rowe
    Ranch Hand

    Joined: Jan 17, 2006
    Posts: 1296
    Steve, I must say that was an excellent post on a difficult to explain topic.


    Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
    steve souza
    Ranch Hand

    Joined: Jun 26, 2002
    Posts: 861
    Thanks, Garrett. I edited the post and expanded itto include some information about encapsulation too.
    [ June 14, 2007: Message edited by: steve souza ]
    Amarnath Mukherjee
    Greenhorn

    Joined: Jun 08, 2007
    Posts: 6
    Thanks Steve ... nice examples .
    Amarnath Mukherjee
    Greenhorn

    Joined: Jun 08, 2007
    Posts: 6
    By the way , can you please explain data abstraction .... is it similar to data hiding , by making it private or protected .
    steve souza
    Ranch Hand

    Joined: Jun 26, 2002
    Posts: 861
    Abstraction of any kind isn't just about java syntax. i.e. you can use private, protected, interfaces and abstract classes and still not be abstract.
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    Googling for definitions of "encapsulation" will find many variations from well respected people. For the most part I've come to equate it with "information hiding" and gone on.

    I worked with someone who thought information hiding was about hiding things from her, like I wouldn't tell her how my code worked. It was funny later, but took a lot of talking at the time.

    Imagine you bought a machine with a clear case so you could see how it works. After some observation you realize the wazzit would trigger a bit faster if you tipped it to the left. And the framis would spin a little faster if you warmed the whole thing up. And the squidge is adjustable with this little wheel on the back. One day the manufacturer shows up and offers to do a free upgrade. He pulls out some parts and puts in some new ones. And now it goes slower and slower until it quits. Why? Because the new guts work better when cooled off and sitting exactly level and the squidge at the factory default setting.

    Now imagine you bought a machine with a solid black case. You can't see how it works. There are no wheels or levers to adjust it. You just turn it on and it goes. The manufacturer installs an upgrade and it just goes better.

    Now imagine you prepare a class for somebody else to use. Do you want to expose the innards? Provide methods that give big hints about the workings? Or do you want to hide all that?

    Here's the machine by the way. It still goes zip when it moves and bop when it stops, whirrrrr when it stands still. I never knew just what it was and I guess I never will.
    Amarnath Mukherjee
    Greenhorn

    Joined: Jun 08, 2007
    Posts: 6
    Good funny examples Stan .... nice explanation . I think I got a good solid overview of Abstraction n Encapsulation ... now I need a detailed study .

    Thank you all .
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Abstraction and Encapsulation