OOP'S Question






FEATURE OF OOPS in C#.Net
Ø  Encapsulation
Ø  Data Abstraction
Ø  Polymorphism
Ø  Inheritance



What is Encapsulation?
Encapsulation definition with example in C#.Net
  • Encapsulation is a process of hiding all the internal details of an object from the outside world & provide global interface access to object.
  • Below table show how can we able to connectivity to the other objects.
Access Modifier
Description (access information)
public
Allows public access to member both inside a class & outside. We can access anywhere. 
private
Allows private access to member only inside a class (within the body of the class).
internal
We can accesses with in the assembly (only current assembly). 
Protected
Allows protected access to member base class & derived class.
protected internal
 Allows protected internal access to member current assembly, base class & derived class.

Example:
using System;

class
 BankAccountDetails
{
    private string m_CustName;

    public string bankCustName
    {
        get { return 
 m_CustName ; }
        set { 
 m_CustName  = value; }
    }
}
 
In this example. we are hiding the string bankCustName to the other object.



What is Polymorphism?


  1.   Polymorphism, Poly means many and morph means form. 
  2.   “same message for different purpose” e.g. salary, salary for Emp or Manager salary .
  3.   Compile Time Polymorphism: Method Overloading(It is also called early binding)
  4.  Run Time Polymorphism: Method Overriding (It is also called late binding.)



Compile Time Polymorphism

  
  Method Overloading

Method with same name but with different arguments OR different sequence of
      Parameter OR different type of parameter is called method overloading”.

   Method Overloading forms compile-time polymorphism.

- Example of Method Overloading:

class A
    {
        public int sum(int a,int b)
        {
            return a + b;
        }
        public int sum(int a, int b, int c)
        {
            return a + b + c;
        }
    }

class A
    {
        void hello()
        {
            Console.WriteLine("Hello1");
        }
        void hello(string s)
        {
            Console.WriteLine("hello1{0}",s);
        }
    }




Run Time Polymorphism


Method Overriding

 “Method with same name   with SAME arguments OR SAME sequence of parameter OR SAME type of parameter is called Method Overriding”.

- Method overriding forms Run-time polymorphism.

- Example of Method Overriding:

Class parent
{
virtual void hello()
{ Console.WriteLine(“Hello from Parent”); }
}

Class child : parent
{
override void hello()
{ Console.WriteLine(“Hello from Child”); }
}

static void main()
{
parent objParent = new child();
objParent.hello();
}                   
   //OutputHello from Child.






What is Data Abstraction?

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

Purpose: - a common definition of the base class that can be shared by multiple derived class.

Features of an Abstract Class as following

  1. Abstract Class cannot be creating an object.
  2. An Abstract Method of an Abstract Class must be overridden.
  3. An Abstract Class must be inherited.
  4. An Abstract Class can only contain Abstract Method.
Example:-example of T.V.,
when a customer goes to purchase a T.V., the shopkeeper gives a small description of the feature of T.V., such as Flat, resolution pixel, sound system, button, but not describe the internal working system of the  T.V.

The process of showing the general information related to the T.V. is Abstraction & hiding the complex information from the user is called Encapsulation


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

namespace ConsoleApplication7
{
        abstract class vechicle//Base class
    {
        //method declared
        public abstract void Drive(); //method should be public
    }
    class car : vechicle//Derived class
    {
        public override void Drive()//method define
        {
            Console.WriteLine("deriving implement from car");
            Console.ReadLine();
        }
    }
   class Program
    {
        static void Main(string[] args)
        {
            car c1 = new car();
            c1.Drive();
        }
    }
}
OUTPUT:- deriving implement from car


What is Data Abstraction?

Abstract Class cannot be instantiated.

Error Message

Error List :- Cannot create an instance of the abstract class or interface 'ExAbstract.A'


It must be inherited



Output

This is Constructor of abstract class A


Abstract Class may have Concrete Method

Output

This is Constructor of abstract class A
This is a concrete method within an abstract class A

Abstract Class may have Abstract Method

Error Message

Error List:- 'ExAbstract.B does not implement inherited abstract member 'ExAbstract.A.display()''

An abstract method of an abstract class must be overridden



Output

This is Constructor of abstract class A
This is concrete method within an abstract class A
An abstract method is being overridden here.

An Abstract Method can be present only in Abstract Class

Error Message

Error List:- 'ExAbstract.Z.newMethod() is abstract but it is contained in non-abstract class 'ExAbstract.Z''




Abstraction
Interface
We can declared field
We cannot declared field
It required override method.
It not required override method.
method should be public
no need to write public key word 
(By Default method is public in Nature)
Purpose :- a common definition of the base class that can be shared by multiple derived class

Purpose: - achieve multiple inheritances.






Abstract Class
Interface
An abstract class can contain fields; implement properties, constructors, or destructors.
An Interface cannot declared field constructors, or destructors and implement properties.
It required override method.
It not required override method.
Various access modifiers such as abstract, public, protected, virtual, internal, etc. are useful in abstract Classes
But not in interfaces class.
Method Should is public in nature.
Method no need to write public key word 
(By Default method is public in Nature)
An abstract class cannot support multiple inheritances.
But an interface can support multiple inheritances. 
Purpose :- a common definition of the base class that can be shared by multiple derived class

Purpose: - achieve multiple inheritances.

Abstract classes are faster than interfaces class.





What is Interface?

  1.   Interface is a collection of abstract data member such as event, method & properties.
  2.   An interface is defined by the key word interface.
  3. We cannot declared field, implement properties and constructors, or destructors.
  4. An interface has no implementation; it only has the definition of the methods without the body.
Purpose: - achieve multiple inheritances.


using System;
namespace OOPsinterface
{

     ///
 Summary description for Interface10.
     
 Public class Interface10myinterface 
     {
          
public static void Main ()
     {
          
Interface1 infobj = new Interface1 ();
          infobj.callmethod ();
     }
      
public Interface10 ()
     {
          
// TODO: Add constructor logic here
     
}
      
public void callmethod()
     {
          
Console.WriteLine("callmethod implemented!!!!");
     }
}
interface myinterface
{
void callmethod();
}
}




What is Sealed Class?

  1.  Once a class is defined as sealed class, this class cannot be inherited. 
  2.  A sealed class cannot be used as a base class. For this reason, it cannot also be an abstract class. 

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

namespace sealedclass
{
    class Program
    {
        public sealed class BaseClass
        {
            public void Display()
        {
            Console.WriteLine("This is a sealed class which can’t be further inherited!!!!");
        }
    }

        public class Derived : BaseClass
        {
           // this Derived class can’t inherit BaseClass because it is sealed
        }
   
        static void Main(string[] args)
        {
            BaseClass obj1 = new BaseClass();

            Obj1.Display();

            Console.ReadLine();
        }
    }
}


Tabs


Tab 1 content goes here

Tab 2 content goes here

Tab 3 content goes here
Multi-Tabbed Widget | DotNetIs