6. Object Oriented Program II(OOPs)

Inheritance

Inheritance is used for code resuability. Inheritance is a mechanism to create a new class from an existing one.

        The general format of inheritance in C# 
        class Base
        { 
        }
        class Derived : Base
        {
        }
       

Inheritance example code
        //Example Listing 6.1
        using System;
        namespace QSSTraining
        {
            public class BaseClass
            {
                public void TestMethod()
                {
                    Console.WriteLine("base class test method");
                }
            }
            public class DerivedClass : BaseClass
            {
                public void TestMethod()
                {
                    Console.WriteLine("derived class test method");
                    base.TestMethod(); //invokes base class test method
                }
            }
            public class Program
            {
                private static void Main(string[] args)
                {
                    var derivedObject = new DerivedClass();
                    derivedObject.TestMethod();
                }
            }
        }
        

Access Modifiers

C# provides the following types of Access Modifiers.
public : The public modifier can be accessed by any other code with in the same assembly or another assembly that references it.

private: The private modifier can only be accessed by code in the same class or struct.

protected: The protected modifier can only be accessed by code in the same class or struct, or in a derived class.

internal: The internal modifier can be accessed by any code in the same assembly, but not accessible from another assembly.

Access Modifiers Example Code:
        //Example Listing 6.2
        // Note: Derived class can not access Baseclass private members.
        using System;
        namespace QSSTraining
        {
            public class BaseClass
            {
                private int i = 5;
                protected int j = 10;
                public int k = 20;
                internal int l = 30;
            }
            public class DerivedClass : BaseClass
            {
                public void Test()
                {
                    Console.WriteLine(i); //Error: i is inaccessible due to protection level
                    Console.WriteLine(j);
                    Console.WriteLine(k);
                    Console.WriteLine(l);
                }
            }
            public class Program
            {
                private static void Main(string[] args)
                {
                    var derivedObject = new DerivedClass();
                    derivedObject.Test();
                }
            }
        }
        

Polymorphism

Polymorphism is one of the primary concept of object-oriented programming. Polymorphisms means the ability to request that the same operations be performed by a wide range of different types of things.

C# .NET provides two types of Polymorphisms and they are
1. Static Polymorphism and 2. Dynamic Polymorphism.

1. Static Polymorphism is also knwon as Compile time Polymorphim or Early Binding. Method overloading is an example oof Compile time Polymorphism..


Static Poloymorphism (Method Overloading) example code
        //Example Listing 6.3
        using System;
        namespace QSSTraining
        {
            public class StaticPolymosphirm
            {
                public void Add(int x, int y)
                {
                    int sum = x + y;
                    Console.WriteLine("Sum = " + sum);
                }
                public void Add(int x, int y, int z)
                {
                    int sum = x + y + z;
                    Console.WriteLine("Sum = " + sum);
                }
            }
            public class Program
            {
                private static void Main(string[] args)
                {
                    var obj = new StaticPolymosphirm();
                    obj.Add(10, 20);
                    obj.Add(10, 20, 30);
                }
            }
        }
        

2. Run time Polymorphism: is also knwon as Run time Polymorphim or Late Binding. Method overriding is an example of Run time Polymorphism.

virtual and override method

Virtual Method: When an instance method declaration includes a virtual modifier, that method is said to be a virtual method.

Oerride Method: When an instance method declaration includes an override modifier, the method is said to be an override method. An override method overrides an inherited virtual method with the same signature.


Dynamic Poloymorphism (Method override) example code
        //Example Listing 6.4
        using System;
        namespace QSSTraining
        {
            class BaseClass
            {
                public virtual void Display()
                {
                    System.Console.WriteLine("Invoking BaseClass::Display Method");
                }
            }
            class DerivedClass : BaseClass
            {
                public override void Display()
                {
                    System.Console.WriteLine("Invoking DerivedClass::Display Method");
                }
            }
            public class Program
            {
                private static void Main(string[] args)
                {
                    var b = new BaseClass();
                    b.Display();
                    b = new DerivedClass();
                    b.Display();
                    Console.ReadLine();
                }
            }
        }
        


Runtime Polymorphism: virtual and override example code.

If derived class method is not overridden, then base class method is invoked.In the following example code, the Baseclass DisplayMethod2 is invokved.

        //Example Listing 6.5
        //Runtime Polymorphism: virtual and override example code 
        //Note: using System;
        namespace QSSTraining
        {
            public class BaseClass
            {
                public virtual void DisplayMethod1()
                {
                    Console.WriteLine("Base Class DisplayMethod1");
                }
                public virtual void DisplayMethod2()
                {
                    Console.WriteLine("Base Class DisplayMethod2");
                }
            }
            public class DerivedClass : BaseClass
            {
                public override void DisplayMethod1()
                {
                    Console.WriteLine("Derived Class DisplayMethod1");
                }
            }
            public class Program
            {
                private static void Main(string[] args)
                {
                    var derivedObject = new DerivedClass();
                    derivedObject.DisplayMethod1();
                    derivedObject.DisplayMethod2();
                }
            }
        }
                //output result displays: 
        //Derived class DisplayMethod1 and Base Class DisplayMethod2
        


AnotherExample code for Runtime Poloymorphism using an array of objects
        //Example Listing 6.6
        //Runtime Polymorphism example code using an array of objects
        using System;
        namespace QSSTraining
        {
            public class BaseClass
            {
                public virtual void Print(string message)
                {
                    Console.WriteLine("Printing from Base Class");
                    Console.WriteLine("Message is {0}", message);
                }
            }
            public class DerivedClass : BaseClass
            {
                public override void Print(string message)
                {
                    Console.WriteLine("Printing from Derived Class");
                    Console.WriteLine("Message is {0}", message);
                }
            }
            public class Program
            {
                private static void Main(string[] args)
                {
                    var obj = new BaseClass[2];
                    obj[0] = new BaseClass();
                    obj[1] = new DerivedClass();
                    for (int i = 0; i < obj.Length; i++)
                    {
                        obj[i].Print("Notiication Message");
                    }
                }
            }
        }
        

Abstract Classes

An abstract class means that, the class can not be be instantiated, but can make derivations of this.

An abstract class can contain abstract methods and/or non abstract methods.

        abstract class BaseClass
        {
           public abstract void MethodA();
        }
        abstract class DerviedClass1: BaseClass
        {
           public void MethodB() {}
        }
        class DerivedClass2: DerivedClass1
        {
           public override void MethodB() {
              // implementation of MethodB
           }
        }
        

Sealed Class

When a class declared as a sealed modifier, the class is said to be a sealed class and is used to prevent inherit from a class. A sealed class cannot be an abstract class

        //Example Listing 6.7
        // Sealed Class example code 
        using System;
        namespace QSSTraining
        {
            public sealed class SealedClass
            {
                public void Display()
                {
                    Console.WriteLine("Invoking sealed class Display method");
                }
            }
            public class Derived : SealedClass {  }  //Error
    
            public class Program
            {
                private static void Main(string[] args)
                {
                     var oSealedClass = new SealedClass();
                    oSealedClass.Display();
                }
            }
        }