This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes What is difference between Abstraction and Encapsulation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What is difference between Abstraction and Encapsulation" Watch "What is difference between Abstraction and Encapsulation" New topic
Author

What is difference between Abstraction and Encapsulation

satish chander
Greenhorn

Joined: Dec 21, 2009
Posts: 12
what is the main difference between these two .
John Storta Jr.
Greenhorn

Joined: Jul 26, 2009
Posts: 29
Encapsulation has to do with hiding the details of an object from the public. In order words, hiding the inner workings. A radio is filled with circuits and wires and resistors. The user of the radio does not see any of that. To the user, they only see the interface (The buttons and dials, and knobs). What happens inside the radio is encapsulated. Hidden from the user.

Abstraction has to do with generalizing an object to maximize code reuse. A common example I have seen uses balls. Let's say you have an object called 'Basketball'. It has a series of features relating to the size and shape of the ball and what happens when you bounce it. But now you want to create an object called 'RugbyBall'. It has a different size and shape and does something different when you bounce it. You have a couple choices. You can create a new class for 'RugbyBall', but then you would be duplicating a lot of code in each of the classes. You could inherit from 'Basketball' to avoid rewriting code, but then you are indicating that a Rugby Ball is related to a Basketball, which it is not. (RugbyBall is-not-a Basketball)

The better thing to do would be to create an abstract class called 'Ball, then make a subclass called 'Basketball' and another subclass called 'RugbyBall'. The abstract 'Ball' class contains all of the information that is common to all balls. Within the Basketball and RugbyBall classes, you simply make modifications for those specific types of balls. You avoid duplicating code and each case passes the 'IS-A' test. (Basketball is-a Ball, RugbyBall is-a Ball)

Hope that helps.
Muhammad Khojaye
Ranch Hand

Joined: Apr 12, 2009
Posts: 449

satish chander wrote:what is the main difference between these two .

Check out the previous Posts


http://muhammadkhojaye.blogspot.com/
Vish Shukla
Ranch Hand

Joined: Oct 12, 2008
Posts: 111
Encapsulation has to do with hiding data, give it a name(private data members in a class - Car) and binding behavior methods(start, stop, accelerate) with it. These behavior methods will mutate or provide access to those data variables.

Abstraction provides perspective of the client in abstract terms. As a concept or an idea. This generalization allows clients to ONLY focus on What they are concerned with. Car is concrete entity where as Drivable, Trackable(which has position and can be tracked down) can be abstraction for Car for different clients.

You can check some real life examples of Abstraction and Encapsulation here.


Thanks & Regards,
Vishal S Shukla (SCJP 93%, SCWCD 94%, SCBCD 100%)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is difference between Abstraction and Encapsulation
 
Similar Threads
JRE vrs. JVM
Differences
Difference between gems and plugins
Quick Question HttpSessionBinding vs HttpSessionAttribute Listeners
Run as AWT or SWT Application Eclipse Question