IS A FUNCTION AN EXAMPLE OF ENCAPSULATION?

Ankita Pandey

Total Post:183

Points:1285
Posted by  Ankita Pandey
OOPS 
 2575  View(s)
Ratings:
Rate this:

By putting functionality into a function, does that alone constitute an example of encapsulation or do you need to use objects to have encapsulation?

I'm trying to understand the concept of encapsulation. What I thought was if I go from something like this:

n = n + 1

which is executed out in the wild as part of a big body of code and then I take that, and put it in a function such as this one, then I have encapsulated that addition logic in a method:

addOne(n)

    n = n + 1

    return n

Or is it more the case that it is only encapsulation if I am hiding the details of addOne from the outside world - like if it is an object method and I use an access modifier of private/protected?

  1. Vijay Shukla

    Post:100

    Points:700
    Re: Is a function an example of encapsulation?

    Hello Ankita Pandey!

    Encapsulation is a process in which attributes(data member) and behavior(member function) of a objects in combined together as a single entity refer as class.

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!