OOPs Concepts

What is Abstract class?

--------------------------------------------------------------
The abstract keyword enables you to create classes and class members solely for the purpose of inheritance—to define features of derived, non-abstract classes

A Sealed class

--------------------------------------------------------------
It  cannot be used as an abstract class as it cannot be used as a base class .A class member,method field,property or event on a derived class that is overriding
a virtual member of the base class can declare that member as sealed class.This will prevent that method from being overriden in further derivation. This can be accomblished bu putting  the sealed Keyword before the overridde keyword in the class member declaration.


Polymorphism :

--------------------------------------------------------------
C# recognises the method by its parameters and not only by its name.The return type of a method is never the part of the method signature if the names of the method are same . So not polymorphism. For polymorphism the methods must differ on the number/type of arguments.Even modifier like 'Static' are also not considered to be apart of method signature.

A method can take four different types of parameters
1)pass by value
2)pass by reference
3)As an output parameter
4)Using parameter Array

A method declaration means creating a separate declaration space in memory . So anything created will be lost at the end of the method


Ref key word and method signature

--------------------------------------------------------------
In the below program as you can see ,The variable 'name' is passed as ref to a function Display2. It can have multiple ref of same variable as long as ot satisfies the signature of the method.The line "x=Amit1 / y=Amit 2" directly lets us change the value of the variable "name" from the inside the function which is possible
only because refernce is used here.Both the parameter reference refers the same location in the memory.

using System;
class overload
{
        private string name="Amit";
        public void  Display()
        {
        Display2(ref name,ref name);
        Console.WriteLine(name);
        }

        public void Display2(ref string x, ref string y)
        {
        Console.WriteLine(name);
        x="Amit 1";
        Console.WriteLine(name);
        y="Amit 2";
        Console.WriteLine(name);
        name="Amit 3";
        }
}

class prog
{
        public static void Main()
        {
        overload ovd=new overload();
        ovd.Display();
        Console.ReadLine();
        }
}

Role of Param Parameters in C# oop

------------------------------------------------------------------
The params keyword can be applied only to the last argument in the method
using System;
class overload
{
        public void display(int amt,params string[] names)
        {
        foreach(string str in names)
        {
        Console.WriteLine(str+':'+amt);
        }
        }
}

class prog
{
        public static void Main()
        {
        overload a =new overload();
        a.display(300,"aaa","bbb","ccc","dddd");
        a.display(200,"xxx");
        Console.ReadLine();
        }
}


the ablove example summerises the usage of param keyword. C# is smart enough to identify by signature which element belongs to params array and which will
belong to inidividual argument. C# in takes only one dimensional array.
If we have a method signature like below

private void DisplayOverload(int a, params int[] parameterArray) {  }

and the method is called

        DisplayOverload(100, 200, 300);-->step1
        DisplayOverload(200, 100);     -->step2
        DisplayOverload(200);           -->step3    

then C# compiler will automatically assign 100 to int a , and rest of the argument(200,300) will be automatically taken up by parameterArray in step 1

Comments

Popular posts from this blog

Authentication and Authorization in Web API -Part1

My Gardening Journey 6