Wednesday, 4 April 2012

OOPS Concept -3 (Overloading and Overriding)

Overloading ---------->




using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestOOPSConcept
{
    /// <summary>
    /// In Same class, u define funtion with same name,parameter then amniguity error comes.
    /// </summary>
    class TestOverloading
    {

        static void Foo(int x)
        {
            Console.WriteLine("Foo(int x)");
        }

        static void Foo(string y)
        {
            Console.WriteLine("Foo(string y)");
        }

        static void Foo(double z)
        {
            Console.WriteLine("Foo(double y)");
        }

        static void Foo(int x, int y = 5)
        {
            Console.WriteLine("Foo(int x, int y = 5)");
        }

   
        //static void Main()
        //{

        //    Foo(10, 5);
        //    Console.Read();
        //}

    }
}






Overriding ------------------->



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestOOPSConcept
{
    /// <summary>
    ///  /// overriding and Hiding
    /// The virtual keyword is used to modify a method, property, indexer or event declaration, and allow it to be overridden in a derived class.
    /// virtual or abstract members cannot be private
    /// You cannot use the virtual modifier with the static, abstract, private or override modifiers.
    /// </summary>
    class A
    {
        public void Foo()
        {
            Console.WriteLine("A::Foo()");
        }
    }

    class B : A
    {
        public new void Foo()
        {
            base.Foo();
            Console.WriteLine("B::Foo()");
        }
    }

    class Test
    {
        //static void Main(string[] args)
        //{
        //    A a;
        //    B b;

        //    a = new A();
        //    b = new B();
        //    a.Foo();  // output --> "A::Foo()"
        //    b.Foo();  // output --> "B::Foo()"

        //    a = new B();
        //    a.Foo();  // output --> "A::Foo()"
        //    Console.Read();
        //}
    }
}

OOPS Concept -2 (All about Constructor)

Constructor  ------------------------>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestOOPSConcept
{
    /// <summary>
    /// Private Constructor ***************
    /// It is generally used in classes that contain static members only
    /// If u don't declare the modifier with constructor, By Default costructor modifier are "private".
    /// If class contains only private constructor, its clear that the class can not be instantiated.
    /// If all the methods in the class are static, consider making the complete class static.
    /// </summary>

    public class Counter
    {
        private Counter() {
        }
        public static int currentCount;
        public static int IncrementCount()
        {
            return ++currentCount;
        }
     
    }
 
    class TestCounter
    {
        //static void Main()
        //{
        //    // If you uncomment the following statement, it will generate
        //    // an error because the constructor is inaccessible:
        //    // Counter aCounter = new Counter();   // Error    
       
        //    Counter.currentCount = 100;
        //    Counter.IncrementCount();
        //    Console.WriteLine("New count: {0}", Counter.currentCount);

        //    // Keep the console window open in debug mode.
        //    Console.WriteLine("Press any key to exit.");
        //    Console.ReadKey();
        //}
    }
}




OOPS Concept -1(Abstract and Interface)


Abstract Class - >>>>>>>>>>>>>>>


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestOOPSConcept
{
    /// <summary>
    /// The purpose of an abstract class is to provide a common definition of a base class that multiple derived classes can share.
    /// we can call the non abstract method from inherited class using base keyword.
    /// if i don't declare any virtual or abstract with function, then inherited class shows warning with that function, then we can use New keyword for hiding
    /// We can't create the instance of abstract class or interface.
    /// we must implement abstract method in derived class.
    /// Abstract class may contain static method.
    /// </summary>
    abstract class ClassA
    {
        public int a = 100, b = 10, totalsum, totalsubtract, totalmultiply;
        public abstract int Sum();   // Abstract methods have no implementations  
        public  virtual int Subtract()
        {
            return totalsubtract = a - b;
        }
        public abstract int Multiply();
        public static void Test()
        {
            Console.WriteLine("Abstract class may contain static method");
        }
    }

    abstract class ClassB : ClassA
    {
        public override int Sum()
        {
            return totalsum = a + b;

        }

        public override int Subtract()
        {
         
            totalsubtract = base.Subtract();
            return totalsubtract - 40;
        }
     
   
    }

    class MyClass : ClassB
    {
        public override int Sum()
        {
            return totalsum = a + b + 50;
        }

        public override int Multiply()
        {
            return totalmultiply = a * b +80;
        }

    }

    class MainClass
    {
        //static void Main(string[] args)
        //{
        //    MyClass obj = new MyClass();
        //    int x = obj.Sum();
        //    Console.WriteLine("Sum: " + x);
        //    int y = obj.Subtract();
        //    Console.WriteLine("Sub: " + y);
        //    int z = obj.Multiply();
        //    Console.WriteLine("Multiply: " + z);
        //    ClassB.Test();
        //    Console.Read();

       // }
    }
}




Interface ------------------------------


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TestOOPSConcept
{
    /// <summary>
    /// If you want your child classes to implement multiple unrelated functionalities in short multiple inheritance use interfaces.
    ///  if u declare public modifier with method in interface, then error comes: by default interface members are public.
    ///   We can't create the instance of abstract class or interface.
    ///   No need to override the interface methods.
    ///
    ///
    /// 1. ExampleClass ex = new ExampleClass(); - When we are instantiating the class by specifying the object as the class name,
    /// we will be able to call methods for the interface that is implicitely implemented. In this case IExampleClass2.
    ///
    ///
    /// 2. IExampleClass1 ex1 = new ExampleClass(); - If we want to call the method that is explicitely implemented,
    /// we need to specify the object as the interface name and instantiate the class then we will be able to call the method of this interface. In this case IExampleClass1.
    ///
    ///
    ///3. IExampleClass2 ex2 = new ExampleClass(); - You can call methods of the interface
    /// that is implicitely implemented by specifying the name of the Interface as the object name (The way we have done in the 2nd point).
    /// This will give the same result as the 1st point above.
    /// </summary>
    interface interfaceA
    {
        void Sum();
        void Subtract();
    }
    interface interfaceB
    {
        void Sum();
        void Sum(string abc);
    }
    interface interfaceC : interfaceA, interfaceB
    {
        void Multiply();

    }
    class interfaceClass : interfaceC
    {
     
        public void Sum()
        {
            Console.WriteLine("Implement sum1 function.");
        }
        public void Subtract()
        {
            Console.WriteLine("Implement Subtract function.");
        }
        public void Sum(string abc)
        {
            Console.WriteLine("Implement sum2 function.");
            //return abc;
        }
        void interfaceB.Sum() // Explicitally call
        {
            Console.WriteLine("Interface B call sum method");
        }
        public void Multiply()
        {
            Console.WriteLine("Implement multiply function.");
        }
    }

    class MaininterfaceClass
    {
        //static void Main(string[] args)
        //{
            //interfaceClass objinterfaceClass = new interfaceClass();
            //objinterfaceClass.Sum();
            //objinterfaceClass.Sum("123");
            //objinterfaceClass.Subtract();
            //objinterfaceClass.Multiply();
            //interfaceB objinterfaceB = new interfaceClass();
            //objinterfaceB.Sum();
            //Console.Read();

      //  }
    }




}