Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The meaning of abstraction and implementation in computer science

  1. Jul 16, 2012 #1
    Hello, I was wondering if someone could give me a "for dummies" definition of abstraction and implementation in computer science? The online definitions are very technical and I can't quite wrap my mind around them.
  2. jcsd
  3. Jul 16, 2012 #2


    Staff: Mentor

    How about you provide us with the definitions you've found, and then we can do our best to translate them into more basic terms.
  4. Jul 22, 2012 #3
    The question is ill-defined so my answer will be very generic.

    In its simplest form abstraction is encapsulating the parts of a system into separate little black boxes. This allows people to use them without requiring knowledge of how they work, or their internals, and prevents one part of the program from adversely affecting another.

    This is done in a number of ways.

    Say you want to write a program that reads in data from a file, and applies a function to said data, then prints it.

    You do could do it like this (code is Python unless otherwise specified):

    Code (Text):

    if = open ("data.txt", "r")  # open input file
    of = open ("data2.txt", "w") # open output file
    for line in f.readlines():   # for each line ...
        line.strip ()            # stupid trailing LF
        line.reverse ()          # reverse it
        of.write (line + "\n")   # save it in output
    But that would suck. To use abstraction.

    Code (Text):
    data = read_in (filename)
    data = process (data)
    write_out (filename, data)
    Much easier with magic black boxes!

    How are they defined? Probably like this:

    Code (Text):
    def read_in (filename):
        f = open (filename, "r")
        return f.readlines ()

    def process (d):
        for i in d:
            i.reverse ()
        return i

    def write_out (filename, d):
        f = open (filename, "w")
        for i in d:
            f.write (d + "\n")
    So long as the API (interface) between the functions is defined, each developer can work away in private on his/her function without stepping on the others' toes (if nothing goes wrong). The developer of process () doesn't need to know or care how the data is read in. All he needs to know is that he receives a list of data in a particular format, he performs the business logic on it, then he returns the modified version in a particular format. If something changes like the method of reading the data, it's similarly easy to redo becuase of the separation of functions.

    When you include a module e.g.
    Code (Text):
    import math
    You are again using abstraction.

    Abstraction is also used to represent data.

    Code (Text):

    # not code, had to use code tags for formatting!
    - engine
      - size, horsepower
    - controls
      - steer, start, stop etc
    - payload
      - number of passengers, max weight possible
    We can rapidly swap engines from another car, etc

    Code (Text):

    # "copy" engine and all its contained properties (size, HP, status etc)
    red_car.engine = blue_car.engine
    I put "copy" in quotes because Python doesn't [STRIKE]always[/STRIKE] copy here, but the result will be the same data in both car engines.

    We can also use the concept of inheritance to derive a more specalised car from the base car

    - derived from car
    - exactly the same but adds new features: roof up, roof down, get roof state

    Then you no longer need separate code to process each car

    Code (Text):
    for this_car in list_of_cars:
        this_car.start_up () # we start ANY car like this
            this_car.roof_up ()
            print ("This car can't do that")
    So you don't need to write separate code for each type of car.
    Last edited: Jul 22, 2012
  5. Jul 22, 2012 #4
    I'd like to get into design patterns (usin abstraction for program structure) but that's kind of long. In any case the first example (text processor) is a very crude exmaple of this.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook