Mimir:Draft2 Chapter8

Back to Table of Contents

=Chapter TODOs=
 * quote
 * summary
 * problem sets

=Chapter 8: Object-Oriented Programming= "QUOTE HERE" - Author

Objects
Objects are data abstracts that help programmers to write easy to understand, efficient and secure computer programs. Software objects are similar to real-world objects in two characteristics; state and behavior. Let's take cars for example. The state of the car is represented by its color, size, gear, etc. The behavior is what the car is doing or capable of doing. This could be signaling, turning or applying brakes. Software objects also consist of state and behavior. The state of an object is represented by its fields (variables), and the behavior of an object is represented by methods (functions).

This new concept of objects in programming introduced number of benefits. It made the process of building software modular. Objects are created and maintained independently. Objects can also be passed around inside the system. This also allowed programmers to hide the details of object implementations from the outside world. Programmers can reuse code in other parts of the system or just provide an API.

Classes
A class is simply a representation of a type of object. It is the blueprint, or plan, or template, that describes the details of an object. A class is the blueprint from which the individual objects are created. Class is composed of three things: a name, attributes, and operations. Hide  Copy Code

public class Student { }

According to the sample given below we can say that the Student object, named objectStudent, has been created out of the Student class.

Student objectStudent = new Student;

In real world, you'll often find many individual objects all of the same kind. As an example, there may be thousands of other bicycles in existence, all of the same make and model. Each bicycle has built from the same blueprint. In object-oriented terms, we say that the bicycle is an instance of the class of objects known as bicycles.

In the software world, though you may not have realized it, you have already used classes. For example, the TextBox control, you always used, is made out of the TextBox class, which defines its appearance and capabilities. Each time you drag a TextBox control, you are actually creating a new instance of the TextBox class.

Inheritance
Objects can share common behavior or attributes. For example, a car can be Ford, Chevrolet or BMW. Each one of these cars inherits certain attribute or behavior from the car. Inheritance allows software objects to share common attributes and behavior.

What is Encapsulation
The encapsulation is the inclusion-within a program object-of all the resources needed for the object to function, basically, the methods and the data. In OOP the encapsulation is mainly achieved by creating classes, the classes expose public methods and properties. A class is kind of a container or capsule or a cell, which encapsulate a set of methods, attribute and properties to provide its indented functionalities to other classes. In that sense, encapsulation also allows a class to change its internal implementation without hurting the overall functioning of the system. That idea of encapsulation is to hide how a class does its business, while allowing other classes to make requests of it.

In order to modularize/ define the functionality of a one class, that class can uses functions or properties exposed by another class in many different ways. According to Object Oriented Programming there are several techniques classes can use to link with each other. Those techniques are named association, aggregation, and composition.

There are several other ways that an encapsulation can be used, as an example we can take the usage of an interface. The interface can be used to hide the information of an implemented class.

IStudent myLStudent = new LocalStudent; IStudent myFStudent = new ForeignStudent;

According to the sample above (let’s assume that both LocalStudent and ForeignStudent classes have implemented the IStudent interface) we can see how LocalStudent and ForeignStudent hide their localize implementing through the IStudent interface.

As an example in both instances ‘myLStudent’ and 'myFStudent' are of type IStudent, but they both carries two separate local and foreign implementation underneath. This way a method call like 'DoLearn(object)' to 'myLStudent' and 'myFStudent' object will trigger their respective foreign and local implementation. This way 'myFStudent' carrying 'ForeignStudent' will trigger the respective learning function with foreign syllabus while the other one with 'LocalStudent' will trigger the learning function with local syllabus.

What is Association?
Association is a (*a*) relationship between two classes. It allows one object instance to cause another to perform an action on its behalf. Association is the more general term that define the relationship between two classes, where as the aggregation and composition are relatively special.

public class StudentRegistrar {   public StudentRegistrar ; {       new RecordManager.Initialize; } }

In this case we can say that there is an association between StudentRegistrar and RecordManager or there is a directional association from StudentRegistrar to RecordManager or StudentRegistrar use a (*Use*) RecordManager. Since a direction is explicitly specified, in this case the controller class is the StudentRegistrar.

To some beginners, association is a confusing concept. The troubles created not only by the association alone, but with two other OOP concepts, that is association, aggregation and composition. Every one understands association, before aggregation and composition are described. Aggregation or composition cannot be separately understood. If you understand aggregation alone it will crack the definition given for association, and if you try to understand composition alone it will always threaten the definition given for aggregation, all three concepts are closely related, hence must be studed together, by comparing one definition to another. Let’s explore all three and see whether we can understand the differences between these useful concepts.

What is the difference between Association, Aggregation, and Composition?
Association is a *has-a* relationship between two classes where there is no particular ownership in place. It is just the connectivity between the two classes. When you define a variable of one class in another class, you enable first to associate functions and properties of the second class. Then again both Aggregation and Composition are types of Association.

Aggregation is a weak type of Association with partial ownership. For an Aggregation relationship, we use the term *uses* to imply a weak *has-a* relationship. This is weak compared to Composition. Then again, weak meaning the linked components of the aggregator may survive the aggregations life-cycle without the existence of their parent objects. For example, a school department *uses* teachers. Any teacher may belong to more than one department. And so, if a department ceases to exist, the teacher will still exist.

On the other hand, Composition is a strong type of Association with full ownership. This is strong compared to the weak Aggregation. For a Composition relationship, we use the term *owns* to imply a strong *has-a* relationship. For example, a department *owns* courses, which means that the any course's life-cycle depends on the department's life-cycle. Hence, if a department ceases to exist, the underlying courses will cease to exist as well.

Whenever there is no ownership in place, we regard such a relationship as just an Association and we simply use the *has-a* term, or sometimes the verb describing the relationship. For example, a teacher *has-a* or *teaches* a student. There is no ownership between the teacher and the student, and each has their own life-cycle.

public class University {   private Chancellor  universityChancellor = new Chancellor; }

In the example given above, I can say that University aggregate Chancellor or University has an (*has-a*) Chancellor. But even without a Chancellor a University can exists. But the Faculties cannot exist without the University, the life time of a Faculty (or Faculties) attached with the life time of the University. If University is disposed the Faculties will not exist. In that case we called that University is composed of Faculties. So that composition can be recognized as a special type (strong kind) of an aggregation.

Same way, as another example, you can say that, there is a composite relationship in-between a KeyValuePairCollection and a KeyValuePair. As it was with the Faculty and the University, the two mutually depend on each other.

So in summary, we can say that aggregation is a special kind of an association and composition is a special kind of an aggregation. (Association->Aggregation->Composition)

What is Inheritance?
The ability of a new class to be created, from an existing class by extending it, is called inheritance.

OOP_Concepts_and_manymore/inheritance.gif

public class Exception { }

public class IOException : Exception { }

According to the above example the new class (IOException), which is called the derived class or subclass, inherits the members of an existing class (Exception), which is called the base class or super-class. The class IOException can extend the functionality of the class Exception by adding new types and methods and by overriding existing ones.

Just like abstraction is closely related with generalization, the inheritance is closely related with specialization. It is important to discuss those two concepts together with generalization to better understand and to reduce the complexity.

One of the most important relationships among objects in the real world is specialization, which can be described as the “is-a” relationship. When we say that a dog is a mammal, we mean that the dog is a specialized kind of mammal. It has all the characteristics of any mammal (it bears live young, nurses with milk, has hair), but it specializes these characteristics to the familiar characteristics of canis domesticus. A cat is also a mammal. As such, we expect it to share certain characteristics with the dog that are generalized in Mammal, but to differ in those characteristics that are specialized in cats.

The specialization and generalization relationships are both reciprocal and hierarchical. Specialization is just the other side of the generalization coin: Mammal generalizes what is common between dogs and cats, and dogs and cats specialize mammals to their own specific subtypes.

Similarly, as an example you can say that both IOException and SecurityException are of type Exception. They have all characteristics and behaviors of an Exception, That mean the IOException is a specialized kind of Exception. A SecurityException is also an Exception. As such, we expect it to share certain characteristic with IOException that are generalized in Exception, but to differ in those characteristics that are specialized in SecurityExceptions. In other words, Exception generalizes the shared characteristics of both IOException and SecurityException, while IOException and SecurityException specialize with their characteristics and behaviors.

In OOP, the specialization relationship is implemented using the principle called inheritance. This is the most common and most natural and widely accepted way of implement this relationship.

Summary
A quick summary of the chapter should go here

Key Terms
Class: A class is an extensible program-code-template for creating objects, providing initial values for state (member variables) and implementations of behavior (member functions, methods)

Encapsulation: Encapsulation is the packing of data and functions into a single component. The features of encapsulation are supported using classes in most object-oriented programming languages, although other alternatives also exist. It allows selective hiding of properties and methods in an object by building an impenetrable wall to protect the code from accidental corruption.

In programming languages, encapsulation is used to refer to one of two related but distinct notions, and sometimes to the combination thereof:


 * A language mechanism for restricting access to some of the object's components.
 * A language construct that facilitates the bundling of data with the methods (or other functions) operating on that data.

Method: A method (or message) in object-oriented programming (OOP) is a procedure associated with an object class. An object is made up of behavior and data. Data is represented as properties of the object and behavior as methods.

Inheritance: In object-oriented programming (OOP), inheritance is when an object or class is based on another object or class, using the same implementation (inheriting from a class) specifying implementation to maintain the same behavior (realizing an interface; inheriting behavior). It is a mechanism for code reuse and to allow independent extensions of the original software via public classes and interfaces.

Instance: In object-oriented programming (OOP), an instance is a specific realization of any object. Formally, "instance" is synonymous with "object" as they are each a particular value (realization), and these may be called an instance object; "instance" emphasizes the distinct identity of the object. The creation of a realized instance is called instantiation.

Problem Sets
A list of practice problems