Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Object Oriented Programming beyond just methods?

Tags:

oop

I have a very limited understanding of OOP.

I've been programming in .Net for a year or so, but I'm completely self taught so some of the uses of the finer points of OOP are lost on me.

Encapsulation, inheritance, abstraction, etc. I know what they mean (superficially), but what are their uses?

I've only ever used OOP for putting reusable code into methods, but I know I am missing out on a lot of functionality.

Even classes -- I've only made an actual class two or three times. Rather, I typically just include all of my methods with the MainForm.

like image 521
John Avatar asked Aug 19 '10 19:08

John


People also ask

What are the 4 basic methods in object-oriented programming?

Now, there are four fundamental concepts of Object-oriented programming – Inheritance, Encapsulation, Polymorphism, and Data abstraction.

What is replacing object-oriented programming?

The natural alternative to OOP is functional programming (FP). Functional Programming follows the idea that a piece of code is stateless and works in a declarative manner. This means that the program exists to solve a specific problem through transforming the input.

Which methods are used for object-oriented?

Four core concepts of object-oriented programming are abstraction, encapsulation, inheritance and polymorphism. Using abstraction, a programmer hides many of the details about an object and shows only the most relevant information.

Why object-oriented programming is highly being used?

Object-oriented programming is ultimately about taking a huge problem and breaking it down to solvable chunks. For each mini-problem, you write a class that does what you require. And then — best of all — you can reuse those classes, which makes it even quicker to solve the next problem.


2 Answers

OOP is way too involved to explain in a StackOverflow answer, but the main thrust is as follows:

Procedural programming is about writing code that performs actions on data. Object-oriented programming is about creating data that performs actions on itself.

In procedural programming, you have functions and you have data. The data is structured but passive and you write functions that perform actions on the data and resources.

In object-oriented programming, data and resources are represented by objects that have properties and methods. Here, the data is no longer passive: method is a means of instructing the data or resource to perform some action on itself.

The reason that this distinction matters is that in procedural programming, any data can be inspected or modified in any arbitrary way by any part of the program. You have to watch out for unexpected interactions between different functions that touch the same data, and you have to modify a whole lot of code if you choose to change how the data is stored or organized.

But in object-oriented programming, when encapsulation is used properly, no code except that inside the object needs to know (and thus won't become dependent on) how the data object stores its properties or mutates itself. This helps greatly to modularize your code because each object now has a well-defined interface, and so long as it continues to support that interface and other objects and free functions use it through that interface, the internal workings can be modified without risk.

Additionally, the concepts of objects, along with the use of inheritance and composition, allow you to model your data structurally in your code. If you need to have data that represents an employee, you create an Employee class. If you need to work with a printer resource, you create a Printer class. If you need to draw pushbuttons on a dialog, you create a Button class. This way, not only do you achieve greater modularization, but your modules reflect a useful model of whatever real-world things your program is supposed to be working with.

like image 77
Tyler McHenry Avatar answered Oct 05 '22 05:10

Tyler McHenry


You can try this: http://homepage.mac.com/s_lott/books/oodesign.html It might help you see how to design objects.

like image 45
S.Lott Avatar answered Oct 05 '22 05:10

S.Lott