locked
abstration vs encapsulation RRS feed

  • Question

  • User-240513752 posted

    How to achieve abstraction?some times i read on net that abstraction can achieve by abstract class & interface,it is rite or wrong?

    How to achieve encapsulation?

    I know hiding details differene but i want to know the difference of implementation & how they are related to each other in one example?

    Friday, September 18, 2015 11:04 AM

Answers

  • User37182867 posted

    You can think of encapsulation as taking code that is repeated throughout an application and turning it one method or class.

    While abstraction is taking a class or method that might be similar in name but do different things depending on the object. IE parent class vs its child class implementation. Also Abstraction can include Interfaces. Both Parent or Base Class structure and an Interface would define basic behavior and the child class or the class that implements the interface would then either modify the behavior in the case of a Base class or in the case of Interface implementation actually define what it does for that instance of the class. (which could be different depending on the class)

    Complex ideas but very different in nature. Saying it in another way, Abstraction hides the implementation of a behavior while Encapsulation brings repeated behavior to one location.

    Make sense?

    Abstraction
    Example would be to have a Automobile class. Then have a tractor class and a car class. The automobile provides a start method, but the implementation between how a tractor and a car could be very different.

    Encapsulation
    Example would be writing spaghetti code with no method calls. There might be code in that object that is exactly repeated. So we move that code to a method and call that method each time it was used in the spaghetti code previously.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 18, 2015 11:25 AM
  • User-986267747 posted

    Hi Khan1

    In my experience, Encapsulation is  hiding data using getters and setters etc. Abstraction is hiding implementation using abstract classes and interfaces etc.

    I know hiding details differene but i want to know the difference of implementation & how they are related to each other in one example?

    For it, you could refer to the following links.

    abstraction:http://www.dotnetfunda.com/articles/show/632/what-is-an-abstraction

    encapsulation:http://www.dotnetfunda.com/articles/show/511/what-is-encapsulation

    http://www.c-sharpcorner.com/UploadFile/tusharkantagarwal/objectorientedcsharp11162005070743AM/objectorientedcsharp.aspx

    I hope it's helpful to you.

    Best Regards

    Klein zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 22, 2015 4:56 AM

All replies

  • User37182867 posted

    You can think of encapsulation as taking code that is repeated throughout an application and turning it one method or class.

    While abstraction is taking a class or method that might be similar in name but do different things depending on the object. IE parent class vs its child class implementation. Also Abstraction can include Interfaces. Both Parent or Base Class structure and an Interface would define basic behavior and the child class or the class that implements the interface would then either modify the behavior in the case of a Base class or in the case of Interface implementation actually define what it does for that instance of the class. (which could be different depending on the class)

    Complex ideas but very different in nature. Saying it in another way, Abstraction hides the implementation of a behavior while Encapsulation brings repeated behavior to one location.

    Make sense?

    Abstraction
    Example would be to have a Automobile class. Then have a tractor class and a car class. The automobile provides a start method, but the implementation between how a tractor and a car could be very different.

    Encapsulation
    Example would be writing spaghetti code with no method calls. There might be code in that object that is exactly repeated. So we move that code to a method and call that method each time it was used in the spaghetti code previously.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 18, 2015 11:25 AM
  • User-986267747 posted

    Hi Khan1

    In my experience, Encapsulation is  hiding data using getters and setters etc. Abstraction is hiding implementation using abstract classes and interfaces etc.

    I know hiding details differene but i want to know the difference of implementation & how they are related to each other in one example?

    For it, you could refer to the following links.

    abstraction:http://www.dotnetfunda.com/articles/show/632/what-is-an-abstraction

    encapsulation:http://www.dotnetfunda.com/articles/show/511/what-is-encapsulation

    http://www.c-sharpcorner.com/UploadFile/tusharkantagarwal/objectorientedcsharp11162005070743AM/objectorientedcsharp.aspx

    I hope it's helpful to you.

    Best Regards

    Klein zhang

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, September 22, 2015 4:56 AM
  • User-1923420989 posted
    Hi Khan.

    Abstraction is a concept and encapsulation is a implementer of such concept.

    In simple words, Abstraction concept is

    * show ONLY what is necessary to the client code.

    Encapsulation does it ( implements it) by

    ** Hide the implementation details by making them private and establish public methods to access these private methods and let the client code

    to access only the public method ( this is encapsulated).
    Tuesday, September 22, 2015 5:18 AM
  • User37182867 posted

    abrly

    Abstraction is a concept and encapsulation is a implementer of such concept.

    That is not an answer I would except as correct if I were interviewing you. The concepts between Abstraction and Encapsulation are very different. The only thing they share in common is they are both OOP concepts.

    Here is a good definition between the two while it references Java specifically these are OOP concepts that apply to .Net Languages too.

    "Abstraction and Encapsulation in Java are two important Object oriented programming concept and they are completely different to each other. Only similarity between Abstraction and Encapsulation is that they are OOPS concept, other than that they mean two different things. Abstraction represent taking out the behavior from How exactly its implemented, one example of abstraction in Java is interface while Encapsulation means hiding details of implementation from outside world so that when things change no body gets affected. One example of Encapsulation in Java is private methods; clients  don't care about it, You can change, amend or even remove that method  if that method is not encapsulated and it were public all your clients would have been affected. Apart from this main difference in behavior here are couple of more differences between Abstraction and Encapsulation in Java."

    http://java67.blogspot.com/2012/08/difference-between-abstraction-and-encapsulation-java-oops.html

    I choose to directly quote him because his definition and example of the two are better than what I previously wrote.

    Tuesday, September 22, 2015 7:45 AM
  • User264732274 posted

    Nice post

    Wednesday, September 23, 2015 2:38 PM
  • User-1461986491 posted

    abstration

    In C#, you can have a single base class and multiple derived classes. If you do not want that any class can create an object of the base class, you can make the base class as abstract.

    The abstract keyword in a class indicates that the class cannot be instantiated. So You can only create instance of its derived class (only if it is not marked as abstract).

    encapsulation

    Encapsulation is implemented by using access specifiers like public, private, protected, internal etc.

    http://csharpcode.org/abstract-classes-and-methods/

    http://csharpcode.org/scopes/

    Thursday, October 1, 2015 3:03 AM
  • User-434868552 posted

    @Khan_1

    in framing encapsulation and abstraction in terms of computer science i would not speak about their differences.

    Their differences are not relevant.  Let me explain.

    Both encapsulation and abstraction from the perspective of programmers who use the object oriented paradigm are siblings.

    in computer science, as in other disciplines, words are often borrowed from the general spoken language  and take on meanings peculiar to computer science.

    For example, one of the purposes of art is an evolutionary way of representing ideas; when art is less like a photograph, it is a called abstract art.  Examples:

    scream: Edvard Munch, "The Scream"         "The Persistence of Memory":  Salvador Dali, 1931

    The art on the left could be someone screaming, or perhaps someone with a migraine headache; the surrealist art on the right is far more abstract.

    in programming, we use abstract more in a sense of "to pull out and put in another place".  Simple example:

    Double areaOfAnAmericanFootballFieldSquareYards = 120d * 53.33d; // 6399.6 square yards
    Double areaOfAnAmericanFootballFieldSquareMetres = 110d * 48.76d; // 5363.6 square metres
    Double areaBaskettballCourtSquareFeet = 84 * 50; // 4200 square feet

    When can pull out the calculation of area and replace it in c# with a method:

    public static Double Area(Double length, Double width)
    {
        return length * width;
    }
    void Main()
    {
        Double areaOfAnAmericanFootballFieldSquareYards = Area(120d, 53.33d); // 6399.6 square yards
        Double areaOfAnAmericanFootballFieldSquareMetres = Area(110d, 48.76d); // 5363.6 square metres
        Double areaBaskettballCourtSquareFeet = Area(84, 50); // 4200 square feet
    }

    Encapsulation

    in English, one of the uses of the prefix en-  is to form verbs  "to put into _______"; endanger.

    en-capsule  → to put into a capsule

    Example, let's encapulate our Area method:

    public class Measurement
    {
        public static Double Area(Double length, Double width)
       {
            return length * width;
       }
    }

    to use our enscapsulated Area method, we must change our calling code slightly:

    void Main()
    {
        Double areaOfAnAmericanFootballFieldSquareYards = Measurement.Area(120d, 53.33d); // 6399.6 square yards
        Double areaOfAnAmericanFootballFieldSquareMetres = Measurement.Area(110d, 48.76d); // 5363.6 square metres
        Double areaBaskettballCourtSquareFeet = Measurement.Area(84, 50); // 4200 square feet
    }
    Thursday, October 1, 2015 9:56 AM
  • User-1923420989 posted

    deadtroll

    Abstraction represent taking out the behavior from How exactly its implemented

    while Encapsulation means hiding details of implementation from outside world

    Abstraction and encapsulation are not just specific to any objects for example abstraction is for interface and encapsulation for others ...

    Abstraction is generic term and it is a theory to achieve OOP principle " show only required details to client " .

    Abstraction can be achieved by implementing encapsulation.

     

     

    Sunday, October 4, 2015 3:40 AM
  • User-434868552 posted

    @abrly

    please see my reply http://forums.asp.net/post/5971942.aspx above.

    encapsulation is not required in order to have abstraction.

    https://en.wikipedia.org/wiki/Abstraction_(computer_science)

    excerpt:  "The recommendation that programmers use abstractions whenever suitable in order to avoid duplication (usually of code) is known as the abstraction principle. "

    https://en.wikipedia.org/wiki/Abstraction_principle_(computer_programming) 

    excerpt:  "Since code is usually subject to revisions, following the abstraction principle may entail refactoring of code. The effort of rewriting a piece of code generically needs to be amortized against the estimated future benefits of an abstraction. A rule of thumb governing this was devised by Martin Fowler, and popularized as the rule of three. It states that if a piece of code is copied more than once, i.e. it would end up having three or more copies, then it needs to be abstracted out."

    Sunday, October 4, 2015 3:51 AM