What is a Class?
Home ] Up ] [ What is a Class? ] What is an Instance? ] Class Members ] Methods and Overloading ] Instance Creation ] Instance Destruction ] Finalizers ] Class Variables and Methods ] Wrapper Classes ]

 

 

Humans classify things all the time.

It helps us from drowning in details:

"What vehicle ran over you, madam?"
"A blue Buick four-door sedan."

rather than:

"What vehicle ran over you, madam?"
"Why, officer, it had a blue body, four wheels, four doors, six windows, a front, back and two sides of some kind of metal. There were four people sitting inside it, and one of them was driving. Each door had a window, a handle, and pinstripes, was about five feet high and four feet wide. In front, there was a shiny metal grille, and ..."

For example:

  • A Dog is a distillation of the common attributes and behaviors of dogs of all sizes, types, and ferocities.
  • A Car is a similar distillation applied to cars of all sizes, colors, speeds, styles, makes, etc.
  • A Nut describes common attributes of Filberts, Brazils, Almonds, Macadamias, etc. Or it describes the attributes of the kind of Nut that attaches to a Bolt (or perhaps even yours  truly...)
  • A Road describes the common attributes of dirt tracks, town streets, county roads and superhighways.
  • A Student encompasses those of types young and old, single and married, male and female, tall and short, etc.
  • A College describes Technical Colleges, Liberal Arts Colleges, Colleges of Art and Design, Universities, etc.

To perform this kind of classification, we need to distill out:

  • Attributes or Properties (has fur, four legs, a tail, sharp teeth, etc.)
  • Behaviors or Operations (barks, walks, runs, wags tail, etc.)

Here's an example of a Java class:

public class Dog
{
    public void StartBarking() { /* ... */ }
    public void StopBarking() { /* ... */ }

    public int getAge()   { return m_age; }
    public int getColor() { return m_color; }
    public int getTemperament() { return m_temperament; }

    public static final int DOCILE = 1;
    public static final int GOOD_WITH_CHILDREN = 2;
    public static final int VICIOUS = 3;

    int m_age;
    int m_color;
    int m_temperament;
}

Here's another example:

import java.lang.Math;

public class Circle
{
    // Accessor methods
    public double getX()      { return m_x; }
    public double getY()      { return m_y; }
    public double getRadius() { return m_r; }
    // Mutator methods
    public void setX(double v)      { m_x = v; }
    public void setY(double v)      { m_y = v; }
    public void setRadius(double r) { m_r = r; }
    // Operations/Attributes
    public double getCircumference()
                { return 2 * Math.PI * m_r; }
    public double getArea()
                { return Math.PI * m_r*m_r; }

    double m_x, m_y;	// coordinates of center
    double m_r;		// radius
}

Note: Think of a class as a template, or a recipe from which to make instances of that class.

Note: It's hard to tell an abstract concept "Dog" to go fetch!

 

This page was last modified on 02 October, 2007