Infolink

 

Search This Blog

Nov 25, 2012

OOPS Concepts (PART-2)

Multiple inheritance

Multiple inheritance is the possibility that a child class can have multiple parents. Human beings have always two parents, so a child will have characteristics from both parents.

In OOP, multiple inheritance might become difficult to handle because it allows ambiguity for the compiler. There are programming languages such as C++ that allow multiple inheritance; however, other

programming languages such as Java and the .NET Framework languages do not allow multiple inheritance. Multiple inheritance can be emulated in .NET using Multiple Interface Inheritance.


Sealed class

A sealed class is a class that does not allow inheritance.  Some object model designs need to allow the creation of new instances but not inheritance, if this is the case, the class should be declared as sealed.

To create a sealed class in C#, the class declaration should be done as:

      sealed class Shape

To create a sealed class in VB.NET, the class declaration should be done as:

      NotInheritable Class Shape

Virtual keyword

The virtual keyword allows polimorphism too. A virtual property or method has an implementation in the base class, and can be overriden in the derived classes.

To create a virtual member in C#, use the virtual keyword:

     public virtual void Draw()

To create a virtual member in VB.NET, use the Overridable keyword:

     Public Overridable Function Draw()

Override keyword

Overriding is the action of modifying or replacing the implementation of the parent class with a new one. Parent classes with virtual or abstract members allow derived classes to override them.

To override a member in C#, use the override keyword:

         public override void CalculateArea()

To override a member in VB.NET, use the Overrides keyword:

        Public Overrides Function CalculateArea()

Abstraction

Abstraction is "the process of identifying common patterns that have systematic variations; an abstraction represents the common pattern and provides a means for specifying which variation to use" (Richard Gabriel).

An abstract class is a parent class that allows inheritance but can never be instantiated.  Abstract classes contain one or more abstract methods that do not have implementation. Abstract classes allow specialization of inherited classes.

Shape class, which is an abstract class. In the real world, you never calculate the area or perimeter of a generic shape, you must know what kind of geometric shape you have because each shape (eg. square, circle, rectangle, etc.) has its own area and perimeter formulas. The parent class shape forces all derived classes to define the behavior for CalculateArea() and CalculatePerimeter().  Another great example is a bank account. People own savings accounts, checking accounts, credit accounts, investment accounts, but not generic bank accounts. In this case, a bank

account can be an abstract class and all the other specialized bank accounts inherit from bank account.

To create an abstract class in C#, the class declaration should be done as:

      abstract class Shape

To create an abstract class in VB.NET, the class declaration should be done as:

      MustInherit Class Shape

To following code shows a sample implementation of an abstract class:
using System;

namespace DotNetTreats.OOSE.OOPSamples{
   
    public abstract class Shape{
       
        private float _area;
        private System.Drawing.Color _color;
        private float _perimeter;

        public float Area{
            get{
                return _area;
            }
            set{
                _area = value;
            }
        }

        public System.Drawing.Color Color{
            get{
                return _color;
            }
            set{
                _color = value;
            }
        }

        public float Perimeter{
            get{
                return _perimeter;
            }
            set{
                _perimeter = value;
            }
        }

        public abstract void CalculateArea();

        public abstract void CalculatePerimeter();

    }
}

1 comment:

Related Posts Plugin for WordPress, Blogger...