• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

What is difference between Abstraction and Encapsulation

 
satish chander
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the main difference between these two .
 
John Storta Jr.
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 449
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
satish chander wrote:what is the main difference between these two .

Check out the previous Posts
 
Vish Shukla
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic