OOPS CONCEPTS IN C#

OOPS CONCEPTS AND INTERVIEW ANSWERS:



Class

      A Class is template definition.  Instance of methods, functions, variables and object.



Object:

  Object is a real entity. Class will not occupy any memory space hence to work with the data represented by class you must create a variables for the class. Which call as an object. Using keyword is new. This memory will be allocated.

Abstraction:

 Hiding unwanted data and given rel vent data. Abstraction solve the problem in design level.

Encapsulation:

Encapsulation mean hiding the code and data in to a single unit to protected the data from outside word.
Solving  the problem in implementation level.

 Inheritence:

Creating a new class from existing class is called as inheritance.


Inheritance can be classified to 5 types.



  1. Single Inheritance
  2. Hierarchical Inheritance
  3. Multi Level Inheritance
  4. Hybrid Inheritance
  5. Multiple Inheritance




1. Single Inheritance

when a single derived class is created from a single base class then the inheritance is called as single inheritance.

                                  


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

namespace ConsoleApplication4
{
class Parent
    {
        public  void add()
        {
            Console.WriteLine("this program is add")
          
        }
class child:parent
    {
        public  void sub()
        {
            Console.WriteLine("this program is sub");
          
        }

   

class Program
    {
       

        static void Main(string[] args)
        {
            child prg = new child ();
            prg.add();
prg.sub();
            Console.ReadLine();
        }
    }
}

2. Hierarchical Inheritance


when more than one derived class are created from a single base class, then that inheritance is called as hierarchical inheritance.




3. Multi Level Inheritance


when a derived class is created from another derived class, then that inheritance is called as multi-level inheritance.


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

namespace ConsoleApplication4
{
class Parent
    {
        public  void add()
        {
            Console.WriteLine("this program is class add");
          
        }
class child:parent
    {
        public  void sub()
        {
            Console.WriteLine("this program is class sub");
          
        }

      class small:child
    {
        public  void mul()
        {
            Console.WriteLine("this program is class mul");
          
        }

   


class Program
    {
       

        static void Main(string[] args)
        {
            small prg = new small ();
            prg.add();
            prg.sub();
            prg.mul();

            Console.ReadLine();
        }
    }
}



4. Hybrid Inheritance


Any combination of single, hierarchical and multi level inheritances is called as hybrid inheritance.




5. Multiple Inheritance


when a derived class is created from more than one base class then that inheritance is called as multiple inheritance. But multiple inheritance is not supported by .net using classes and can be done using interfaces.


Polymorphism


   Polymorphism means same operation may behave different on different class

     Two Types:
                    1. Overloading
                        2. Overriding
   


Overloading-static-compile time 



                Same method name but different arguments it is overloading.

Overriding-Dynamic-run time

              Same method name and same argument. It is overriding.
              Parent Class-keyword- virtual
             Parent Class-keyword- override

Sealed Class:

    When you  want to restrict your class from being inherited by others you can create the class as sealed class.
               Method is sealed method.