Solved

Difference between Objects, Classes, Instances, Methods

Posted on 1998-08-26
5
258 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
tomcat startup error 5 104
Facing this issue for maven proxy setting 2 20
Java 8 to Java 6 8 21
Delphi android app hide keyboard 3 16
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

828 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