5

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.

FrustratedWithFormsDesigner
  • 26,726
  • 31
  • 139
  • 202
John
  • 51
  • 2
  • 8
    Based on your description, you haven't actually be doing nearly *any* OOP. Putting re-usable code into methods, when everything is all within the same object (MainForm) is just regular procedural programming. – Tyler McHenry Aug 19 '10 at 19:03
  • 3
    @Tyler McHenry: "just regular procedural programming" tarted up with class decorations. – S.Lott Aug 19 '10 at 19:04
  • 1
    These other questions might help you get started: http://stackoverflow.com/questions/2235986/how-can-i-think-in-oop http://stackoverflow.com/questions/2688910/learning-to-think-in-the-object-oriented-way http://stackoverflow.com/questions/293152/best-way-to-learn-net-oop-best-practices – Jeff Aug 19 '10 at 19:07
  • 1
    Related threads: http://stackoverflow.com/questions/24270/whats-the-point-of-oop, http://stackoverflow.com/questions/995161/how-to-explain-an-object – Péter Török Aug 19 '10 at 19:07
  • Not sure if the standard here is to comment on my own post or make an answer, but thank you everyone for the quick, bountiful answers. I will start working through all the links you guys gave me. Thanks! – John Aug 19 '10 at 19:13
  • 2
    @John. The standard is commenting, you got it right ;-) – Federico klez Culloca Aug 19 '10 at 19:14

6 Answers6

4

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
  • 74,820
  • 18
  • 121
  • 166
2

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

S.Lott
  • 384,516
  • 81
  • 508
  • 779
  • Hey Steve, I just noticed that's *your* book! ;) – FrustratedWithFormsDesigner Aug 19 '10 at 19:10
  • 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 Aug 19 '10 at 19:12
  • @Jon B: No, that sounds mean. I wasn't trying to get him in trouble, I just noticed that he's the author. – FrustratedWithFormsDesigner Aug 19 '10 at 19:13
  • 1
    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 Aug 19 '10 at 19:16
  • (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 Aug 19 '10 at 19:17
  • @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 Aug 19 '10 at 19:25
  • 4
    @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 Aug 19 '10 at 19:48
  • 4
    Well, you should probably still mention that it's your book in the answer. – Georg Schölly Aug 19 '10 at 20:02
  • 2
    Geez, guys - I was just yanking S.Lott's chain. You didn't have to actually flag this as spam! – Jon B Aug 19 '10 at 20:53
  • @Georg I think the "s_lott" in the URL is a bit of a giveaway – Michael Mrozek Aug 20 '10 at 02:20
2

You must go though this I can't create a clear picture of implementing OOP concepts, though I understand most of the OOP concepts. Why?

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.

Community
  • 1
  • 1
Shantanu Gupta
  • 20,688
  • 54
  • 182
  • 286
0

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
  • 26,726
  • 31
  • 139
  • 202
0

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
  • 34,554
  • 11
  • 80
  • 120
0

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.

John Alexiou
  • 28,472
  • 11
  • 77
  • 133