Wednesday, July 16, 2014

Object Oriented Programming (OOP) Tutorial

In this blog I would like to present a simple tutorial on the basic principles of Object Oriented Programming (OOP) and the design patterns that are the basis of modern programming.
For convenience I will be using C#.

1. History

The principles of object oriented programming existed even before modern computers came into being. However the so-called 






1. Basic OOP Concepts

Object Oriented Programming is built on three principles: Encapsulation, Inheritance, and Polymorphism.

1.1. Encapsulation

Encapsulation allows you to hide data and implementation details and expose them in a controlled manner through publicly exposed methods.
Objects are self-describing things with internal structure not visible to the external world. They encompass one single idea or concept and they provide methods that allow external objects to interact with them.

1.2 Inheritance

Inheritance allows you to create a new class, using the previously defined methods of a base class.
Your new class will expose the existing methods of the base class as if they were newly defined. As such, creating a method with the same signature as the base class will hide the method in the base class.
NOTE: Inheritance is not polymorphism.

1.3. Polymorphism

Polymorphism occurs when an instance of a child class is treated as an instance of a parent class. Calling a method on the parent class will then express different behaviors, based on how the child class has overridden the parent class method.

Example:

As an example, you have a class called ‘Printer’. It defines certain actions, such as ‘Print’.
You then create a child class called Cannon 233. In this class you override the Print method.
You then call the ‘Print’ method on Printer. Since Printer is an instance of Cannon 233, the document is printed using the implementation encapsulated by Cannon233’s overridden method.
NOTE: In C#, polymorphism doesn’t occur unless you use either the key words virtual or abstract.

1. 4. OOP-Class

A class is the basic construct in Object Oriented Programming. As such it supports Encapsulation, Inheritance, and Polymorphism.
Note: When people talk about classes, they are referring to pure abstract classes that don’t have any implementation details. All you have are method signatures. As such, multiple inheritance is allowed.
For example:
pubic class Printer
{
    int PrintDocument(Document doc);
   
    Preferences GetPreferences();
   
    void Cancel(int cancelToken);
}

An important thing to keep in mind is that it is not the intent of OOP to prevent code duplication. That is just a side-effect of proper OOP design.
1.4.1. Responsibilities
  • Data Consistency: A class is responsible for making sure that the data it encapsulates is in a legal state between method calls.
  • Data Hiding: Class fields (integers, strings, etc.) should always be private. This data should only be exposed through methods.
  • Data initialization: It is the responsibility of the class to begin its life in a consistent state that is useful to the user.
  • Resource Management: Classes must make sure all resources are returned to the system when a task completes.
1.4.2. Data Structures
There is a fundamental difference between a class as defined in OOP and one as defined in a modern language such as C#. Unfortunately they both use the same name, which causes untold confusion. As such I will use OOP-Class to refer to the idea that OOP tries to encapsulate.
The following are various data structures relevant to modern programming…
C# Class
Supports: Encapsulation, Inheritance, Polymorphism
The C# class is the what people think of when they say ‘class’. C# classes are OOP-Class, since they enable encapsulation, inheritance, and polymorphism.
The fact that C# classes are reference types and reside on the heap is not relevant to the concept of an OOP-Class. That’s just an implementation detail.
Interface
Supports: Encapsulation, Inheritance, Polymorphism
The C# interface is the closest thing we have to the classic OOP-Class. The reason is that it only defines the operations in terms of method signatures. As a result, multiple inheritance is possible.
Interfaces are important since they are the basis for many design patterns.
Struct
Supports: Encapsulation
In C#, a struct is Not an OOP-Class. The reason is because it doesn’t provide inheritance (and polymorphism).
Structs are data structures that are used to implement integers, floats and other value types, including composite values.
Structs implement encapsulation. As a result, methods are exposed to modify its internal state.
C# Enums
Supports: <None>
In C#, an enum is Not an OOP-Class. The reason is because C# enums are just named lists of built-in numeric types such as int and float.
Java Enums
Supports: Encapsulation
In Java, the enum is a type of struct that has methods and internal states. However, just like enums, they don’t offer inheritance.
Prototype Collection
Supports: Encapsulation, Inheritance
See:
1.4.3. Objects
Classes define functionality. However, you can’t do anything unless you define an object and assign it states that are useful to the problem at hand.
An object is a data structure containing internal data that is in some valid state.
C# Objects
Objects are self-describing things with internal structure not visible to the external world. They encompass one single idea or concept and they provide methods that allow external objects to interact with them.



1.5. Methods

In OOP, methods encapsulate the specific single action you take to transform your data from its initial state to its final state.
The purpose of methods is to encapsulate actions performed of input data. When creating methods, you must ask yourself – What are the logical operations you are exposing through your API?
1.5.1 Method Variations
In traditional OOP, there is classes only implement methods. However, modern languages have variations on the theme. This allows us to encapsulate different design patterns within the language.
Methods





Properties
In C#, properties allow you to encapsulate internal fields and expose them to the caller. This allows the class to enforce policies that ensure its internal state is always consistent between method calls.
Static Methods
Static fields and methods are an odd thing in OOP. They don’t exist in traditional OOP.


























1.6. Conclusion

A programming language is considered object oriented if it implements a class as defined above.
According to the above definition C++ is an object oriented programming language.
That’s so last century. Smile with tongue out
When I think of OOP, I think of languages such as Java and C#, where everything is an object. It is impossible to program without creating a class definition and then calling methods on the class.
With C++, object oriented programming is possible, but not required.

2. Design Patterns

Design patterns are a funny thing. Everyone who has programmed with modern languages use the patterns without knowing that they are doing so. The reason is that many of them are built into the language.




3.1.1 Struct

A struct is a special type of class that is used to define values or collections of values.
The fundamental difference between a value type object





Reference

No comments:

Post a Comment