Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 [2]

Author Topic: Difference between a "Method" "Class" and "Object" (Java)?  (Read 54919 times)

Reelyanoob

  • Bay Watcher
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #15 on: September 05, 2011, 12:28:24 am »

Basically 'new' is the cloning machine for your army of bots.

BTW in c++ you can reprogram what the new operator does for an object you've created, to get some extra performance hacks, e.g one way I've only read about can rewrite the byte-level storage of the object, which can make your objects smaller and faster to create by removing the part of the object that refers to how big it is. You then program the 'delete' command for that object to hard-code how many bytes are deleted. (normally the generic delete looks at the object size and deletes that amount of memory).

Talk about NSFW programming!
« Last Edit: September 05, 2011, 12:38:14 am by Reelyanoob »
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #16 on: September 05, 2011, 03:20:02 am »

Slow down there Reely. He only just got what an object is, let's keep away from memory management yet.
Just like a single threaded program, gotta take things one step at a time.

Virex

  • Bay Watcher
  • Subjects interest attracted. Annalyses pending...
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #17 on: September 05, 2011, 06:42:21 am »

Oh, you nailed it, when you said, "Class Square //This is the blue prints for a square, but not one in itself."
I don't know why that definition did it but none of the others did, but I get it now! An object is something that is being built by the classes and methods, it isn't a physical thing that can be toyed with, but its classes and methods can, right?
Most programming languages won't let you alter a class while the program is running, mainly because nobody knows what is supposed to happen if you suddenly decide to remove a variable from your class definition (asides from crashing in a spectacular way). There are some languages that allow you to define classes progamaticly (allowing you to for example write a method that produces class definitions), but that's pretty advanced stuff.
« Last Edit: September 05, 2011, 09:57:40 am by Virex »
Logged

Falc

  • Bay Watcher
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #19 on: September 05, 2011, 02:48:56 pm »

Oh, you nailed it, when you said, "Class Square //This is the blue prints for a square, but not one in itself."
I don't know why that definition did it but none of the others did, but I get it now! An object is something that is being built by the classes and methods, it isn't a physical thing that can be toyed with, but its classes and methods can, right?

Hmmm, you seemed to have it right but then you mixed up a few things...

Let's work backwards a bit and take this forum here as an example and simplify it a bit. What we have here are users writing posts. Every user has a user name and a password. Every post belongs to a user and has content.

Those last two sentences are key. These are basically how classes are defined. Every x has a y. Every x can do z. The first sort, 'has a', is usually called an attribute. The 'can do' are referred to as methods. Every user can write a post.

Now, the class thus merely defines what all these users and posts will have in common. So what do you do when you actually want to get something done? You use the class (which is pretty abstract) to make a concrete object. This is sometimes called instantiating and some languages will use the word instance instead of object. This is what 'new' does. A new person comes to this forum and signs up, so the software needs to make a new user with the username and password he chose.

So instead of talking about every user having a username, we are now talking about *this specific* user who has *this specific* username. And now this specific user can make a new post, which the forum will automatically assign to this specific user while using the content that the user provided.

A class defines what every instance of it will share. It's impossible to explain or to understand classes without also explaining or understanding instances.
Logged

optimumtact

  • Bay Watcher
  • I even have sheep
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #20 on: September 06, 2011, 03:33:57 am »

The way we were taught it was to think of the class as a blank form, with the methods being sub sections on the form that defined what you could do with the data. Then an object was a photocopy of the class form with the data fields filled out, making it ready for methods to be called upon it. Because it was a copy of the original you could make as many as you like and each one would have it's own space for each bit of data. e.t.c.
Logged
alternately, I could just take some LSD or something...

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #21 on: September 06, 2011, 12:53:02 pm »

Here is the thing guys...

He asked about java, practically everything you guys just said isn't about java even if it is true for some other language (mostly c++).

In java, a class is an archetype, a unique pattern from which objects of a type are modeled after. That class may contain static methods as well as the definitions for object members and methods. In order to create an object from a class, the class must not be abstract, an abstract class may only have static methods and empty method definitions. A class may inherit the object methods and members of a super-class, and optionally redefine those methods and members. All classes eventually inherit from the Object class.

On object is created from a class and contains all the members and references to the methods defined in the class. It is created on the heap, which you don't normally worry about, but that is where the memory used for the members of each instance of on object.. An object is an instance of a class, as well as an instance of every class in the inheritance hierarchy all the way back to Object. This means the object can be cast into any of those classes.

A method is a segment of code that can be executed and is associated with a class. It is a function, and unlike some other languages all functions in java are methods because there is no way to define a function outside a class. Static methods can be called on the class itself, non static or object methods can only be called on an instantiated object.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #22 on: September 06, 2011, 03:25:29 pm »

He asked about java, practically everything you guys just said isn't about java even if it is true for some other language (mostly c++).

Trolololol. Java follow c syntax, so everything we say worky for Java too!
But seriously, this has been for Java.

an abstract class may only have static methods and empty method definition
Best not to lie to the kid. An abstract class can totally have fields and defined methods! But you still need to extend the class in order to create an instance.
Also, if he is learning about the difference between a class and an object, why are you trying to shove generalisation and polymorphism down he's throat? Not that helpful.

Let him fully understand the concept of an object and a class before we move onto things like static or inheritance. It is called a learning curve, and it takes some time, rather than showing somebody everything in a single sitting.

Andir

  • Bay Watcher
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #23 on: September 06, 2011, 03:51:13 pm »

Objects, methods, and classes hold pretty true for any language (conceptually)

Classes are blueprints, objects are the boxes made from that blueprint and methods are the holes in the box where you put things in or get things out of the box.
Logged
"Having faith" that the bridge will not fall, implies that the bridge itself isn't that trustworthy. It's not that different from "I pray that the bridge will hold my weight."

GlyphGryph

  • Bay Watcher
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #24 on: September 07, 2011, 08:16:14 pm »

Hey, I have a question.

I know in the languages I tend to work with, at least, an object references a class, and the class can actually be changed during execution (reflected in what can be done to the object post-change changing), as well as being able to extend individual objects to expand them beyond the initial class definition if need be. So you could give an individual square object a method for calculating its perimeter even if the Square class doesn't have one (or you could add the method to the class when you needed, retroactively making it available to all previous objects of that type).

How widespread is that sort of thing?
Logged

Max White

  • Bay Watcher
  • Still not hollowed!
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #25 on: September 07, 2011, 08:25:52 pm »

Used to be unheard of, but it is becoming more popular. What your describing is a property of a weakly types language. I'm guessing your describing either python or Ruby, the two most popular?
That doesn't work in Java as far as I know, and you can do it in c# for anonymous classes, but it isn't recommended at all.

Nadaka

  • Bay Watcher
    • View Profile
    • http://www.nadaka.us
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #26 on: September 07, 2011, 08:36:44 pm »

That is completely absent from java. Well, not completely. You can change a class at run time, but any already existing objects of a class are tied to the definition that was there when it was instantiated.
Logged
Take me out to the black, tell them I ain't comin' back...
I don't care cause I'm still free, you can't take the sky from me...

I turned myself into a monster, to fight against the monsters of the world.

Andir

  • Bay Watcher
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #27 on: September 07, 2011, 08:46:44 pm »

I'm pretty sure it's also possible in Erlang, Haskell(?), Lisp, and Clojure... if that matters at all. :p
Logged
"Having faith" that the bridge will not fall, implies that the bridge itself isn't that trustworthy. It's not that different from "I pray that the bridge will hold my weight."

Reelyanoob

  • Bay Watcher
    • View Profile
Re: Difference between a "Method" "Class" and "Object" (Java)?
« Reply #28 on: September 20, 2011, 08:58:12 pm »

You can simulate "changeable" classes in c++ by using pointers to other classes, and virtual methods. Not the most elegant solution ever, but it works.
Logged
Pages: 1 [2]