aspose file tools
The moose likes Beginning Java and the fly likes What is difference between Abstraction and Encapsulation Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply 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: 341
satish chander wrote:what is the main difference between these two .

Check out the previous Posts


http://muhammadkhojaye.blogspot.com/
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What is difference between Abstraction and Encapsulation
 
Similar Threads
Quick Question HttpSessionBinding vs HttpSessionAttribute Listeners
Run as AWT or SWT Application Eclipse Question
Differences
JRE vrs. JVM
Difference between gems and plugins