1

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.

haylem
  • 29,005
fthkk
  • 29

3 Answers3

3

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.
haylem
  • 29,005
0

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...

Michael Shaw
  • 10,114
0

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.)

RalphChapin
  • 3,320