what is a class?
Author |
Message |
syntax_error
![](http://compsci.ca/v3/uploads/user_avatars/196798963948cf16794b6e5.jpg)
|
Posted: Thu Feb 15, 2007 8:05 pm Post subject: what is a class? |
|
|
Hello I just finshed gr.9 turing and got the ready to program java complier ans since it already has permade clsses....... I was wondering
if anyone can explain what a class is exactly..... in very basic terms so i may understand
thx will really like this explained
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
rdrake
![](http://compsci.ca/v3/uploads/user_avatars/113417932472fc6c9cd916.png)
|
Posted: Thu Feb 15, 2007 8:25 pm Post subject: RE:what is a class? |
|
|
Think of a class as a black box. You cannot see into the black box, but you can still interact with it. Everything the black box is mysterious. You do not care how it works, nor do you know how it works. All you care is that it works. The innards of the box may change, but you cannot see nor should you notice the change.
That is essentially what a class is. The End. |
|
|
|
|
![](images/spacer.gif) |
PaulButler
![](http://compsci.ca/v3/uploads/user_avatars/60319641477fc8be91d94.jpg)
|
Posted: Thu Feb 15, 2007 8:46 pm Post subject: Re: what is a class? |
|
|
rdrake summed it up pretty nicely, and I would hate to confuse you by throwing another analogy out there... So if this confuses you, just ignore it .
A class is a blueprint that can be used to create one or more object. You have probably already worked with objects like String or Integer.
Classes go in their own .java files. Here is a very basic example:
code: |
public class something {
public int dosomething(){
System.out.println("hi");
}
}
|
I could then say in the main loop:
code: |
something a = new something();
a.dosomething();
|
|
|
|
|
|
![](images/spacer.gif) |
Clayton
![](http://compsci.ca/v3/uploads/user_avatars/1718239683472e5c8d7e617.jpg)
|
Posted: Thu Feb 15, 2007 10:35 pm Post subject: Re: what is a class? |
|
|
I'm gonna add to this.
Now, according to the above, a class in a blueprint for an object, correct? What this allows us to do, is build objects so that we can run code and such and do useful things with it. One of the best examples I can think of, is an engine. You don't need to know how it works, or why, all you need to know is that it does work. However, this engine needs input to actually run, hence the gas pedal. With this input, you get output. Again, you need not care where it came from or why, all you need to know is what it is, in this case power and torque. Now, because we don't have only one engine in the world, we create blueprints (or schematics or whatever you want to call them), so that we always have instructions to build another engine. This would be a class. With it we can build as many engines as we want, and do the exact same things that we could do with any other engine.
Now, say we want a different kind of engine. Are we just going to throw the old design out the window and say the hell with it? Probably not, instead, you are going to take the old design and create a new one based on the old design. Simply put, you modify the existing blueprint to create a new one. In programming terms, this would be inheritance (using the old blueprint (class) to create a new one) and polymorphism (changing the old class to make a new one).
Now, does creating this new type of engine limit it from going into cars? No, in fact, this new engine you have made will fit into any car just as well as the original one will. This is because the new engine is still an engine, even though it's different. Again, in programming terms, this is called the "is a" relationship. This means that the new type of engine "is a" regular old engine because it took (inherited) the old design and modified it (polymorphism).
Having run out of ways to keep this to my example of an engine, we can talk about parent and child classes. Think of your parents, when they created you, you inherited genes from them, and you were born. You are their child. The same is true in programming, any class that has inherited from another class is the child of that class. This is just another way of explaining the "is a" relationship, but is also a key way in explaining inheritance.
Seeing as this post has gone on a lot longer than I thought it would, I will end here. Hopefully this has shed some light on Object Oriented Programming (OOP) for you. If you have any questions, don't be afraid to ask. |
|
|
|
|
![](images/spacer.gif) |
syntax_error
![](http://compsci.ca/v3/uploads/user_avatars/196798963948cf16794b6e5.jpg)
|
Posted: Fri Feb 16, 2007 7:15 pm Post subject: Re: what is a class? |
|
|
thank you all of you all your explaintions were good and i got all of then thx a lot guy i actully get it now...................
now hte hard part writeing codes.....o well 1 problem at a time lol thx again off of you ![Laughing Laughing](http://compsci.ca/v3/images/smiles/icon_lol.gif) |
|
|
|
|
![](images/spacer.gif) |
wtd
|
Posted: Fri Feb 16, 2007 7:43 pm Post subject: RE:what is a class? |
|
|
Please keep in mind that object-orientation is about objects. Classes are only a small part of that equation, and only in some languages. |
|
|
|
|
![](images/spacer.gif) |
|
|