• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

What is difference between Abstraction and Encapsulation

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what is the main difference between these two .
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 449
Scala IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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


Check out the previous Posts
 
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
If you were a tree, what sort of tree would you be? This tiny ad is a poop beast.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic