• Post Reply Bookmark Topic Watch Topic
  • New Topic

object/class  RSS feed

 
d skin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What's the different between a object and a class?
 
Stefan Krompass
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

why not ask
Google?

cu

Stefan
 
d skin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow! Why didn't I think of asking google instead of JavaRanch. Thanks!
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan:

If you haven't got anything to contribute, please refrain from contributing.

"D" --

A class is a kind of a thing, while an object is the thing itself. For example "book" is a kind of object; "this XSLT book on my desk" is an actual object. So it is with Java. In this line of code

String s = "Hello";

"String" is a class, and "Hello" is an object; the variable "s" can hold any object of the class String, and right now it holds one such object.
 
d skin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok if a class is a thing and the object is the thing itself then in your example String s = "Hello"; The object s which is equal to Hello tells me the String class s is Hello right?
 
Jason Fox
Ranch Hand
Posts: 114
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ok if a class is a thing and the object is the thing itself then in your example String s = "Hello"; The object s which is equal to Hello tells me the String class s is Hello right?

Not exactly, unless I'm misunderstanding your post, which is possible, I misunderstand alot. Anyhow, my two second explanation:
String is a 'Class' that may include any number of strings "hello", "goodbye", "crud", etc. In the above example 's' is an object of Class String, specifically "Hello". Classes are like blueprints for making objects. To keep with the example, the String class is a blueprint for creating strings, the actual string itself is an object.
 
Eric Freeman
author
Ranch Hand
Posts: 33
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think of a class as a type. It defines a bundle of data and associated operations. An object is an instance of that type.

For example, we could have a Person class that has some internal data (say, name and age) and some associated operations, like setName, getName, setAge, get Age.

To create an object of type Person in Java we'd just write:

Person bob = new Person();

or we could also create another person object

Person alice = new Person();

In the previous poster's example, String is a special case in Java, where you can type:

String s = "My String"; as a shorthand for

String s = new String("My String");

Hope this helps get you started. You'll also want to look at constructors, subclasses, interfaces, etc.

Eric
[ June 21, 2004: Message edited by: Eric Freeman ]
 
d skin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the objects do the work like setName, setAge, getAge right? And when you create an object of a type, what you're saying is take the blueprint of a class that was created already for you in the API or by another programmer and use methods defined in that class to solve parts of your problem like to get the age (i.e bob.getAge()).
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's getting there. "blueprint" is a nice word that works fairly well, though no metaphor is quite perfect.

Your examples of get & set as behavior could lead you off path, tho. Too much get & set activity turns an object into an unintelligent data structure. There's nothing wrong with data structures - that's all we had in COBOL and C and Pascal for years, but now we're trying to do something very different. So focus on object behaviors. What can they do for you? How can you divide the responsibilities in your own programs among various objects? There is much fun to be had exploring the possibilities.
 
Eric Freeman
author
Ranch Hand
Posts: 33
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, or as Stan is suggesting, calling a method that is more complex, like

bob.computeIncomeTax();

Keep in mind that the class may not come from an API, you might write the class yourself, possibly extending an existing class:

public class Cowboy extends Person {
private Horse horse;

public void ride() {
// your implementation here
}
}

Eric
 
d skin
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I see and because COBOL was the first programming lang. I learned I kind of think that way. But I've been reading How to program Java so I'm learning a lot of stuff, and I want to be able put it all together and getting the feeling that I'm on the right track helps! Like putting the terminology together and just beening able to think OOP. And any suggestions are welcome and thanks for the help so far it's been great for me!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!