Monday, October 26, 2009

C# Object oriented programming concepts

Object oriented programming is a successful paradigm shift from procedural world. Representing a real world entity through an object and encapsulating data and behavior together in an object are some of basics of OOPS world. Object oriented has showered us with many benefits esp. code re usability through inheritance and abstraction.  At the same time, we are pushed into complex world filled with classes, contracts, objects and relationships between them. Polymorphism, encapsulation, messaging, abstraction are OOPS concepts.

There are three kinds of relationships between objects

Association:  Simple relationship which relates two objects. Customer object is related to sales object. 

Aggregation: It is containment or composition model. Example: page class encompasses controls on the page.

Inheritance:  Defining specialized derived classes from preexisting generalized classes. It shows more of a parent-child relationship like vehicle -> car


Abstract classes and Interfaces:


A class can implement more than one interface but can only inherit from one abstract class. Since C# doesn't support multiple inheritance, interfaces are used to implement multiple inheritances. All elements in interfaces are public by default.

An abstract class can contain non-abstract methods. i.e methods with code in it.
An interface cannot contain non-abstract methods. Abstract classes SHOULD have atleast one method abstract and interfaces have all abstract methods.

Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.

An abstract class defines the core identity of a class and there it is used for objects of the same type.

An abstract class can contain non-static member variables.
An interface cannot contain non-static member variables.

A derived class uses the keyword "Extends" for an abstract class.
A subclass uses the keyword "Implements" for an interface.

If the subclass extends an abstract class, it cannot extend any other class.
If the subclass implements an interface, it can implement any number of other interfaces.

If the various implementations only share method signatures then it is better to use interfaces ( Multiple behaviors for different conditions)

Abstract classes provide a simple and easy way to version our components. By updating the base class, all the inheriting classes are automatically updated with the change. In an interface, creation of additional functions will have an effect on its child classes due to the necessary implementations of interface methods in classes. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.

Say there are two classes, bird and airplane, and both of them have methods called fly. It would be ridiculous for an airplane to inherit from the bird class just because it has fly() method. Rather, the fly() method should be defined as an interface and both bird and airplanes should implement that interface. If we want to provide common, implemented functionality among all implementations of component.

In VB.Net language, abstract classes are created using "MustInherit" keyword. In C#, we have "Abstract" keyword. Abstract class is designed to act as a base class. You cannot create a object of abstract class.

Shadowing:

When two elements in a program have same name, one of them can hide and shadow the other one. So in such cases the element which shadowed the main element is referenced.

Differences between overriding and shadowing:

  • Overriding redefines only the implementation while shadowing redefines the whole element.

Sealed classes:

The sealed modifier is used to prevent derivation from a class. An error occurs if a sealed class is specified as the base class of another class. A sealed class cannot also be an abstract class. In .Net Framework, string is a sealed class.

Virtual keyword: (Overridable)
They signify  that method and property can be overridden.

Static/Shared:

  • If you want an object to be shared between multiple instances you will use a static/shared class.
  • Static classes cannot be instantiated. By default, an object is created on the first method call to that object.
  • Static classes cannot be inherited. 
  • Static classes can have only static members.
  • Static classes can have only static constructor

Structures and classes:

  • Structures are value types and classes are reference types. So structures use stack and classes use heap. 
  • Structure members cannot be declared as protected, but class member can be.
  • You cannot define inheritance in structures.
  • Structures do not require constructors while classes require.
  • Objects created from classes are terminated using garbage collector while structures are not destroyed GC