MY bLOG

Wednesday, 5 March 2014

OOPS Concepts

oops(Object oriented programming structure)

1.Abstraction 
2.Encapsulation
3.Polymorphism
4.Inheritance.

It can use while implementing the functionality of your projects ,according to your requirement
Class:
class is an user defined data type
class is a collection fields , Methods, properties and events.
A class exist logically whereas object exist physically
Public class Customer
{
}
Customer objCust=new Customer();

Object: Object is an instance of class
Syntax: customer ObjCust;
ObjCust=new customer();
Abstraction: Hiding the unnecessary details from the view of the users is called  abstraction
Or
 Hiding the implementation is known as Abstraction.
Abstraction is a process that involves identifying the critical behavior of an object and eliminating irrelevant and complex details.
Encapsulation:
Binding or Encapsulating the methods with the fields by providing the security for data.
Or
Accessing  the public properties by using private variables is one of the good example in encapsulation
Example:
private int _id;

    public int Id
    {
        get { return _id; }
        set { _id = value; }
    }
Here you are not accessing the private variable id but you are accessing the public property id
Polymorphism:
Same function with the different behavior   with different no of values is called is polymorphism
By achieving polymorphism in Three types:
Function Overloading, Operator overloading-early binding-compile time
Method Overloading:
Same function with different  signature is known as method overloading.
Function Overloading is implemented within the same  class.
Example:
public class class1
    {

        public int add(int a, int b)
        {
            return a + b;

        }

        public int add(int a, int b, int c)
        {
            return a + b + c;
        }
    }

Function overriding-late binding-run time Polymorphism
Method Overriding:
Overriding means changing the functionality of a method without changing the signature. We can override a function in base class by creating a similar function in derived class. This is done by usingvirtual/override keywords
class Baseclass
    {
       
            public virtual void A()
            {
                Console.WriteLine("BaseMethod");
            }

            public class derivedclass : Baseclass
            {
                public override void A()
                {
                    Console.WriteLine("DerivedMethod");
                }
           
        }
        static void Main(string[] args)
        {
            derivedclass objprog = new derivedclass();

            objprog.A();
            Baseclass objprog1 = new derivedclass();
            objprog1.A();
            Console.ReadLine();

        }
    }
o/P: Derived Method
     Derived Method

Method Hiding:
It is a concept of polymorphism while method shadowing redefines the whole element,
Shadowing helps you override a function in a parent method even if that is not marked virtual
 New keyword helps you to override a non-virtual method in the child class.
Example:
public class Baseclass
    {
public void A()
        {
            Console.WriteLine("BaseMethod");
        }

        public class derivedclass : Baseclass
        {
            public new void A()
            {
                Console.WriteLine("DerivedMethod");
            }
        }
        static void Main(string[] args)
        {
            derivedclass objprog = new derivedclass();
            objprog.A();
            Baseclass objprog1 = new derivedclass();
            objprog1.A();
            Console.ReadLine();
}
}
O/p: Derived Method
     Base Method.
The new keyword is used to hide the method because the base class method is not marked as
Virtual. If it is marked as virtual and if there is any overridable method is available then you will get derived method output.
Note:Overriding redefines only the element while shadowing redefines the whole element.

Inheritance: This is a process of creating a new class from already existing class
Whereas  existing class is known as parent class or Base class  and new class is known as child class or derived class, Inheritance is the purpose of code reusability and providing additional features.
Example:
public class Baseclass
    {
       public Baseclass()
       {
             Console.WriteLine ("Base Class");
    }
                                
    public void Write ()
    {
        Console.WriteLine ("Write method in baseclass");
    }
                               
public class ChildClass:Baseclass
{
                                
    public ChildClass ()
    {
        Console.WriteLine("Child Class");
    }
  
    public static void Main ()
    {
        ChildClass CC = new ChildClass ();
        CC.Write ();
        Console.ReadLine();
    }
}
       }
Types of Inheritance:
Single Inheritance: Creating Single new class from single base class is known as single inheritance.
Multiple Inheritance: Creating a new class from two or more base classes is known as multiple inheritance.
Note: In c# multiple inheritance is not possible  using inheritance instead of that you can use interfaces.
Mutli Level Inheritance: Creating a new class from already derived class is known as multilevel inheritance.

Hybrid Inheritance: It is the combination of multiple and Multilevel Inheritance

No comments:

Post a Comment