Sean Paulson wrote:i do have some questions about that though. objects are variables or functions that are within other methods right
No. Objects are instances of a
class; and the business of a class is to define something useful. Now that "something useful" might well include other "things" which are themselves objects, but it's not a requirement.
The main difference between an Object-Oriented language like Java and older ones like C or BASIC is that those languages only allow you to define
methods and have a very limited set of data types - usually things like integers and Strings and decimals - whereas an OO language allows you to define classes, which
combine methods and data in a single
unit; and that unit can then be used anywhere else in your program as a "type".
Those older languages are called "procedural" because they usually involve writing programs as a set of instructions. For example:
Suppose somebody gave you the pieces of a carburettor for a car and asked you to put it together, you might well find the task impossible to do because the
order in which you do things is essential to the job. If you put flange C in place
before you've finished dealing with bracket B, you may find it impossible to tighten all the screws because they're now covered by the flange.
So you need a set of instructions that tells you what to do and (most importantly)
when to do it - and this is very similar to the instructions of a procedural language:
"insert bracket B; tighten screws in rotation; add flange C..."
However, there are problems which, though still relatively simple, are quite complicated to describe as a single set of instructions. Do you know how to ride a bike? Many of us do, and once we master the business of "turning into the direction you're falling" we can do it quite easily. But could you write a
program to do it?
Have you ever watched someone balance a pole on the end of their finger, or a helicopter hovering in place during a rescue. If you look closely, they're doing all sorts of minor corrections in real time to allow for changes in direction or wind speed. Could you write a set of instructions to do that?
The trouble is that these kinds of problems don't fit neatly into the "do this;
then do this; then do this..." style because there are
several things going on at once, and each has an effect on the others.
And this is where having objects
really come into its own: Because each object has methods
and data, we can get them to do things that would be very difficult for a single set of instructions. It's almost like having several mini-programs, each with its
own set of instructions, that can "talk to" and react with each other.
The trouble is that thinking "objectively" is more difficult for us puny humans. We
like "do this; do that; do the other..." instructions because they're easy to understand, and we can follow them in our mind, but trying to connect several of them together and thinking about all the possibilities makes our tiny brains explode.
Another problem is that Java is a bit of a "hybrid" language. You
can write large amounts of procedural code, and that's what you're doing at the moment. Eventually you'll learn how to get away from that style and think in terms of objects, but for most of us it doesn't come naturally. But don't worry; you'll get there in the end.
You might be interested to read my
WhatNotHow article. Even if you don't understand it completely right now, it'll give you a few things to think about.
BTW, in answer to your other question: The difference between a function and a method is that a function usually takes a value (or two, or three...) and returns a result,
without any outside effects - ie, it's completely self-contained.
So, for example:
double sqrt(int n) { ...
that calculates the square root of a number, would be a
function; but:
void setPrice(double newPrice) { ...
that sets the price of a product, would be a
method (and you'd probably write it for a
Product class).
In Java terms, they're
both methods, but the first one has the
characteristics of a "function".
HIH
Winston