Object Oriented Programming Concepts (OOP) Introduction – Part 2

  • Facebook
  • Twitter
  • Delicious
  • StumbleUpon
  • Pinterest
  • Google Plus

Inheritance :

Creating a new class from an Existing Class by extending it, is known as Inheritance. Inheritance acts as the major concept in Object Oriented Programming language. Code re-usability is its main advantage.

Lets Implement Inheritance :

public class BaseClass
{
    public BaseClass() //Constructor
    {
        Console.WriteLine("Base Class Constructor"); //Print Statement
    }
    public void BaseClassPrint()
    {
        Console.WriteLine("Base Class Print Statement"); //Print Statement
    }
}
public class DerivedClass : BaseClass
{
    public DerivedClass() //Constructor
    {
        Console.WriteLine("Derived Class Constructor"); //Print Statement
    }
    private static void Main()
    {
        DerivedClass DC = new DerivedClass();
        DC.BaseClassPrint();
        Console.ReadKey();
    }
}

OUTPUT :
Base Class Constructor
Derived Class Constructor
Base Class Print Statement

In Main() method we have created new instance of DerivedClass. If we see here that derived class doesn’t have a mothed BaseClassPrint().
The BaseClassPrint() is inherited from the BaseClass.
The DerivedClass is Inherited from a BaseClass using a Colon “:”.

Inheritance is a good choice when :

  1. Our inheritance hierarchy represents an “is-a” relationship and not a “has-a” relationship.
  2. when to reuse code from the base classes.
  3. We need to apply the same class and methods to different data types.
  4. The class hierarchy is reasonably shallow, and other developers are not likely to add many more levels.
  5. We want to make global changes to derived classes by changing a base class.

Above Example is a Single Inheritance. Now lets see Multi-Level Inheritance.

public class BaseClass
{
    public BaseClass() //Constructor
    {
        Console.WriteLine("Base Class Constructor");
    }
    public void BaseClassPrint()
    {
        Console.WriteLine("Base Class Print Statement");
    }
}
public class DerivedClass : BaseClass
{
    public DerivedClass() //Constructor
    {
        Console.WriteLine("Derived Class Constructor");
    }
}
public class newDerivedClass : DerivedClass
{
    public newDerivedClass() //Constructor
    {
        Console.WriteLine("newDerived Class Constructor");
    }
    private static void Main()
    {
        newDerivedClass nDC = new newDerivedClass();
        nDC.BaseClassPrint();
        Console.ReadKey();
    }
}

OUTPUT :
Base Class Constructor
Derived Class Constructor
newDerived Class Constructor
Base Class Print Statement

DerivedClass will inherit all members of BaseClass except the Constructor. In above example newDerivedClass inherits DerivedClass which inherits BaseClass.

Base classes is instantiated before Derived classes.

C# support Single Inheritance only. Your Class can inherit only one Base Class. Lets consider the below scenario.

public class BaseClass
{

}
public class DerivedClass
{

}
public class newDerivedClass : BaseClass, DerivedClass
{

}

Above Example gives us a Compiler Error. As newDerivedClass cannot inherit Two BaseClass.

As C# doest not support Multiple Inheritance.

Polymorphism :

Ploymorphism means “many shapes”. Its a fundamental concept in Object Oriented Programming.

Polymorphism is of 2 types:

1. Compiletime Polymorphism which is Method Overloading and Operator Overloading.

2. Runtime Polymorphism which Method Overriding.

Method Overloading:

The method overloading is, to define several methods with the same name. Its also know as early binding.

Lets Implement Method Overloading :

public class MethodOverload
{
    public void Add(int A, int B)
    {
        Console.WriteLine("Value of int A + B = " + (A + B));
    }
    public void Add(string A, string B)
    {
        Console.WriteLine("Value of string A + B = " + A + B);
    }
    public void Add(double A, double B)
    {
        Console.WriteLine("Value of double A + B = " + (A + B));
    }
    private static void Main()
    {
        MethodOverload MO = new MethodOverload();
        MO.Add(1, 1);
        MO.Add("Pro", "gram");
        MO.Add(1.1, 1.2);
        Console.ReadKey();
    }
}

OUTPUT :
Value of int A + B = 2
Value of string A + B = Program
Value of double A + B = 2.3

Operator Overloading & Method Overriding :

public class OperatorOverloading
{
    static void Main()
    {
        OperatorOverloading obj1 = new OperatorOverloading(10);
        OperatorOverloading obj2 = new OperatorOverloading(20);
        OperatorOverloading obj3 = obj1 + obj2; // Executes operator +
        Console.WriteLine(obj3);
        Console.WriteLine(obj3 + 25); // Executes operator +
        Console.WriteLine(obj3 + 1.5); // Executes operator +
        Console.ReadKey();
    }

    private double Pos;

    public OperatorOverloading(int obj) // Constructor
    {
        Pos = (double)obj;
    }

    public OperatorOverloading(double obj)
    {
        Pos = obj;
    }

    //override ToString() method.
    public override string ToString()
    {
        return this.Pos.ToString();
    }

    public static OperatorOverloading operator +(OperatorOverloading x, OperatorOverloading y)
    {
        return new OperatorOverloading(x.Pos + y.Pos);
    }

    public static OperatorOverloading operator +(OperatorOverloading x, double y)
    {
        return new OperatorOverloading(x.Pos + y);
    }

    public static OperatorOverloading operator +(OperatorOverloading x, int y)
    {
        return x + (double)y;
    }
}
Like(0)Dislike(0)

Share and Enjoy

prabaharancs@gmail.com'

SurFolks

SurFolks, the one stop destination to cater all your wants and needs.

Leave a Reply