Showing posts with label Object Oriented Programming (OOP). Show all posts
Showing posts with label Object Oriented Programming (OOP). Show all posts

Monday, April 19, 2010

Object Oriented Programming: Polymorphism

What is Polymorphism?

Polymorphism allows objects to be represented in multiple forms. Even
though classes are derived or inherited from the same parent class, each
derived class can have its own behaviour. Polymorphism is a concept linked
to inheritance and assures that derived classes have the same functions
even though each derived class performs different operations.

Polymorphism is important not only to the derived classes, but to the base classes as well. Designers of a base class can anticipate the aspects of their base class that are likely to change for a derived type. For example, a base class for cars might contain behaviour that is subject to change when the car in question is a minivan or a convertible. A base class can mark those class members as virtual, allowing derived classes representing convertibles and minivans to override that behaviour.

Object Oriented Programming: Inheritance


What is Inheritance?

In OOP, a parent or base class can inherit its behaviour and state to children or derived classes. Inheritance gives you the ability to build new classes based on an existing class. You can then extend a base class by enabling a new class to inherit its characteristics and behaviour.

Inheritance allows one class (the sub-class) to be based upon another (the super-class) and inherit all of its functionality automatically. Additional code may then be added to create a more specialised version of the class (Polymorphism).

For example a base class of vehicles can have sub-classes for cars or motorcycles. Each would still have all of the behaviour of a vehicle but can add specialised methods and properties. This mechanism of redefining the functionality of the base class in the derived class is called “overriding

Some programming languages allow for multiple inheritances where a sub-class is derived from two or more super-classes. C# does not permit this but does allow a class to implement multiple interfaces. An interface defines a contract for the methods and properties of classes that implement it. However, it does not include any actual functionality.

Saturday, March 27, 2010

Object Oriented Programming: Abstraction.


What is Abstraction?

Abstraction is the process of hiding all but the relevant data about an object in order to reduce complexity and increase efficiency. The result of removing the elements not directly related to solving the problem at hand is that you're able to focus specifically on the problem and not be mired in the details of how your class works. The class's interface is the implementation of the abstraction.

This understanding will help you create an interface that gives the user access to the information and methods that they need, yet insulates them from the internal workings of the class. You need to design an interface not only to solve today's problems but also to abstract sufficiently from the class's internals so that private class members can undergo unlimited changes without affecting existing code. The user can then declare/modify attributes, variables and methods within the class without having to worry about clashes with other objects. This basically means that data within a class is only available /modifiable via the class methods.

Always consider the programmer who is going to instantiate or derive from the classes that you create when designing your classes. Designing the abstraction of your classes in a way most useful to the programmers using them is paramount in developing reusable software.

Access Modifiers

Abstraction in C# can be achieved by using Access modifiers when designing your class. Access modifiers are keywords used to specify the declared accessibility of a member (such as a variable, constant or method) or type (which could be a data type such as an integer or a string).

In C# there are 5 different types of Access Modifiers.
Modifier
Description
Public
There are no restrictions on accessing public members.
Private
Access is limited to within the class definition. This is the default access modifier type if none is formally specified
Protected
Access is limited to within the class definition and any class that inherits from the class
Internal
Access is limited exclusively to classes defined within the current project assembly
protected internal
Access is limited to the current assembly and types derived from the containing class. All members in current project and all members in derived class can access the variables.

public: Any code that uses a class can only access the methods that have been marked with the keyword public. The public declaration gives the interface to the external world and defines what the class does, as viewed by the rest of the world.

private: This means that it is not visible outside the class, Marking a field or method as private effectively ensures that that field or method will be part of the internal working of the class, as opposed to the external interface. The advantage of this is that if you decide to change the internal you can just make the change without breaking the code outside the class definition - nothing from outside of this class can access this field.

protected: The protected keyword makes a member accessible within its class and by derived class instances.

internal: The internal keyword makes a member accessible by any code in the same assembly, but not from another assembly. A common use of internal access is in component-based development because it enables a group of components to cooperate in a private manner without being exposed to the rest of an application’s code.

protectedinternal: The protectedinternal accessibility means protected OR internal, not protected AND internal. In other words, a protectedinternal member is accessible from any class in the same assembly, including any derived class in another assembly.

Thursday, March 11, 2010

Object Oriented Programming: Encapsulation



What is Encapsulation (or information hiding)?

Encapsulation (as in enclosed in a capsule), sometimes called information hiding, is the ability to hide the internals of an object from its users and to provide an interface to only those members that you want the client to be able to directly manipulate. 

Encapsulation provides the boundary between a class's external interface, its public members visible to the class's users and its internal implementation details. The result is that each object exposes to any class a certain interface (those members accessible to that class). The interface must encapsulate the implementation - hide it from other parts of a program and protect an implementation from unintended actions and inadvertent access, exposing only the members of a class that will remain static, or unchanged, while hiding the more dynamic and volatile class internals. 

The advantage of encapsulation means once the interface to the object is designed you don't have to be concerned as others work on it, fix bugs and find better ways to implement it. You'll get the benefit of these improvements but none of them will affect what you do in your program. Because you're depending solely on the interface nothing they do can break your code. Your program is insulated from the object's implementation. The implementation is insulated from anything that you or other users of the object might do.

Wednesday, March 03, 2010

Object Oriented Programming: The Class


What is a Class?
The difference between a Class and an object is a source of a lot of confusion for programmers new to the terminology of object-oriented programming. The basic building blocks of object-oriented programming are the class and the object.

In object-oriented programming, a Class is a construct which is defined by a programmer in code and is used as a template (or blueprint) to create objects of that class. This template describes the state and behaviour that the objects of the Class all share.

A class encapsulates the state and behaviour of the concept it represents.
·         It encapsulates state through data placeholders called attributes (or member variables);
·         It encapsulates behaviour through reusable sections of code called methods.

A class is used to create new instances (Objects) by instantiating the class. An Object doesn't exist until an instance of the class has been created. The class is just a definition.

Tuesday, February 23, 2010

Object Oriented Programming: The Object

The Object

What is an Object?

To define objects we have to define two things:
·         State (Attributes, properties)
·         Behaviour (Methods)

Real-world objects share two characteristics: They all have state and behaviour. Dogs have state (name, colour, hunger) and behaviour (barking, fetching, wagging tail).

Real-world objects vary in complexity; a desktop lamp may have only two possible states (on and off) and two possible behaviours (turn on, turn off), but a desktop radio might have additional states (on, off, volume, station) and behaviour (turn on, turn off, increase volume, decrease volume, seek, scan, and tune). You may also notice that some objects, in turn, will also contain other objects. These real-world observations all translate into the world of object-oriented programming.

The term “Object,” that gives OOP its name, refers to a conceptual object that represents an item in our program or system. This could be anything from a button on a web page or a computer file, to a real world object such as a car.

Software objects like real-world objects also consist of state and related behaviour.
·         An object stores its state (Attributes) in fields or variables
·         An object exposes its behaviour through methods (functions in some programming languages).
Methods operate on an object's internal state and serve as the primary mechanism for object-to-object communication.

Hiding the internal state and methods of a class through abstraction and requiring all interaction to be performed through an object's methods is known as encapsulation.

Bundling code into individual software objects provides a number of benefits, including:
·         Modularity: The source code for an object can be written and maintained independently of the source code for other objects. If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement. This is analogous to fixing mechanical problems in the real world. If a bolt breaks, you replace it, not the entire machine.
·         Information-hiding: By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world.
Code re-use: If an object already exists (perhaps written by another software developer), you can use that object in your program.

Wednesday, February 17, 2010

Principals of Object Oriented Programming (OOP)



I decided to write this as I remember how hard it was to get my head around the principals of Object Oriented Programming (OOP) when I made the jump from procedural (or sequential) programming in C to using OOP in  C#.
I think some programmers underestimate how big a leap this is when someone say from an engineering background who learned to program in a procedural language has difficulty with the principals of OOP. This is mainly because it just isn't how we learned to think about programming and requires having to relearn our approach to solving our programming problems.


Object oriented programming (OOP) is a programming model that uses the concept of breaking programs or assemblies into a collection of smaller more manageable Objects or building blocks thus keeping projects simple and promoting improved code reusability and maintainability.

A Class (classification - class) represents the definition for an object. A class is the generic definition of what an object is - a template. A term unique to OOP, instantiation, is simply the act of creating an instance of a class. That instance is an object.

A summary of the principals that define OOP are:

Encapsulation:
Binding related data and functionality in an object is called data encapsulation and allows the user to hide the information/behaviour of the object from the outside world using abstraction.

Abstraction:
Abstraction is the process of hiding all but the relevant data about an object in order to reduce complexity and increase efficiency.

Inheritance:
In OOP, a parent or base class can inherit its behaviour and state to children or derived classes. Inheritance gives you the ability to build new classes based on an existing class. You can then extend a base class by enabling a new class to inherit its characteristics and behaviour.

Polymorphism:
Poly meaning many, and morph meaning forms, literally many forms. Polymorphism allows objects to be represented in multiple forms. Even though classes are derived or inherited from the same parent class, each derived class will have its own behaviour. Polymorphism is a concept linked to inheritance and assures that derived classes have the same functions even though each derived class performs different operations.