tags:

views:

200

answers:

6

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.

+2  A: 

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

S.Lott
Hey Steve, I just noticed that's *your* book! ;)
FrustratedWithFormsDesigner
Uh oh. Now we have to downvote S.Lott and flag this as spam unless he puts a big disclaimer on there about how it's *his* book!
Jon B
@Jon B: No, that sounds mean. I wasn't trying to get him in trouble, I just noticed that he's the author.
FrustratedWithFormsDesigner
Looks good though. Object-oriented *design* as opposed to object-oriented *programming*, like it. I think the design aspect of programming lacks good treatments, possibly because it’s non-determinate — there’s no provably correct design for something.
Paul D. Waite
(Oh but I’m still well up for some downvoting and flagging and general merciless punishment for daring to mention one’s own book. *Without even* an Amazon link to generate affiliate revenue for Stack Overflow, *for shame*.)
Paul D. Waite
@Fru - we can't go soft on spammers like S.Lott. For crying out loud, he has spent nearly two years tirelessly volunteering his time on SO just to build up enough of a reputation to sucker one or two people into buying his book. :)
Jon B
@Jon B: Buying!?!?! Darn it. [Face-palm!] I've been missing out on the kind of revenue stream that would make this into spam.
S.Lott
Well, you should probably still mention that it's your book in the answer.
Georg
Geez, guys - I was just yanking S.Lott's chain. You didn't have to actually flag this as spam!
Jon B
@Georg I think the "s_lott" in the URL is a bit of a giveaway
Michael Mrozek
A: 

OOP can be used to model things in the real world that your application deals with. For example, a video game will probably have classes for the player, the badguys, NPCs, weapons, ammo, etc... anything that the system wants to deal with as a distinct entity.

Some links I just found that are intros to OOD:

http://accu.informika.ru/acornsig/public/articles/ood_intro.html

http://www.fincher.org/tips/General/SoftwareEngineering/ObjectOrientedDesign.shtml

http://www.softwaredesign.com/objects.html

FrustratedWithFormsDesigner
A: 

Keeping it very brief: instead of doing operations on data a bunch of different places, you ask the object to do its thing, without caring how it does it.

Polymorphism: different objects can do different things but give them the same name, so that you can just ask any object (of a particular supertype) to do its thing by asking any object of that type to do that named operation.

tpdi
+1  A: 

You must go though this http://stackoverflow.com/questions/1959295/i-cant-create-a-clear-picture-of-implementing-oops-concepts-though-i-understand

I had same scenario and I too is a self taught. I followed those steps and now I started getting a knowledge of implementation of OOP. I make my code in a more modular way better structured.

Shantanu Gupta
+3  A: 

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.

Tyler McHenry
A: 

I learned OOP using Turbo Pascal and found it immediately useful when I tried to model physical objects. Typical examples include a Circle object with fields for location and radius and methods for drawing, checking if a point is inside or outside, and other actions. I guess, you start thinking of classes as objects, and methods as verbs and actions. Procedural programming is like writing a script. It is often linear and it follows step by step what needs to be done. In OOP world you build an available repetoire of actions and tasks (like lego pieces), and use them to do what you want to do.

Inheritance is used common code should/can be used on multiple objects. You can easily go the other way and create way too many classes for what you need. If I am dealing with shapes do I really need two different classes for rectangles and squares, or can I use a common class with different values (fields).

Mastery comes with experience and practice. Once you start scratching your head on how to solve particular problems (especially when it comes to making your code usable again in the future), slowly you will gain the confidence to start including more and more OOP features into your code.

Good luck.

jalexiou