• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 289
  • Last Modified:

Difference between Objects, Classes, Instances, Methods

For todays I tried to figure out what was the difference between objects, classes, instances and methods! It is so confusing! I've read a dozen tutorials and haven't understand this properly yet! Take the following for ex:

public class HelloWorld extends Applet {    
  public void paint(Graphics g) {
    g.drawString("Hello world!", 50, 25);    
  }
}

Now I would think that "HelloWorld" is a class. That seems pretty obvious. "paint" I think is a method. Am I right? So in this exemple there aren't any objects and instances? (whatever they are)

I'm really confused... your help would be really appreciated! Thank you very much!

Jean-Francois Joly
jolyal@capitalnet.com
0
jolyjf
Asked:
jolyjf
1 Solution
 
msmolyakCommented:
Let me try it.

Object is a generic term used in OO programming to define something which has well defined properties and behavior.

Class in Java defines an Object.

Instance is something you get when you instantiate a Class, a representative of a Class. You can create many Instances of one class.

Your example is not very representative. But you can still identify the participants.

HelloWorld is a Class, so is an Applet (the former is a subclass of the latter). Class HelloWorld defines an Object which is an applet but with different from a "generic" applet defined by an Applet class.

Instance of a HelloWorld class gets created by a Java VM which is part of the browser displaying that applet. Somewhere it says: HelloWorld myApplet = new HelloWorld(). You jsut do not see this code (that is why I do not like your example). Then myApplet will be an instance of class HelloWorld.

Let me know if I confused you.
0
 
gadioCommented:
jolyjf, I'll also give it a try to help from a different angle and I hope this wouldn't confuse you.
As msmolyak said, Object is a generic Object Oriented programming term. Let use a traditional example. Say you want to represent
an object. This object is a cat. A cat have a definition, certain behaviors and there are many cats living in the word. So, the class - the definition of the cat can be (not in a specific language) "A four legged animal that can eat and walk". This definition can define most of the living animals. Now, the cat I define have several ways of behavior. I define them by "hungry", "afraid", "happy". These will be methods that will define the objects behavior under different stimulus. Now, I have three cats at home, and the definition is written on the paper. I need 'live' cats. The instantiation of the definition - the creation of a live copy of the definition, is the instance. I have three instances of a "A four legged animal that can eat and walk", that walk after me when hungry, run when afraid, and purr when happy.
Did that made any sense?
0
 
diakovCommented:
I totally agree. Try to read some introductory book in 'Java Programming'. There usually is a Chapter dedicated on Object Oriented approach in programming (which some people reasonably call Method Oriented).

Apart from the objects, instances, and classes, there many other interesting things, for example, concerning the members of the class definition - data members, operations (methods). Some keywords are: OOP, virtual mehtods, single(/multiple - not in java except interfaces) inheritance, interfaces, threads, and many others.

Wait,..., I'm accessing the university library now, let me see an appropriate book in Java...
"Java programming language handbook / Anthony Potts, David H. Friedel Jr.. - Scottsdale, AZ : Coriolis Group Books, cop. 1996. - XVII, 414 p. : ill. ; 24 cm. - (Internet programming series)
ISBN 1-88357-777-2"
This I find appropriate.

Success.
0
 
aviPCommented:
Hi,

I'll give your question a shot...

The way I see it, a Class in an abstraction, while an Object is concrete.  A Class is a "type", like int & float are types, while objects are concrete instances of these types.

For example:
  int x, y;
  float a, b;

Here the abstract data types are int & float, i.e. int & float are the Classes.  They do not exist in any sense of reality.  They merely define what integers and floats look like.  On the other hand 'x' & 'y' are Objects of type (or Class) "int".  They exist in memory, and have a certain set of properties that are defined in Class int.

OO languages like Java & C++ allow you to define your own types or Classes.

For Example:

/*---------------Define a Class------------------------------- */
class Car
{
//define properties of a car, eg. wheels, steering wheel,engine, etc
//define the behavior of a car, ie. what a car can do.  This are programmatically represented as "Methods".
}

/*---------------"Instantiate" the Class---------------------- */
public static void main (String[] args)
{
  Car MyCar, YourCar, HisCar, HerCar; //process of Instantiation
}

Here "MyCar", "YourCar", "HisCar", & "HerCar" are instances of the Class "Car".

I am not sure if that has helped at all, but I hope it has :)

Cheers,
AviP

0
 
msmolyakCommented:
A minor comment: int and float are not really abstract data types. They are so-called built-in data types and they make Java "not purely OO language" (I do not think they are a deficiency of the language). Thus variables of those types are not really objects, they are simply values.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now