Solved

Difference between Objects, Classes, Instances, Methods

Posted on 1998-08-26
5
232 Views
Last Modified: 2013-11-23
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
Comment
Question by:jolyjf
5 Comments
 
LVL 5

Accepted Solution

by:
msmolyak earned 200 total points
ID: 1222627
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
 
LVL 6

Expert Comment

by:gadio
ID: 1222628
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
 
LVL 8

Expert Comment

by:diakov
ID: 1222629
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
 

Expert Comment

by:aviP
ID: 1222630
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
 
LVL 5

Expert Comment

by:msmolyak
ID: 1222631
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now