Object Classes

Hierarchy

Classes are used as templates for objects to define them. Classes are used in a hierarchial nature. This means that a class may have subclasses. For exampls a car might have the following subclasses:

  • Body
  • Engine
  • Tire (4 instances plus a spare)
  • Transmission
  • Seat(s)
  • Door(s)
  • Trunk

Each of these objects may also have a subclass. For example the engine would include carburator, pistons, cylinders, spark plugs and more. The object containing the subclasses is referred to as the parent class, also called the superclass. The object to subobject hierarchy is arranged from the less specific parent to the more specific subclass, also sometimes called the child class. As you move down into the subclasses, information is more specific. This means that the superclass or parent class is the generalization of the subclass and the subclass is the specialization of the parent class.

Class Relationships

When classes use or contain subclasses, the following relationships outline the differences between the respective classes:

  • A-kind-of - The A-kind-of relationship is a classic relationship such as a sports car is a kind of car, or a Ford mustang is a kind of a car. This refers from the specific (type of car) to general (car) description. This is a general description of the subclass to parent class relationship.
  • Part-of - This relationship is of the type described above where a car is composed of specific parts. This refers from the subclass to the parent or superclass. An engine is part of a car.
  • Has-a - This is the inverse of the part-of relationship and refers from the parent or superclass to the subclass. A car has an engine. This concept is called aggregation or composition and is designated by a filled diamond in the Unified Modeling Language (UML).

Class Declaration and Sections

When a class is declared, there are three possible sections inside the class:

  • Public - Data and methods in this section are accessible outside the class.
  • Protected - Data and methods in this section are accessible to this class and subclasses derived from this class or one of its subclasses.
  • Private - Data and methods in this section are not accessible outside the class. Only functions in this class have access to private functions and data.

The friend construct allows private data and methods to be accessible to classes that are declared "friends" with the class the private data and methods are in. An example of class declarations with friend declarations in C++ is:

class Aclass
{
   friend class Bclass;    //Make Bclass a friend class
   friend int  Zclass::ready();  //Make the ready() method in Zclass a friend
   private:
      int x, y;
      void funct1(int n)
      {
         x=n;
      }
   protected:
      int a, b;
   public:
      int x1, y1;
      functa(int m)
      {
         if (mᡂ) funct1(m);  //although x cannot be changed externally, this function called from external sources can change it.
      }
}

Inheritance

Inheritance is applied in the a-kind-of class relationship. As in all parent child relationships, children (subclasses) inherit the characteristics of the parent class, also called the superclass. What this means is when a subclass is created from a parent class, it will inherit the functions and data that is defined in the parent class. What this means is that the same attributes and methods are available to the subclass. For instance the car class is:

class Car {
attributes:
   float positionx
	float positiony
	float speed
   float direction
   float maxaccel = 50
methods:
   accelerate(float)
   turn(float)
}

An example of a subclass being declared follows:

class parent_classA
{
   int var1, b;
   setVariable(int n)
	{
      var1=n;
   }
};

class sub_ClassA : parent_classA
{
   setVariable(int n)
   {
      var1=b*n;
   }
};

The class sub_classA is the subclass of the superclass or parent class parent_classA. In this case sub_classA inherited the integer variables var1 and b along with the method setVariable(int) from the parent class parent_classA. It overrode the setVariable(int) method with a setVariable(int) method of its own. A derived class cannot access the private data of its parent unless specifically authorized by the parent.

When the child class is created using a parent class, additional values and functions may be added to the subclass to make it more specific. For instance if the parent class is a car, the child class may be sports car. The sports car could have additional data describing it in more detail. In this case, although the parent car class has an engine, the sports car may have a more powerful engine definition. Therefore in the sports car class, the engine is defined again with different information. In the example above maxaccel=120 may be defined rather than the value of 50 for a standard car. This way the original car engine characteristics are overridden in favor of the more powerful engine.

Multiple Inheritance

A subclass may inherit from two or more parent classes. This does not refer to two parent classes where one is a parent to the other. This refers to two parent classes that are independent of each other. This capability does not apply to Java. Java uses interfaces to add some functionality in this area.

Abstract Classes

Abstract classes are used to define a basic class which may contain specific defined attributes and methods along with specific abstract methods which are not fully defined. These methods not fully defined must be fully defined in the child class. For example a goemetric figure may have a drawing method along with X and Y position information. The draw method would be defined in the abstract class for the geometric figure (the geometric class). The draw method would be fully defined in all subclasses which are the type of geometric figure such as circle or square. Therefore the subclass circle or square would define their own draw method which would override the draw method defined in the geometric class.

Generic Types

Generic types are the complement to abstract classes although they are not classes. They are merely a template used to create classes. They define the methods to be used in the class, but leave the data or attributes to be defined in the specific class. List types are used for this purpose. The methods such as appending or removing items from the list are provided, but the data is not since it will depend on the type of list.

Messages

Messages are sent when an object calls another object method. There are three parts to the message:

An example of calling a method would be to first create an instance of the Car class (as in the above example) with the following line:

car Car;

Then call a method of the object car:

car accelerate(20);

This sends a message to the car object and calls the method.

Virtual Functions and Polymorphism

Methods are uniquely identified by:

Polymorphism (many form) refers to an objects ability to become many different object forms. Polymorphism allows multiple methods (functions) to have the same name. The functions are uniquely identified by the parameter list used. For instance if there are two methods with the name "method1" and one of them requires two integer variables and the other requires one integer variable in the parameter list, if "method1(int 10)" is called, the function with the single integer on the parameter list is called. The difference in parameter list may be that parameters passed to the function are of a different type rather than quantity. One method may require a string variable while another requires a float variable.

An example should help. In this example, the class "Shape" is defined in another file and has a getArea() method that is overridden by the Triangle and Rectangle classes. The getArea() methods in both Triangle and Rectangle share the same interface (The same number and type of parameters are passed which is none) although the methods are different. When the printArea(Shape) method is called from main, the correct method for the correct object is called although the method is actually different from one object to another. This is one way polymorphism is applied.

import Shape;

public Class Triangle extends Shape
{
   int height, width;

   public int getArea()
   {
      return (int) ((height * width)/2);
   }
}

public Class Rectangle extends Shape
{
   int height, width;

   public int getArea()
   {
      return height * width;
   }
}

public static void main(String[] args) 
{
   rect = new Rectangle;
   tri = new Triangle;

   printArea(rect);
   printArea(tri);
}

void printArea(Shape figure)
{
	int x, y;

   System.out.println(figure.getArea());
}

Constructors and Destructors

Constructors have the same name as the class they are defined in. They provide for automatic initialization of objects and parameters when the object is declared. Destructors are indicated with the tilde, ~, in front of them. They provide for automatic deallocation of resources used by the object. Constructor example:

class Aclass
{
   friend class Bclass;    //Make Bclass a friend class
   friend int  Zclass::ready();  //Make the ready() method in Zclass a friend
   private:
      int x, y;
   public:
      int a, b;

      Aclass()
      {
         x=0;
         y=0;
         a=0;
         b=0;
      }

      void funct1(int n)
      {
         x=n;
      }

      functa(int m)
      {
         if (mᡂ) funct1(m);  //although x cannot be changed externally, this function called from external sources can change it.
      }
}
Object Oriented Basics Guide Contents Page