What is the best way or best resources or tutorials to learn Object Oriented Programming?

For instance, when I begin a program with Java and all my code goes into just one class and I can't estimate when I need to create new classes and operate on these classes together.

  • yes you can break the one class in many classes according to requirment and functionality so when other one try to understand he will go to that class only so its easier for problum solving and also less buggy code easiy to find the error . – Vikas Gautam Sep 18 '13 at 11:43
  • 1
    I got the concepts academically at school, but it took exposure to real-world projects to actually "get it". – ftr Sep 18 '13 at 11:56
  • 1
    Your question is too broad. How does one "learn" anything? There are thousands of books and online resources and projects for you to learn OOP. Just pick a couple that a friend or colleague recommends, and go from there. – Andres F. Sep 18 '13 at 12:10
  • Try getting a copy of Head First Design Patterns (amazon.com/First-Design-Patterns-Elisabeth-Freeman/dp/…) – Matthew Flynn Sep 18 '13 at 18:53

A class must aim to do one thing and do it well. Nothing else.

There's more to it than that, but that's the main thing. If you go by that you can't go wrong. If your class does too much, it's time to break it apart into smaller pieces.

This is known as the Single Responsibility Principle (emphasis mine):

In object-oriented programming, the single responsibility principle states that every class should have a single responsibility, and that responsibility should be entirely encapsulated by the class. All its services should be narrowly aligned with that responsibility.

Note that I am just answering your example. For the rest, your question is too broad. Your best bet to "get" OOP will be to indeed practice OOP. Just:

  1. Read a few books.
  2. Do some projects to break your teeth on these concepts a few times.
  3. Don't try to memorize all these fancy names (including the SRP above).
  4. Get your code bashed and reviewed.
| improve this answer | |

It starts at the design phase, before you write any code. Think about what your program is modelling. If its an e-commerce website you might think about Customer, order, product and so on. These nouns become your first objects.

Then you think about what processes need to occur. As an example, when the website takes a new order, there will need to be a point where you take payment for the order. You may define an object as Payment, and this object represents the payment details, and the status of the payment. Now different payment types need processing differently. So you could define a common interface IPaymentProcessor and then create payment processor objects that each only process particular types of payment. e.g PayPalPaymentProcessor and VisaPaymentProcessor.

At this stage, you are starting to write object oriented code...

| improve this answer | |
  • And a IPaymentProcessorFactory. – jsedano Sep 18 '13 at 14:23
  • 4
    @anakata And an IPaymentProcessorFactoryAdapterFacadeVisitorTemplateMethod. It's not Java otherwise. – Andres F. Sep 18 '13 at 14:25
  • 2
    Welcome to the Kingdom of Nouns! :P – Andres F. Sep 18 '13 at 14:26
  • 5
    Don't forget IPaymentProcessorFactoryAdapterFacadeVisitorTemplateMethodImpl, because being Java I hope IPaymentProcessorFactoryAdapterFacadeVisitorTemplateMethod to be an abstract interface. – jsedano Sep 18 '13 at 14:35

The Sun/Oracle API, particularly the Javadoc but also the source code itself, provides lots of good examples. You'll use this stuff anyway; when you do, just take a harder look at it than you need to do to get the job done. In particular, JTable is very interesting. It and its associated classes have way too many methods, which muddles it a bit as a learning tool. (I'm becoming convinced that convenience methods are evil.) But note particularly how it uses TableModel. (And also rows, row selection, columns, printing a cell, editing a cell....)

It can take a while to grasp what Sun was doing. OOP tends to be at right angles to everything else; you have to get your brain to think in different ways and then keep it thinking differently.

To try to put OO into two sentences: An object acts as a server, returning information or taking an action upon request. In doing this it can also act as a client and use other servers to do things or return information. (And it can treat, upon request, a client as a server and feed back information on its own schedule and initiative.)

| improve this answer | |
  • 1
    "OOP to me means only messaging, local retention and protection and hiding of state-process, and extreme late-binding of all things. It can be done in Smalltalk and in LISP. There are possibly other systems in which this is possible, but I’m not aware of them." Alan Kay – jsedano Sep 18 '13 at 14:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.