OOP's

Object Oriented Programming Features

1. Encapsulation

All managed languages in the .NET Framework, such as Visual Basic and C#, provide full support for object-oriented programming including encapsulation, inheritance, and polymorphism.

Encapsulation means that a group of related properties, methods, and other members are treated as a single unit or object.
or
Encapsulation is the process of hiding the irrelevant information and showing only the relevant information of a specific object to a user. The process of hiding the internal facts is known as Encapsulation.
or
Encapsulation is way to hide data, properties and methods from outside the world or outside of the class scope and exposing only necessary thing.

  • Encapsulation complements Abstraction.
  • Abstraction display only important features of a class and
  • Encapsulation hides unwanted data or private data from outside of a class.It hides the information within the object and prevents from accidental corruption.

or
So encapsulation means hiding the important features of a class which is not been needed to be exposed outside of a class and exposing only necessary things of a class.
or
encapsulation is the process of wrapping up of data and members of a class.
  • it is an approach of hiding the internal state and behavior of a class or an object from unauthorized access.,
  • encapsulation restricts user from sharing and manipulating data; there by minimizing the chances of data corruption.
  • Encapsulation and abstraction is the advanced mechanism in C# that lets your program to hide unwanted code within a capsule and shows only essential features of an object.
  • Encapsulation is used to hide its members from outside class or interface,
  • whereas abstraction is used to show only essential features.


Encapsulation uses five types of modifier to encapsulate data.

These modifiers are:

  1. Public
  2. Private,
  3. Internal 
  4. Protected 
  5. Protected internal


  • An access specifier defines the scope and visibility of a class member.



  • Abstraction is just opposite of Encapsulation. 
  • Abstraction is mechanism to show only relevant data to the user.
  • Abstraction allows making relevant information visible and encapsulation enables a programmer to implement the desired level of abstraction.



Advantages


  • A way to protect our data from unauthorized access. using private access specifier
  • Increases the maintainability of code by showing the relevant information to user.
  • Prevents data corruption by enabling private member variables
  • Binds member variables and functions of a class into a single unit. This is called wrapping up of data members and member functions in a class.

Encapsulation Using Accessors and Mutators

To save our precious data from unauthorized access and corruption, declare it with the private keyword.

using System;

namespace Employee
{
    public class EmployeeDetail
    {
        private string Name;
        // Accessor
        public string Get() { return Name; }

        // Mutator
        public void Set(string name) => Name = name;
    }

    public class MainClass
    {
        static void Main(string[] args)
        {
            EmployeeDetail detail = new EmployeeDetail();
            detail.Set("Martin");
            Console.WriteLine("The Employee Name is :" + detail.Get());
            Console.Write("\nPress ENTER to quit...");
            Console.ReadLine();
        }
    }
}

Encasulation Using Properties
you ca also implement encasulation by using the user - defined properties.
these properties contain a pair of get and set methods.

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

namespace Student
{
    public class StudentDetail
    {
        private string Name;
        public string StudentName
        {
            get
            {
                return Name;
            }
            set
            {
                Name = value;
            }
        }
    }
    public class MainClass
    {
        public static void Main(string[] args)
        {
            StudentDetail detail = new StudentDetail();
            detail.StudentName = "Martin";
            Console.WriteLine("The Student Name is :{0}", detail.StudentName);
            Console.Write("\nPress ENTER to quit...");
            Console.ReadLine();
        }
    }
}




Inheritance
Inheritence
the most importent reason to use OOP is to promote reusability of code and eliminate the redundant code.
to reduce the redundancy
a class that inherites the properties of another class is called a child or derived class
the class from which the child class inherites properties is known as a parant or base class
4 types of inheritence
1.single inheritence
only one base class and one derived class.
derived class inherites properties from single base class.
2.hierarchical inheritence
multiple derived classes are inherited from the same base class
3.multilevel inheritence
child class derived from a class which in turn is derived from another class
4.multiple inheritence
a child class derived from multiple derived classes.

c# supports single,hierarichal, and multilevel inheritence because there is only a single base classes.
it does not supports multiple inheritence directly

to implement multiple inheritence, you need to use interfaces.
interface: is a collection of datamembers and member functions, but it only contains the declaration of data members and member functions.

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

namespace MyInheritance
{
    public class BaseClass
    {
        public int DataMember;
       public BaseClassMethod()
        {
            Console.WriteLine("I am a BaseClassMethod()");
        }

    }
    public class DerivedClass : BaseClass
    {
        public void DerivedClassMethod()
        {
            Console.WriteLine("I am a DerivedClassMethod()");
        }
    }
    public class Program
    {
        public static void Main(string[] args)
        {
            //ceate a base class object
            Console.WriteLine(" I am accessing base class object");
            BaseClass bc = new BaseClass();
            bc.DataMember=1;
            bc.BaseClassMethod();
            //create a derived class object
            DerivedClass dc = new DerivedClass();
            dc.DataMember=2;
            dc.BaseClassMethod();
            dc.DerivedClassMethod();
            Console.Write("\nPress ENTER to quit..");
            Console.ReadLine();
        }
    }
}

Inheritence and constructors
constructor is used to initializwe the members of the same class
a constructor is called by defult whenever the object of a class is created.
it is important to note that aderived class cannot inherite the constructor of the base class. all the derived classes has thire defult constructor.
when you instantiate a derived class, the constructor of the base class is not avilable to it,
however the defult constructor of the base class is called automatically and then the derived class constructor is called.
the constructor of the base class initializes the members of the base class, before the derived class constructor is executed.

class BaseClass
{
public BaseClass()
{
Console.WriteLine("Base Class Construtor");
}
}
class SubClass1:BaseClass
{
public SubClass1()
{
Console.WriteLine("Sub Class Constructor");
}
}
class Subclass2:SubClass1
{
public SSubClass2()
{
Console.WriteLine("Sub Class 2 Constructor");
}
}
when the Subclass2 class is instantiated, first the defult constructor of the BaseClass is called then the constructor of the subclass1 is called, and at the last the constor of the subclass2 is called.

Sealed Classes and Sealed Methods
sealed classes are the clasess that cannot be inherited.
use sealed keyword to define a class is a sealed class

sealed class <class name>
{
//Data members and member function
}

sealed methods are also defined using the sealed keyword. a derived class cannot override the sealed methods;
you can use a sealed method to override an inherited virtual method with the same signature.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MySealedClass
{
    sealed class MyClass
    {
        string fName;
        string lName;
        int Age;
        public void GetDetail()
        {
            Console.Write("Enter Your First Name:");
            fName = Console.ReadLine();
            Console.Write("Enter Your Last Name:");
            lName = Console.ReadLine();
            Console.Write("Enter Your Age:");
            Age = int.Parse(Console.ReadLine());
        }
        public void ShowDetail()
        {
            Console.WriteLine(fName + " " + lName + ", you are " + Age + " Years Old");
        }
    }
    // Following lines shows compiler error as Sealed class cannot be inherited
    /* class Derived : MyClass
     {
     }*/
    class MainClass
    {
        static void Main(string[] args)
        {
            MyClass obj = new MyClass();
            obj.GetDetail();
            obj.ShowDetail();
            Console.Write("\nPress ENTER to quit...");
            Console.ReadLine();
        }
    }
}

Extension Methods
Extension method is a method that helps you to extend a class without creating a new derived class or modifying the original class.
it works as a stitic method, but is invoked with an instance of the extended class
the extension method can be any static method which uses the this keyword before it's first parameter.

only a class that has static members and implements the this keyword in the first parameter of a static method can use the extension methods.

static class MyClass
{
public static int MyExtensionMethod(this string number)
{
//method body
}
public static int MyStaticMethod(string number)
{
//method bidy
}
}
class MainClass
{
string number = "100";
int ext = Number.MyExtensionMethod();
int stat = Myclass.MyStaticMethod();
}

Extension methood contains the this keyword before the first argument;
Static methods do not contain this keyword in thir argument declaration.

when an extension method is called, the argument declared with this keyword is not passed
while calling a static method, you must pass all the arguments

Extension methods are declared in a static class only,
but it is not necessary that the static methods are declared in a static class only.

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

namespace MyExtensionMethod
{
    public static class MyClass
    {
        public static int MyExtensionMethod(this string Number)
        {
            return Int32.Parse(Number);
        }
        public static int MyStaticMethod(string Number)
        {
            return Int32.Parse(Number);
        }
    }
    class MainClass
    {
        static void Main(string[] args)
        {
            string Number = "100";
            int ext = Number.MyExtensionMethod(); // Line A
            Console.WriteLine("The Output from MyExtensionMethod()");
            Console.WriteLine(ext);
            int stat = MyClass.MyStaticMethod(Number); // Line B
            Console.WriteLine("");
            Console.WriteLine("The Output from MyStaticMethod()");
            Console.WriteLine(stat);
            Console.Write("\nPress ENTER to quit...");
            Console.ReadLine();
        }
    }
}





used to exhibit different forms of any perticular procedure
you can use one procedure in many ways as per your requirements.
use the same procedure with different parameters for each geometrical figure.

advantages
allows you to invoke methods of a derived class through base class reference during runtime
provides different implementations of methods in a class that are called through the same name.

object of the derived class can hold the reference of it's base class

class BaseClass
{
public void BaseMethod()
{
//Method body
}
}
class DerivedClass:BaseClass
{
public void DerivedClass()
{
//Method body
}
}

you can write

BaseClass bc = new DerivedClass();
we are taking the object of the derived class as an object of the base class
then we can write
bc.BaseClass();
bc.DerivedClass();  \\It shows error
although, we have created an object of the derived class , but we cannot access the members of the derived class
because the apparent class is base class not derived class

there are 2 types of polymorphism
1.static polymorphism/ compile time polymorphism/ overloading / early binding
2.dynamic polymorphism/ runtime polymorphism/ overriding / late binding

1.Compile time polymorphism/ overloading
the information about the method arguments and accordingly, it binds the appropriate method to an object at the compile time itself
you can implement compile time polymorphism through overloaded methods and operators.
overloaded methods are methods that have same name but different signature, such as number, type and sequence of parameters.
there are 3 types of compile time polymorphism

a. method overloading
b. operator overloading
c. indexer overloading

method overloading
methods behaves according to the number and type of parameters passed to it.
many methods with the same name but different signature
method overloading is used when methods are required to perform similar tasks but with different input parameters
note that only the return type does not play any importent role in the method overloading.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MethodOverload
{
    public class Shape
    {
        public void Area(int Side)
        {
            int SquareArea = Side * Side;
            Console.WriteLine("The Area of Square is: " + SquareArea);
        }
        public void Area(int Length, int Breadth)
        {
            int RectangleArea = Length * Breadth;
            Console.WriteLine("The Area of Rectangle is: " + RectangleArea);
        }
        public void Area(double Radius)
        {
            double CircleArea = 3.14 * Radius * Radius;
            Console.WriteLine("The Area of Circle is: " + CircleArea);
        }
        public double Area(double Base, double Height)
        {
            double TriangleArea = (Base * Height) / 2;
            Console.WriteLine("The Area of Triangle is: " + TriangleArea);
            return TriangleArea;
        }
    }
    class MainClass
    {
        static void Main(string[] args)
        {
            Shape shape = new Shape();
            shape.Area(15);
            shape.Area(10, 20);
            shape.Area(10.5);
            shape.Area(15.5, 20.4);
            Console.Write("\nPress ENTER to quit...");
            Console.ReadLine();
        }
    }
}

b. Operator Overloading
in c# you can change the functionality of an operator by overloading them.
the mechanism of assigning a special meaning to an operator according to user defined data type, such as classes or structs is known as operator overloading..

+, -, !, ~, ++, --, true, false, unari can be overloaded
+, -, *, /, %, &, |, ^, <<, >> binary can be overloaded
= =, !=, <, >, <=, >= comparison can be overloaded

if you overload the comparision operators, they must be overloaded in pairs, such as if = = is overloaded then != must also be overloaded.
the reverse is also true, and the same rule applies to < and > , and for <= and >=.

when you overload an operator, you need to create a method that must be preeceded by the operator keyword.

overloading of a unary operator is known as unary operator overloading
overloadinng of a binary operator is known as binary operator overloading.

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

namespace UnaryOperatorOverload
{
    class UnaryOperator
    {
        private int Number1;
        private int Number2;
        public UnaryOperator()
        {
        }
        public UnaryOperator(int number1, int number2)
        {
            Number1 = number1;
            Number2 = number2;
        }
        public void ShowData()
        {
            Console.WriteLine("The Numbers are: " + Number1 + " and " + Number2);
        }
        public static UnaryOperator operator -(UnaryOperator opr)
        {
            UnaryOperator obj = new UnaryOperator();
            obj.Number1 = -opr.Number1;
            obj.Number2 = -opr.Number2;
            return obj;
        }
    }
    class MainClass
    {
        public static void Main()
        {
            UnaryOperator opr1 = new UnaryOperator(20, 30);
            Console.WriteLine("Before Operator Overloading");
            opr1.ShowData();
            UnaryOperator opr2 = new UnaryOperator();
            opr2 = -opr1;
            Console.WriteLine("---------------------------");
            Console.WriteLine("After Operator Overloading");
            opr2.ShowData();
            Console.Write("\nPress ENTER to quit...");
            Console.ReadLine();
        }
    }
}


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace BinaryOperatorOverload
{
    class BinaryOperator
    {
        private int Number1;
        private int Number2;
        public BinaryOperator()
        {
        }
        public BinaryOperator(int number1, int number2)
        {
            Number1 = number1;
            Number2 = number2;
        }
        public void ShowData()
        {
            Console.WriteLine("The Numbers are: " + Number1 + " and " + Number2);
        }
        public static BinaryOperator operator +(BinaryOperator opr1, BinaryOperator opr2)
        {
            BinaryOperator opr = new BinaryOperator();
            opr.Number1 = opr1.Number1 + opr2.Number1;
            opr.Number2 = opr1.Number2 + opr2.Number2;
            return opr;
        }
    }
    class MainClass
    {
        public static void Main()
        {
            BinaryOperator opr1 = new BinaryOperator(20, 30);
            opr1.ShowData(); // displays 20 & 30
            BinaryOperator opr2 = new BinaryOperator(30, 40);
            opr2.ShowData(); // displays 30 & 40
            BinaryOperator opr3 = new BinaryOperator();
            opr3 = opr1 + opr2;
            opr3.ShowData(); // dislplays 50 & 70
            Console.Write("\nPress ENTER to quit...");
            Console.ReadLine();
        }
    }
}

Indexer Overloading
an indexer is used to treat an object as an array.
it is used to provide index to an object to obtain values from an object.
Implementing an indexer requires you to use breakets([]) with an object to get and set a value of the object.
Indexers are declared as proparties, with thedefference that in case of indexers, you do not need to provide name to them.

you need to use the this keyword to define an indexer.
public string this[int position]
{
get { return MyData[Position];}
set { MyData[Position] = value; }
}
the get proparty is used to retrive the index of the MyData mamber,
the set property is used to assign an index to the member.
there is no name assign to the indexer.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace IndexerOverload
{
    class MyClass
    {
        private string[] MyData;
        private int ArraySize;
        public MyClass(int size)
        {
            ArraySize = size;
            MyData = new string[size];
            for (int i = 0; i < size; i++)
            {
                MyData[i] = "DataValue";
            }
        }
        public string this[int Position]
        {
            get
            {
                return MyData[Position];
            }
            set
            {
                MyData[Position] = value;
            }
        }
        public string this[string data]
        {
            get
            {
                int Count = 0;
                for (int i = 0; i < ArraySize; i++)
                {
                    if (MyData[i] == data)
                    {
                        Count = Count + 1;
                    }
                }
                return Count.ToString();
            }
            set
            {
                for (int i = 0; i < ArraySize; i++)
                {
                    if (MyData[i] == data)
                    {
                        MyData[i] = value;
                    }
                }
            }
        }
        class MainClass
        {
            static void Main(string[] args)
            {
                int size = 10;
                MyClass MyIndexer = new MyClass(size);
                MyIndexer[9] = "Hello";
                MyIndexer[3] = "Welcome";
                MyIndexer[5] = "Good Morning";
                MyIndexer[7] = "Good Night";
                MyIndexer["DataValue"] = "Have a Nice Day";
                Console.WriteLine("\nIndexer Output\n");
                Console.WriteLine("-----------------");
                for (int i = 0; i < size; i++)
                {
                    Console.WriteLine("MyIndexer[{0}]: {1}", i, MyIndexer[i]);
                }
                Console.WriteLine("\nNumber of \"Have a Nice Day\" Entries: {0}",
                MyIndexer["Have a Nice Day"]);
                Console.Write("\nPress ENTER to quit...");
                Console.ReadLine();
            }
        }
    }
}
Runtime Polymorphism/Overriding
overriding is a feature that allows a derived class to provide a specific implimentation of a method that is already defined in a base class.
the implementation of method in the derived class overrides or replaces the implementation of a method in it's base class.
when you call a method, the method defined in the derived class is invoked and executed insted of the one in the base class.

to invoke the method of a derived class that is already defined in the base class, you need to perform the following steps.
1. Declare the base class method as virtual
2. Implement the derived class method using the override keyword

Class BaseClass
{
public virtual void ShowData()
{
//Method Body
}
}
Class DerivedClass : BaseClass
{
public override void ShowData()
{
//Method Body
}
}

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

namespace MethodOverriding
{
    class Program
    {
        static void Main(string[] args)
        {
            Person p = new Person();
            p.setAge(18);
            AdultPerson ap = new AdultPerson();
            ap.setAge(18);
            Console.WriteLine("Person Age: {0}", p.getAge());
            Console.WriteLine("AdultPerson Age: {0}", ap.getAge());
            Console.Write("\nPress ENTER to quit...");
            Console.ReadLine();
        }
    }

    public class Person
    {
        private int fAge;
        public Person()
        {
            fAge = 21;
        }

        public virtual void setAge(int age)
        {
            fAge = age;
        }

        public virtual int getAge()
        {
            return fAge;
        }
    }

    public class AdultPerson : Person
    {
        public AdultPerson() { }
        override public void setAge(int age)
        {
            if (age > 21)
                base.setAge(age);
        }
    }
}

the new operator is used to override the base class method in the derived class without using virtual keyword.
the new operator tells the compiler that the derived class method hides the base class method

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

namespace MethodHiding
{
    class BaseClass
    {
        public void MyMethod()
        {
            Console.WriteLine("I am in Base Class");
        }
    }

    class DerivedClass : BaseClass
    {
        public new void MyMethod()
        {
            Console.WriteLine("I am in Derived Class");
        }
    }

    class MainClass
    {
        static void Main(string[] args)
        {
            BaseClass bc = new BaseClass();
            DerivedClass dc = new DerivedClass();
            bc.MyMethod();
            dc.MyMethod();
            bc = new DerivedClass();
            bc.MyMethod(); //BaseClass Method
            Console.Write("\nPress ENTER to quit...");
            Console.ReadLine();
        }
    }
}




Abstraction

Abstraction is the process of hiding the details of  a particular concept or object from a user and exposing only the essential features.

ex:
the process of showing the general information related to the refrigirator is abstraction and
hiding the complex information from the user is called encasulation

abstraction refers to the act of representing essential features without including the background details or explanations.
abstraction is the development of classes, objects, and types in terms of their functionality, instead of thire implimentation details.

abstracion simply denotes a mdel, a view, or a representation for an actual item(class, object, or type)

characterastics of abstraction
managing the complexity of the code
decomposing complex systems into smaller components

Abstract Classes
you can have a single base class and multiple derived classes.
if you do not want that any classs can create an object of the base class, you can make the base class as abstract.
the abstract keyword in a class indicates that the class cannot be instantiated.
you can create the instance of derived class ( only if it is marked as abstract)

abstract class Baseclass
{
//Data Members and Member functions
}
class DerivedClass : BaseClass
{
//Data Members and Member Functions
}
class MainClass()
{
...
...
BaseClass bc; //can't be instantiated
DerivedClass dc; //can be instantiated
}
characterestics of an abstract class

1. Restricts instantiation, implying that you cannot create an object of an abstract class
2. Allows you to define abstract as well as non-abstract members in it.
3. Requires at lest one abstract method in it.
4. Restricts the use of sealed keyword in it.
5. Posseses public access specifier; therefore, it can be used anywhere in a program.

Abstract Methods
abstract method is similar to a virtual method which does not provide any implementation;
therefore, an abstract method does not have method body.
it can be declared by using the abstract keyword.

public abstract void area(int Length, int Breadth)

characterestics of abstract methods
1. Restricts it's implementation in a abstract class
2. Allows implementation in a non-abstract derived class
3. Requires declaration in an abstract class only
4. Restricts declaration with static and virtual keywords
5. Allows you to override a virtual method

public abstrct class shape
{
....
....
public abstract void area(int Length, int Breadth)
{
//method area() cannot be implemented at this place
}
}
public abstract class BasicShape : Shape
{
//method area() cannot be implemented in this place also
}
public class Rectangle : BaseShape
{
public override void area(int Length, int Breadth)
{
//method area() can be implemented at this place
}
}





Interface
An interface is a collection of datamembers and member functions, but it does not implement them
interfaces are introduced to provide the feature of multiple inheritence to classes.
multiple inheritence is the feature of OOP which allows a class to inherit from multiple classes.
the methods defined in an interface do not have implementation, they only specify the parameters that they will take and the type of values they will return.
the interface is always implemented in a class
the class that implements interface need to implement all the members of the interface.

In C#, interfaces are more flexible in thire implementation, then any other language.
interface in c# is the equivalent of an abstract base class.
this implies that you cannot instantiate an object through an interface, but you can offer set of functionality to classes that implement the interface.
an interface can have the  same access modifire as a class, such as public and private,

syntax of interfaces
An interface is declared just as a class.
interface is declared usingthe interface keyword.
interfaces behave as templates that have method declarations in them.
these methods are abstract in nature, as they do not have code in their body.
a class that implements an interface must implement all of it's methods; otherwise an error occures

interface <interfaceName>
{
//Abstract method declarations in interface body
}

the interface definition itself contains no code, only the signature of the actual method.
the implementation of methods is left to the class that inherits the interface.

public interface Channel
{
void Next();
void Previous();
}
public interface Book
{
void Next();
void Chapter();
}
any inheriting class can implement the methods of the interfaces channel and book

Implementation of Interfaces
To inplement multiple inheritence in a program, you need to use interfaces.
implementing an interface is similar to inheriting a class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace AccessingInterface
{
    public interface Channel
    {
        void Next();
        void Previous();
    }
    public interface Book
    {
        void Next();
        void Chapter();
    }
    class Program : Channel, Book // Implementing Interface in a class
    {
        void Channel.Next()
        {
            Console.WriteLine("Channel Next");
        }
        void Book.Next()
        {
            Console.WriteLine("Book Next");
        }
        public void Previous()
        {
            Console.WriteLine("Previous");
        }
        public void Chapter()
        {
            Console.WriteLine("Chapter");
        }
        static void Main(string[] args)
        {
            Program app = new Program();
            ((Book)app).Next();
            app.Previous();
            app.Chapter();
            Console.Write("\nPress ENTER to quit...");
            Console.ReadLine();
        }
    }
}

Interfaces and Inheritence
when an interface is implemented by a base class, then the derived class of the base class automatically inherits method of the interface.
you can initialize an object of the interface by type casting the object of the derived class with the interface itself
an interface can be inherited by a class or by another interface.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace InterfaceInheritance
{
    interface BaseInterface
    {
        void GetPersonalDetail();
        void GetContactDetail();
    }
    interface DerivedInterface : BaseInterface
    {
        void ShowDetail();
    }
    class InterfaceImplementer : DerivedInterface
    {
        string Name;
        int Age;
        string Address;
        long PhoneNumber;
        public void GetPersonalDetail()
        {
            Console.Write("Enter Your Name:");
            Name = Console.ReadLine();
            Console.Write("Enter Your Age:");
            Age = int.Parse(Console.ReadLine());
        }
        public void GetContactDetail()
        {
            Console.Write("Enter Your Address:");
            Address = Console.ReadLine();
            Console.Write("Enter Your Phone Number:");
            PhoneNumber = long.Parse(Console.ReadLine());
        }
        public void ShowDetail()
        {
            Console.WriteLine("");
            Console.WriteLine("Your Details are:");
            Console.WriteLine("Name: " + Name);
            Console.WriteLine("Age: " + Age);
            Console.WriteLine("Address : " + Address);
            Console.WriteLine("Phone Number: " + PhoneNumber);
        }
    }
    class MainClass
    {
        static void Main(string[] args)
        {
            InterfaceImplementer MyObj = new InterfaceImplementer();
            MyObj.GetPersonalDetail();
            MyObj.GetContactDetail();
            MyObj.ShowDetail();
            Console.Write("\nPress ENTER to quit...");
            Console.ReadLine();
        }
    }
}

Multiple Inheritance Example c#



    The following example program in c# implements multiple inheritance in .net with interfaces. Multiple inheritance in .NET framework cannot be implemented with classes, It can only be implemented with interfaces.


Example program for Multiple Inheritance



using System;

//Example Program for Multiple Inheritance
namespace ProgramCall
{

    //The Icar interface should defines all car properties
    interface Icar
    {
        int WheelsCount();

    }



    //The IPlane interface should defines all plane properties
    interface IPlane
    {
        bool CanFly
        {
            get;
        }

    }



    //The superCar class should implement Icar and Iplane interfaces to become supercar
    class SuperCar : Icar, IPlane
    {

        //The class should implement both intefaces
        #region Icar Members

        public int WheelsCount()
        {
            return 4;
        }

        #endregion




        #region IPlane Members

        public bool CanFly
        {
            get
            {
                return true;
            }

        }

        #endregion
    }





    class Program
    {
        static void Main(string[] args)
        {
            //Creating instance for SuperCar class
            SuperCar mysupercar = new SuperCar();

            Console.WriteLine("My Super Car has " + mysupercar.WheelsCount() + " Wheels and can fly is " + mysupercar.CanFly);

            Console.ReadLine();
        }

    }
}


//Output
//My Super Car has 4 Wheels and can fly is True

 Output

My Super Car has 4 Wheels and can fly is True

My Favorite Books

  • C and Data Structures by Ashok N. kamthane
  • Web Technologies by A. A. Puntambekar
  • Learn HTML and CSS with W3Schools
  • Learn JavaScript and Ajax with W3Schools
  • HTML5 Black Book: Covers Css3, Javascript,XML, XHTML, Ajax, PHP And Jquery
  • HTML5 Application Development Fundamentals: MTA Exam 98-375
  • .NET 4.0 Programming 6-In-1, Black Book
  • SQL Server 2008 R2 Black Book
  • Asp.net 4.0 Projects Covers: net 3.5 And .net 4.0 Codes, Black Book
  • UNIX Shell Programming 1 Edition by Yashavant Kanetkar
  • UNIX and Shell Programming 1 Edition by Richard F. Gilberg, Behrouz A. Forouzan
  • Computer Networks by Andrew S. Tanenbaum
  • Multiple Choice questions in computer science by Timothy J Williams