Chapter 1 : Object Oriented Design

1.1 Concepts of Object Oriented Programming Language

 

Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data, in the form of fields, and code, in the form of procedures. A feature of objects is an object's procedures that can access and often modify the data fields of the object with which they are associated. Objects typically represent real-world entities such as humans, computers, animals, and buildings. As such, object-oriented programming organizes software around its data, rather than functions or logic.

 

There are several major benefits to using an object-oriented approach when developing software. These include:

 

- Encapsulation: The data fields of an object are accessible only through the object's procedures. This protects the data from accidental modification and makes the code easier to understand and maintain.

 

- Modularity: Objects can be designed such that they can be easily reused in other programs. This reduces the amount of duplicate code and makes software development faster and cheaper.

 

- Extensibility: New objects can be created by extending existing ones. This allows for new functionality to be added without having to modify existing code.

 

- Polymorphism: Objects can be designed such that they can take on different forms depending on the context in which they are used. This allows for greater flexibility and code reuse.

 

1.2 Major and Minor Elements

 

There are two major elements in an object-oriented design: objects and classes. Objects are the basic building blocks of an object-oriented system. They are the things that the system is designed to represent and manipulate. Classes are used to define the properties and behavior of objects.

 

1.2.1 Objects

 

An object is a thing that can be represented and manipulated by a computer program. Objects have two major characteristics: they have state and they have behavior.

 

The state of an object is its data, which includes all the information that describes the object's current condition. For example, if we were to represent a person as an object, their state might include their name, age, height, weight, etc.

 

 

 

The behavior of an object is the set of operations that can be performed on the object. For example, the behavior of a person object might include the ability to walk, talk, eat, sleep, etc.

 

Objects are often said to be "instances" of a class. This means that they are generated from a class template which define their state and behavior.

 

1.2.2 Classes

 

A class is a template for creating objects. It is a blueprint that defines the state and behavior of objects that are created from it.

 

Classes are used to represent real-world entities such as persons, buildings, animals, etc. They can also be used to represent more abstract concepts such as numbers, points in space, and so on.

 

Classes are composed of two parts: data fields and methods.

 

Data fields are the variables that store the state of an object. For example, a class representing a person might have data fields for name, age, height, weight, etc.

 

Methods are the functions that define the behavior of an object. For example, a class representing a person might have methods for walking, talking, eating, sleeping, etc.

 

In order to create an object from a class, we need to use a process called instantiation. This is simply the act of creating an object from a class template.

 

1.3 Relationships Among Objects

 

Objects can be related to each other in two ways: aggregation and association.

 

1.3.1 Aggregation

 

Aggregation is a relationship between two objects in which one object contains the other. This is also sometimes called a "has-a" relationship.

 

For example, consider a class representing a car. This class might have data fields for the make, model, and year of the car. It might also have methods for starting, stopping, and driving the car.

 

Now consider a class representing a person. This class might have data fields for the person's name, age, and address. It might also have methods for walking, talking, and breathing.

 

 

The relationship between these two classes is aggregation. A car has a make, model, and year. A person has a name, age, and address.

 

1.3.2 Association

 

Association is a relationship between two objects in which each object contains a reference to the other. This is also sometimes called a "has-a" relationship.

 

For example, consider a class representing a car. This class might have data fields for the make, model, and year of the car. It might also have methods for starting, stopping, and driving the car.

 

Now consider a class representing a person. This class might have data fields for the person's name, age, and address. It might also have methods for walking, talking, and breathing.

 

The relationship between these two classes is association. A car has a make, model, and year. A person has a name, age, and address.

 

1.3.3 Using Association

 

There are two ways to use association: composition and aggregation.

 

Composition is a type of association in which one object contains another object. For example, consider a class representing a car. This class might have data fields for the make, model, and year of the car. It might also have methods for starting, stopping, and driving the car.

 

Now consider a class representing a person. This class might have data fields for the person's name, age, and address. It might also have methods for walking, talking, and breathing.

 

The relationship between these two classes is composition. A car has a make, model, and year. A person has a name, age, and address.

 

Aggregation is a type of association in which one object contains a reference to another object. For example, consider a class representing a car. This class might have data fields for the make, model, and year of the car. It might also have methods for starting, stopping, and driving the car.

 

Now consider a class representing a person. This class might have data fields for the person's name, age, and address. It might also have methods for walking, talking, and breathing.

 

The relationship between these two classes is aggregation. A car has a make, model, and year. A person has a name, age, and address.

 

1.3.4 Instantiation

 

In order to create an object from a class, we need to use a process called instantiation. This is simply the act of creating an object from a class template.

 

For example, consider a class representing a car. This class might have data fields for the make, model, and year of the car. It might also have methods for starting, stopping, and driving the car.

 

In order to create an object from this class, we need to use the keyword new. This keyword will allocate memory for the object and call the constructor method for the object.

 

The constructor method is a special method that is called when an object is created. This method can be used to initialize data fields or perform other actions.

 

1.3.5 Meta-class

 

A meta-class is a class that contains information about other classes. Meta-classes are often used to store information about relationships between classes.

 

For example, consider a class representing a car. This class might have data fields for the make, model, and year of the car. It might also have methods for starting, stopping, and driving the car.

 

 

 

Now consider a class representing a person. This class might have data fields for the person's name, age, and address. It might also have methods for walking, talking, and breathing.

 

The relationship between these two classes is association. A car has a make, model, and year. A person has a name, age, and address.

 

1.3.6 Grouping Constructs

 

There are two types of grouping constructs: inheritance and delegation.

 

Inheritance is a type of grouping in which one class inherits the properties of another class. For example, consider a class representing a car. This class might have data fields for the make, model, and year of the car. It might also have methods for starting, stopping, and driving the car.

 

Now consider a class representing a person. This class might have data fields for the person's name, age, and address. It might also have methods for walking, talking, and breathing.

 

The relationship between these two classes is inheritance. A car has a make, model, and year. A person has a name, age, and address.

 

Delegation is a type of grouping in which one object delegates responsibility to another object. For example, consider a class representing a car. This class might have data fields for the make, model, and year of the car. It might also have methods for starting, stopping, and driving the car.

 

Now consider a class representing a person. This class might have data fields for the person's name, age, and address. It might also have methods for walking, talking, and breathing.

 

The relationship between these two classes is delegation. A car has a make, model, and year. A person has a name, age, and address.

 

1.3.7 Conclusion

 

In this chapter, we have covered the basics of object-oriented design. We have discussed the major and minor elements of object-oriented languages and the relationships between them. We have also covered some of the more advanced topics such as instantiation, meta-classes, and grouping constructs.

Comments