fog37 said:
My understanding is that OOP organizes code more efficiently grouping functions and variables that are "similar" (related) into an object.
More generally
object-oriented design (OOD) which provides a more natural and intuitive way to view the design process, is modeling software components just as we describe real-world objects i.e. by their attributes and behaviors. Also, OOD models communication between objects. Objects communicate via messages just as people do for instance.
fog37 said:
The term encapsulation is a fancy way to describe the idea that related variables and the functions that use those variables are grouped together into a unit called object. In regular procedural programming, the variables and the functions using those variables are in the same code but act more as separate entities.
It rather refers to the concept of
information hiding which is crucial to good software engineering.
When we design an object - and next implement it in an OOP language, we encapsulate attributes and operations (behavior) into objects. The attributes and operations of an object are intimately tied together. Now, objects have the property of information hiding. This means that, although objects may know how to communicate with one another across well-defined interfaces, objects normally are not allowed to know how other objects are implemented as implementation details are hidden within the objects themselves.
fog37 said:
Question: how does a method, which is essentially a function, perform some task without any input parameters? I am used to think that a function must have input(s) and output(s). Is that not always the case? There are surely void function that produce no output. Does that mean that they produce no numerical output? I think they still produce some outputs which get then assigned to a variable...As far as inputs, I have seen functions that have no inputs but use the external variables and their values to perform some calculation, etc..Aren't those external variables to be considered inputs for the function?
Methods in an OOP language and
functions in a procedural language have essentially the same functionality as both are called by their name and operate on data but in the case of a method, it is associated with an object. Now, a function
may be passed data on which it operates (input parameters) and
may return data i.e. some return value . When a method is called, it is implicitly passed the object on which was called and it operates on this so we also have the notion of a
class here which is basically a blueprint for the aforementioned object.
Now, a
void function performs a task, and then control returns back to the caller but, it does
not return a value. As for functions having no inputs that use "external" variables as you say, you most likely mean functions operating on
global variables which is a fairly dangerous programming practice. Global variables are visible i.e. can be used from any part of a program, so this does not refer exclusively to functions.
fog37 said:
An interesting feature of OOP is abstraction: this makes it easier to use an object. It is about by hiding (making abstract) some of its properties and methods within the object itself so the user only interfaces with certain methods and functions and does not need to care about the other variables and methods inside the object .
Abstraction also makes changes to the object easier to implement without affecting the rest of the code outside of the object.
Just to make it a little more clear, in the real world we, humans, think in terms of objects and we possesses the marvelous ability of
abstraction. For instance
, we can view a mountain in a digital image as object rather than as individual dots of color. In the OOP context, objects provide an abstraction which hides the internal implementation details. You are given a "handle" which are the methods that the object exposes to the programmer in order to use it but you don't have to know about these methods' implementation details i.e. how they produce the desired result.
fog37 said:
There is then inheritance which means that several objects can refer to a particular different object (which has its own properties and methods) and "inherit", i.e. refer to and use, the properties and methods defined for this particular reference object without having to redefine all those same properties and methods.
Inheritance is a form of software reuse in which classes are created by absorbing an existing class's data (attributes) and methods (behaviors) and embellishing them with new or modified capabilities. I'm referring to classes because these are the blueprints from which the objects are instantiated. The benefits of inheritance from a software engineering perspective are both saving time during program development through software reusability and also reuse of proven high quality software.