Personal tools
You are here: Home Information Systems Java Platform Introduction 5. Objects Overview 5.1 What is a class?

5.1 What is a class?

Document Actions
  • Send this
  • Print this
  • Content View
  • Bookmarks

What is a class?

<-- Previous

Table of Contents

Next -->

A class is simply the blueprint for an object - the plan from which it will be built. If you go to an architect, you can buy the plans for a house, but you still won't have a house. You can build a house from the plans though. Think about it - in the real world, there are many instances of the same object, many tables, many dogs, many cars - but none of them is exactly the same as another. Even if two houses are built from the same plan, they may not be identical. The state of each object will usually differ from the state of any of the other similar objects. One automobile might be moving, another stopped; one green, another blue, and so on. Factories take advantage of blueprints to build many cars from the same plans - it would be very inefficient to draw up a new plan for every car you needed to make.

In programming, a class serves the same function as a blueprint - you can use it to make as many instances of an object as you like.

A class is a prototype that defines the variables and the methods common to all objects of a certain kind.

The class for our automobile example would declare the instance variables necessary to contain the current gear, the current engine speed, and so on for each automobile object. The class would also declare and provide implementations for the instance methods that allow the driver to change gears, brake, and accelerate the automobile.

Above, we see the difference between a class and an implementation or instance of a class, called an object. Classes define instance variables and instance methods to be inherited by the objects they produce, which are copies of the same that are defined in the class. Each object built from a class will use these methods to change the state of the object as required; different objects built from the same class may therefore have states which differ from each other at the same moment in time.

Classes can also define class variables. A class variable contains information that is shared by all instances of the class, i.e. all instances will have the same value in the class variable at all times. Changing the value of a class variable changes the value for all other objects of that type. For example, imagine that all automobiles have four wheels. Every automobile object would have its own copy of the numberOfWheels variable, but every object would have the value 4 in its copy of the variable. This is inefficient - far better to have a single variable that holds the value for numberOfWheels for all objects. This is what a class variable does, which is why if you change its value the value changes for all objects of that type. Class methods can also be defined. Class methods can be invoked directly from the class, but instance methods can only be invoked on a specific instance of the class.

In the real world it's usually pretty obvious that the blueprint of an object is not the object itself. In programming though, the idea of a class and its objects being separate entities can cause confusion, because quite often the term object is used to refer to both objects and classes, and they're both virtual (exist only in the computer's memory) in any case! In the images above the class is not shaded to indicate that it's just a blueprint, whereas the object is shaded to indicate that it actually exists and can be used by your code.

<-- Previous

Table of Contents

Next -->

Copyright 2007-2008, by the Contributing Authors. Cite/attribute Resource. 5.1 What is a class?. (2008, July 16). Retrieved June 20, 2013, from UWC Free Courseware Web site: http://free.uwc.ac.za/freecourseware/information-systems/java-platform-introduction/objects-overview-1/what-is-a-class. This work is licensed under a Creative Commons License : Attribution-ShareAlike 3.0. Creative Commons License : Attribution-ShareAlike 3.0