Documente Academic
Documente Profesional
Documente Cultură
Object Classes
Hierarchy
Classes are used as templates for objects to define them. Classes are used in a hierarchical
nature. This means that a class may have subclasses. For examples 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
carburetor, 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;
}
};
printArea(rect);
printArea(tri);
}
System.out.println(figure.getArea());
}
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 Data
Scope
• Local
• Global
Java Basic Data Types
Also called primitive types.
• Integers - All types are signed.
○ byte - 8 bits
○ short - 16 bits
○ Int - 32 bits
○ long - 64 bits
• Floating point numbers
○ float
○ double - For more precise real number calculations.
• Characters - char
• Boolean - boolean - True or false.
Object Relationships
In the drawing below, the class "airplane" "has-an" engine and wings which is an aggregation or
composition relationship. The "jetfighter" and the "ultralight" class are "a-kind-of" the class
"airplane". The aggregation relationship is shown with a solid diamond, and the "a kind of"
relation is shown with an arrow. Of course, the jetfighter and the ultralight class would use the
keyword "extends" with the phrase "jetfighter extends airplane" when the class is declared to be
a subclass of the airplane class. The engine and the wings class would be a part of the airplane
class and be implemented inside the airplane class.