Friday, January 8, 2010

Hey, what's the difference between object oriented programming language and function(al) programming? thanks!!?

More specifically, I need to be able to differentiate between C programming language and the Java programming languageHey, what's the difference between object oriented programming language and function(al) programming? thanks!!?
Reusability of code





Polymorphism


Inheritance


encapsulation





also: java doesnt allow you to mess with memory adresses directly. (thank god)Hey, what's the difference between object oriented programming language and function(al) programming? thanks!!?
An object oriented language is a language that expresses things as ';objects'; which contain data and the code that acts on that data. OO languages support concepts like encapsulation, polymorphism, and inheritance.





In functional languages each function is completely self contained. You pass data in and the function passes results out with not side effects outside the function.





Neither C (or c++) nor Java are functional languages. Plain C (not C++) is not object oriented, but Java is object oriented.



Ok here we go:


Differences between functional and objected oriented programming can


be summed up as follows:


In object oriented programming everything (or almost everything) is


treated as an object that can be modified and that can perform tasks,


or in OOP speak one might say objects have state and behavior. What it


buys you (among other more advanced things) is: modularity,and data


hiding.





Here is an example: You might have an object that models a ball, from


above the ball can be modified (i.e. you can change its state) for


example you may be able to change the color of the ball. It can also


perform tasks (i.e. it also has behavior) for example the ball can


roll, or be thrown. As an object it is bundled neatly in a package


that provides methods to change the state of the ball, and to make the


ball perform actions. This package is usually called a module, in


addition to the methods used to change state, and perform actions, the


module also has data that is used to store any state information


needed.





Because this module is a complete package that models your object


competely, the module can easily be reused in many different


applications. Once it is wirtten and working anyone should be able to


use the module without fully understanding internals. For examplea all


one needs to know is that they want a red ball to throw. :)





Here is a good resource on OOP:


';Object-Oriented programming concepts';


http://java.sun.com/docs/books/tutorial/鈥?/a>








In functional programming what you have basically are a set of


functions each of which performs a task. Selectively executing these


function results in the solution to the problem at hand.





For example you might have a function that takes the coords. of a


square computes the area, and you may have another function that


computes the area of a triangle. By executing the square function 6


times you could compute the area of a cube. Or by executing a


combination of the square and triangle functions you could compute the


area of a rhomboid. As you can see you can build quite complex systems


based on simple functions.





Technically choosing OOP of FOP (or vice versa) you can always


accomplish the same results in either paradigm, however what may be


very easy to do with an OOP approach may prove to be very difficult in


FOP, and the converse may also be true depending on the problem.





Which leads to: Why has the whole world moved from FOP to OOP...


Welp this one is simple, it hasn't (in my opinion). Much of the world


is moving to OOP, and OOP is getting all the press of late. But there


is still a TON of work going on in the FOP world. But I digress, that


is a different debate.





I think the examples above may have already shed some light on this


question, but simply put OOP makes it easier to conceive very large


complex systems. Because it gives the developers the ability to break


the system down to small tangible.. uhh.. objects... :) which can be


designed, developed, and tested independently.





In a functional paradigm what invariably happens as a large system


grows: it becomes a huge pile of functions. All of which are


interconnected, and dependent on each other. Which means if you change


one function you break 10 others, it becomes impossible to manage. A


*good* large program written in a functional language is usually


designed with OOP principles in mind. (i.e. it is designed as a set of


modules that are not inter-dependent on other modules)



1 comment: