OOPS in C# – Interface polymorphism Part 5

Scenario:-
Different implementations for both interface and class.

    public interface MyInterface1
    {
        int foo();
    }

    public interface MyInterface2
    {
        int foo();
    }
    public class MyClass : MyInterface1, MyInterface2
    {
        int a = 10;
        #region MyClass Members

        public int foo()
        {
            return a;
        }

        #endregion

        #region MyInterface1 Members

        int MyInterface1.foo()
        {
            return 11;
        }

        #endregion

        #region MyInterface2 Members

        int MyInterface2.foo()
        {
            return 12;
        }

        #endregion
    }



In this case

            MyClass obj = new MyClass();
            MyInterface1 obj2 = (MyInterface1)obj;
            MyInterface2 obj3 = (MyInterface2)obj;
            obj.foo(); //Returns 10
            obj2.foo(); //Returns 11
            obj3.foo(); //Returns 12

OOPS in C# – Class vs Struct

Class v/s Struct
1.) Class can have default constructor. Struct can only have parametrized constructors.
2.) Class is a reference type whereas Struct is a value type.
3.) Struct can’t inherit or implement whereas class can both implement interfaces and inherit interfaces.

We will start by simple interface implementation. Let’s create an interface with one method.

    public interface MyInterface
    {
        int foo();

    }

Now let’s first have a look at a simple class

    public class MyClass
    {
        int a;
    }



Continue reading

OOPS in C# – Interface polymorphism Part 4

Scenario:-
Interface polymorphism with multiple interfaces having same method.

    public interface MyInterface1
    {
        int foo();
    }

    public interface MyInterface2
    {
        int foo();
    }
    public class MyClass : MyInterface1, MyInterface2
    {
        int a = 10;

        #region MyInterface1 Members

        public int foo()
        {
            return a;
        }

        #endregion
    }
           MyClass obj = new MyClass();
            MyInterface1 obj2 = (MyInterface1)obj;
            MyInterface2 obj3 = (MyInterface2)obj;
            obj.foo(); //Returns 10 
            obj2.foo(); //Returns 10
            obj3.foo(); //Returns 10

OOPS in C# – Interface polymorphism Part 3

Scenario :-
Lets now discuss explicit implementation of interface be taking below example. In explicit implementation functions are qualifies with parent interface names as below:-

    public interface MyInterface
    {
        int foo();
    }

    public class MyClass : MyInterface
    {
        int a = 10;

        #region myinterfacse Members

        int MyInterface.foo()
        {
            throw new NotImplementedException();
        }

        #endregion
    }

Now there will be some errors in code of scenario 10 as below

   MyClass obj = new MyClass();
            MyInterface obj2 = (MyInterface)obj;
            obj.foo(); //Compile time error.No class member foo
            obj2.foo(); //Returns 10

OOPS in C# – Interface polymorphism Part 2

Scenario :-

Private Implementation of Interface member.
Remember OOPS is a bit tricky and if you write MyClass definition as below then

 

    public interface MyInterface1
    {
        int foo();
    }
    public class MyClass : myinterfacse
    {
     int a=10;
     int foo()
      {
       return a;
      }
    }

It will give a compile time error.
Error 1 ‘WindowsFormsApplication3.Program.MyClass’ does not implement interface member ‘WindowsFormsApplication3.Program.myinterfacse.foo()’. ‘WindowsFormsApplication3.Program.MyClass.foo()’ cannot implement an interface member because it is not public.

OOPS in C# – Interface polymorphism Part 1

Scenario 10:-
First we will discuss implicit implementation of interface.

    public interface MyInterface
    {
        int foo();
    }

    public class MyClass : MyInterface
    {
        private int a = 10;

        public int foo()
        {
            return a;
        }
    }
    MyClass obj = new MyClass();
    MyInterface obj2 = (MyInterface) obj;
    obj.foo(); //Returns 10
    obj2.foo(); //Returns 10

Here foo is acting both as a class member as well as interface member.

OOPS in C# – Struct

Struct in .Net:-

1.) Struct are value types.
2.) Struct in .Net are sealed i.e. they cannot be inherited.
3.) The members of a Struct are by default private and not public.
4.) Struct can have Main method also just like a class.
5.) Struct can even have a constructor but Struct can only have parametrized constructors. In early days Struct had default constructors but they were re-moved due to some reasons known only to creators ? Continue reading

OOPS In C# – Interface

Interfaces:-
In layman’s terms we can say that interface is a pure abstract class with all methods abstract and no data members.
Interview definition:-
Interface is a contract between two parties. The contract is that all the parties implementing interfaces must implement all the methods declared in interfaces.
Abstract class V/s Interfaces:-


1.) Abstract class can contain data members. Interfaces can’t.
2.) Abstract class can contain non abstract methods too. Interfaces can’t.
3.) Default access specifier is abstract class is private but we can have other access specifiers also. Interfaces methods are by default public; you can’t apply any access specifier; not even public.
4.) In interfaces members are by default abstract you can’t even specify abstract keyword.
5.) Abstract class can implement interfaces. Interfaces can’t inherit classes but can implement other interfaces also.
6.) You can inherit from a single base class but you can implement multiple interfaces.
7.) Abstract classes are created using abstract keyword. Interfaces are created using interface keyword. Continue reading

OOPS In C# – Inheritance Part 3

Scenario:-
Function present in both base class and child class. This concept is called Method hiding. See the sample code below:

    public class MyClass
    {
        public void foo()
        {
            Console.WriteLine("Base version of foo");
        }
    }

    public class MyChild : MyClass
    {
        public new void foo()
        {
            Console.WriteLine("Child version of foo");
        }
    }



Continue reading

OOPS In C# – Inheritance Part 4

Scenario:-
Function present in both base class and child class but this time virtual in base class. See the sample code below:

    public class MyClass
    {
        public virtual void foo()
        {
            Console.WriteLine("Base version of foo");
        }
    }

    public class MyChild : MyClass
    {
        public new void foo()
        {
            Console.WriteLine("Child version of foo");
        }
    }





Continue reading

OOPS in C# – Polymorphism Part 1

Polymorphism means one thing exists in many forms of its own with each form having different implementation of its own.
Polymorphism can be compile time or run-time.

Compile time polymorphism includes:-
1.) Function Overloading
2.) Operator Overloading

Function Overloading:-
   Function overloading depends on:-

1.) Variable Number of parameters to function
2.) Order of parameters to function
3.) Data type of parameters
Function overloading doesn’t depend on:-
1.) Return type of function
2.) Name of parameters Continue reading

OOPS in C# – Polymorphism Part 3

6.) Overloading in case of inheritance

public class MyBaseClass
    {
        public void foo(int i)
        {
            Console.WriteLine("The is one parameter function which takes 
int as parameter and value of i is {0}."
, i); } } public class MyChildClass : MyBaseClass { public void foo(string s) { Console.WriteLine("The is one parameter function which takes
string as parameter and value of s is {0}."
, s); } }

Continue reading

OOPS In C# – Inheritance Part 6

Scenario:-
Function declared as virtual in base class and overridden in child class. See the sample code below:

  public class MyClass
    {
        public virtual void foo()
        {
            Console.WriteLine("Base version of foo");
        }
    }

    public class MyChild : MyClass
    {
        public override void foo()
        {
            Console.WriteLine("Child version of foo");
        }

    }



Continue reading

OOPS In C# – Inheritance Part 1

Inheritance:-

Inheritance basically provides two models. Reusability model and extensibility model. Reusability model was there in C also through header file and in VB since Visual Basic is an object based language. An object based language is the one where in we can use the objects or components or modules as is but can’t modify or extend their behavior.
Interview Definition: – Extending or enhancing the functionality of base class.
In inheritance few terms or concepts that needs mentioning are hiding and overriding.
Method or Data Hiding: – Having data member or function in child class having same signature as that in base class. This is not an error but compiler shows you a warning messages when you have same data member in both child and base stating that you are hiding the base class implementation if hiding is intended use new keyword. So by specifying new keyword with data member and functions you can hide base class’s implementation of data or function. This will be clearer with an example later on. Continue reading

OOPS In C# – Encapsulation – Abstraction

Introduction


OOPS or Object Oriented Programming Concepts but some of the concepts have been always used in one or the other programming languages. For example you must have used structs in C which is a good example of encapsulation. There are four major pillar of OOPS. Let’s try to understand each one of them by taking some examples also:-
1.) Encapsulation
2.) Abstraction
3.) Polymorphism
4.) Inheritance

Encapsulation:-


Interview Definition: – Binding data and member functions together inside a single unit.
How to Encapsulate:- By creating types e.g. Classes and Struct
Bu using encapsulation we can create our own custom types by reusing the existing or primitive types.
Continue reading