Question:
Java Programming Help - What is a class?
2012-07-02 14:08:17 UTC
I know this seems like a very basic question but due to poor teaching at college I have decided to learn how Java during the summer before I go to university. I brought a book to help me do this however because of my teaching at college I am slightly muddled with the meaning of a class.

A dog has a colour, a height, a weight, a fur pattern and so on.

Is a class the combination of all of these possible characteristics or is each of these possible characteristics a class, so the colour is a class, the height is a class and so on?

Or am i totally wrong? Ill be grateful for any help.

Thanks.
Five answers:
Jared
2012-07-02 14:39:31 UTC
Class is a complicated topic and basically the basis for learning Object Oriented programming. A class represents a prototype or data type for an object. So, the answer to you question is yes and no. First, NOT everything can be a class (it has to end somewhere). And in fact, in Java there is a "root" class:



Object



ALL classes (implicitly) extend the Object class. So there IS a concrete datatype, Object, which EVERY class extends (although I doubt you'll use any of this at first). Now, everything CAN be an Object, but due to reasons dealing with Memory (for which I am NOT going to go into--it's a drawback of Java), there are also several primitive types--these include:



integer types: short, int, long (16, 32, 64 bits)

char: 16-bit, unsigned integer (represented unicode characters, hence why it's 16 bit)

byte: unsigned integer (8 bits, 1 byte long)

float: 32-bit floating point number

double: 64-bit floating point number

boolean: either true or false (should be represented by single bit, but likely is a full byte in length)



The only real difference in primitive types and Objects, is where they are stored. ALL objects are stored on the heap (including String's) while primitive types, declared inline, will be stored in the stack (i.e. they will NOT need to be garbage collected). Primitive types that appear as members to a class will be stored in the heap where the object is created.



Furthermore, EVERY Object in Java is a pointer and ALL functions use pass by value (which confuses people because Objects and primitives are different)--primitives contain the actual value, while Objects contain ONLY the pointer value (akin to C/C++). What this means is that a method in Java can modify a given Object (through object methods)--but if a method reassigns an Object (passed as a parameter) then this will have NO EFFECT on the Object that you used to call the function (when you run your program)--this is likely beyond the current grasp of your understanding.



OK, so classes have two main functions:



1) They represent Objects and thus are prototypes for Objects

2) They represent functionality and thus "general" methods or members can be declared static... (I'm not going to go into this part)



1) Let's focus on what an object is:



An Object is something that represents a datatype (i.e. it's "something") AND it represents a functional (or perhaps methodical) interface. That is to say, that you have a Dog object, and likely it will be under the control of the program, so likely there are several actions that a Dog might do:



A dog will bark, he will bite (say another dog), he will fight (with another dog), he has a color, a weight, a height, a look (i.e. fur pattern).



So the ATTRIBUTES (color, height, weight, fur pattern) are all going to be members of the class.



First are these attributes changeable or permanent? Surely the height and weight will change and possibly the color will change, but surely the pattern will remain the same! If we assume we have a FurPattern class, then we could do like this:



http://ideone.com/QqVrr



The point of Objects is that you have some actual object in mind (real thing you are trying to model), then you figure out what all components make up this object? Create those components--they could be THEMSELVES objects (like the FurPattern or Color object), but sooner or later you HAVE to reduce an object to primitive types (i.e. an integer, floating point, character, boolean, or String). So don't create objects for those variables...



...you are going to use so many bad habits depending who your teacher is, I pray for you.
husoski
2012-07-02 22:15:11 UTC
You're not totally wrong. You seem to be mixing up what's a "class" and what's an "object", though.



A class, in the Java sense, is a type; a user-defined data type (often abbreviated UDT). Java uses a class as a template for creating objects. If you understand the concept that "int" is a data type, but "int weight=7;" declares and creates a variable whose type is "int", that's the same relationship as between "class" and "object". If you have a class named Dog, and the declaration:



Dog asta = new Dog("terrier");



Then Dog is a class, but asta is an object, also called an instance, of type Dog.



In some Object-Oriented languages, every data value is an object. Not so in Java. Java has primitive data types like int, double, char, boolean, which are not class types and instances of those types are values...not objects. Objects and values are treated differently in Java, particularly when passing as parameters or comparing with == and != operators. You'll read about those later.



Every Java object of a given class type has some number of named "instance variables". The class declaration defines what these variables are and what their names are. So, in a particular dog object, there might be an instance variable named "weight". This can be either a primitive value, like a double or int, or it could be an object. The Dog class defines the name "weight" and what the type of "weight" is, and that determines whether "weight" is an object or a value.



So, the weight of a Dog is not a class, but may be of a class type. It's not "a class" in the sense that 42 is not a "data type". It's common to abbreviate "asta is an instance of Dog" to "asta is a Dog", just like you'd shorten "42 is an instance of type int" to "42 is an int", but you wouldn't say "42 is data type".
green meklar
2012-07-03 06:48:17 UTC
A class is a description for a type of thing. If you have two dogs (named Fido and Rover) and a fish (named Oscar), then Fido, Rover and Oscar are all objects, and dog and fish and animal are classes. Fido and Rover are objects of the class dog, Oscar is an object of the class fish, and both dog and fish are subclasses of animal.



Of course, you don't necessarily need to represent dog, fish, animal or whatever in your particular program, and you'll only use the classes you need. If your program deals with dogs and fish (in the sense that they are dogs and fish), or some representation thereof, then you might want dog, fish and animal classes. If your program deals with bank accounts, you might want bankaccount, customer and transaction classes. You think about what sorts of things your program needs to deal with, and whenever you can say of some subset of the things 'each of these things is an X', that's a good indication that you want X to be a class, since those things are going to have common attributes that you could describe in a class.



It turns out that, although the physical analogies like dogs and bank customers and so on come up a lot when Java is being taught, in real-world programming things are not quite so simple. A lot of the time, a class used in a program has absolutely no physical analogy, and is just there to make it easier to keep track of some kind of data. When you're programming in an object-oriented language, you have to think about when you are likely to want many data elements with similar properties, whether or not they have physical analogies, because those are the cases when you probably want to take advantage of classes. For instance, if I had a binary tree data structure in order to do fast searching, I would make a class called something like 'node' so that each node in the tree could be an object of that class. There are no binary trees or nodes in the real world, nor will the user ever even notice that the program is using nodes, but encapsulating the data that way makes the program work nicely and so we use a class anyway.
Derp
2012-07-02 21:13:38 UTC
A class is basically a template for an object.



So you could make a class called "Dog" that contains some attributes: a colour, a height, a weight, a fur pattern and so on, as you said.



You can then make a Dog Object, which will have each of those objects inside of it. So you have a Dog which has multiple different attributes associated with it. When you make a new Dog object from your Dog class (think of it as a template, making a new object uses the template, which is the class file), this new Dog object will contain each of these attributes. The process of making a new object from your class (template) is called "instantiation", you're making an INSTANCE of this Dog object.



So you can have a class:



public class Dog

{



public int height;

public int weight;

public String name;



public Dog(int h; int w; String n;)

{

height = h;

weight = w;

name = n;



}

}



This class allows you to make a dog object like so:



Dog nameItWhateverYouWant = new Dog(3,15,"Spot");



^ That code looks at your Dog template and makes a Dog type object, which contains three attributes: height, weight, and name, all of which were specified when you created the object. So now we have an object made from our class (the template), and it has its own attributes, which each hold a value.
2012-07-03 14:51:02 UTC
A Class is a blueprint for an Object.



In object oriented programming (like Java, C++, Python etc...), the real world is represented in terms of objects. So if you want to create a program, we create objects and they interact among themselves. Like the dog is an Object, human is a object, leash is an object, shoes can be an object etc... So, here one object Human interacts with a Dog object, interactions like walking the dog, grooming the dog etc....



An object has to important properties:

1. State

2. Behavior



State, explains about the various aspects like color, height, weight, number of legs, length of tail etc... as you can see, its just a value.

Behaviour, talks about the actions that can be performed ON the object and BY the object etc... like, barking, walking, running, licking, grooming of the dog etc....



So now, how to tell the computer/compiler about these objects and how they interact? We write a Class for that and store in in a .java file.



The Class is like a blue print of a building.... we create the blueprint before we build the actual house/building. We ask an construction engineer to give us a blueprint, once we are happy we can ask the construction company to create the house using that blueprint.



So we create a class and use it to tell the computer to create an object. State, is represented by the variables and Behavior by the Methods.



So when we are creating a Dog class we write in a java file:



class Dog{



double weight, heights, tail_length;

double energy_level;



void walk(){

// do something here.... like lower energy level as the dog loses energy when it does some physical activity;

}

}





In the above small program, the dog has few properties like weight, height, energy level etc.. and it can perform actions like walk.



And.......

The way you create objects is up to you. You can definitely create an Object for Height or just store it in a double variable like I did in the above program as its just a number. But for like the Fur Pattern you again want to break it down to things like... color of the fur, length of the hair, number of coats etc... so you can again create an Object for that... and you can perform actions on the fur when there is something like the grooming action is performed, we call a method inside the Fur Object to update the variables.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...