OOPS Concept in C#

Object Oriented Programming language(OOPS):- It is a methodology to write the program where we specify the code in form of classes and objects.
    CLASS:-Class is a user defined data type. it is like a template. In c# variable are termed as instances of classes. which are the actual objects.
    EX:-
    Class classname
    {
      [variable declaration;]
      [Method declaration;]
    }
    Here Class is a keyword and class name is valid c# identifier. Everything inside the square brackets is optional.It is also valid class definition.
    EX:-

    Class Empty
    {

    }

    NOTE:- There are some similarities and difference between class & struct. we will discuss later.

    OBJECT:- Object is run time entity which has different attribute to identify it uniquely.
    EX:-
    Here This is an example of creating an object of type Rectangle.

    Rectangle rect1=new rectangle();
    Rectangle rect1=new rectangle();

    Here variable 'rect1' & rect2 is object of the rectangle class.
    The Method Rectangle() is the default constructor of the class. we can create any number of objects of Rectangle class.

    Basic Principle of oops:- There are main three core principles of  any object oriented languages.
    • ENCAPSULATION:- Encapsulation provides the ability to hide the internal details of an object from its users.The concept of encapsulation is also known as data hiding or information hiding. In c# , Encapsulation is implemented using the access modifier keywords.
    1. Public
    2. Private
    3. protected
    4. Internal
    5. Protected Internal
      • POLYMORPHISM:- It is also concept of oops. It is ability to take more than one form. An operation may exhibit different behaviour in different situations.                                                           Ex:-  An addition operation involving two numeric values will produce sum and the same addition operation will produce a string.If we pass parameter numeric value then method return sum(numeric value) and if we pass parameter  String then same method is return string value .this called Polymorphism.
      • INHERITANCE:-  One class can include the feature of another class by using the concept of inheritance.In c# a class can be inherit only from one class at a time.Whenever we create class that  automatic inherit from System.Object class,till the time the class is not inherited from any other class.
      Ex:-
      using System;
      namespace Inheritance
      {
          class Program
          {
              public static void Main()
              {
                  employee obj2 = new employee();
                  obj2.display();
                  obj2.show();
                  Console.ReadLine();
              }
          }
          public class cls
          {
              public void display()
              {
                  Console.WriteLine("HELLO");
              }
          }
          public class employee : cls
          {
              public void show()
              {
                  Console.WriteLine("welcome");
              }
          }
      }
      

      NOTE:-Here we are calling cls class(base class) member by creating the object of employ class(child class).it is known as inheritance. Other elements of oops concepts:
      1. Method Overloading 
      2. Method Overriding
      3. Method hiding
      4. Access-specifier or Modifier
      Steps:- open console application in your visual studio First.
      windows forms application

      copy the code and paste program.cs file and run the application.

      OUTPUT:-

      output

      For More:-
      1. Create setup file with Database
      2. Web Form Controls
      3. File Handling Real Application
      4. Host ASP.NET Application on Server Free
      5. Ado.Net Application
      6. Asp.net 4.0 coding models
      7. How to create dll file for .NET Application
      8. How to use check constraint in your application
      9. Structure and classes in c#
      10. How to solve sql server database problems
      11. How to understand event concepts in c# 
      12. How to implement WCF service in asp.net
      13. How to implement multi threading in c#
      14. How implement Web Form controls in asp.net application
      15. How to save image in database and display in picture Box control using c#

      I hope this helpful for you
      To Get the Latest  Free Updates Subscribe
      click below for download the application;
                           DOWNLOAD

      18 comments:

      1. in this .its been said like "Whenever we create class that automatic inherit from System.Object class,till the time the class is not inherited from any other class."

        if i am not wrong, every class inherits from system.object class.

        will there be any situation when one derived class don't have the features of the system.object class?

        ReplyDelete
      2. hi mini, you alreay know c# ,java,c++ does not support multiple inheritance.there are four types of inheritance
        1- Single inheritance
        2- multilevel inheritance
        3- hierarchical inheritance
        4- multiple inhritance

        here i have used single inheritance.in multilevel inheritance if any class inherited from other class then
        we can again inherit child class from another classes.same as hierarchical inheritance.c# does not support multiple
        inheritance.if any class inherit from other class then you can not inherited from other class.it means all three are
        possible but multiple inheritance is not possible.

        ReplyDelete
        Replies
        1. Hello Mr. RAMASHANKER VERMA,
          In C# inheritance is only of two types.
          Remaining are the sub- categories of inheritance.

          Delete
      3. Why c# does not supports multiple inheritace give an example

        ReplyDelete
      4. Hi friend !this is good question, In multiple inheritance one child class inherit the feature of two or more base classes.
        first i will give YOU real time example. suppose there are two people with same name(ramesh)lives in two different home.if one person (ram) is friend of both(ramesh).if any unknown person came and asked about ramesh to ram.Here ram will confuse because he knowS two person with same name. So he can give wrong address.this is problem of multiple inheritance.

        Now according to programming language: if two base class(student,men) has same method name(show()) and these two classes are inheriting by the another child class (employee).
        if we create the object of child class(employee),then another two base class (student ,men) will load automatically in memory. if we access the show() method through employee class object(emp).ex:emp.show();at that time compiler will confuse which show() method called,so it will give error that why c# not support multiple inheritance.i have given perfect example why c# does not support multiple inheritance ,which is given below:
        see it:

        using System;
        namespace whynot_mutiple_inheritsnce
        {
        class Program
        {
        public class student
        {
        public void show()
        {
        Console.WriteLine("this is student class method");
        }
        }
        public class men
        {
        public void show()
        {
        Console.WriteLine("this is men class method");
        }

        }
        public class employee : men,student
        {
        public void dislpay()
        {
        Console.WriteLine("this is employee class method");
        }

        }
        public static void Main(string[] args)
        {
        employee emp = new employee();
        emp.show();
        Console.ReadLine();
        }
        }
        }

        compiler confuse which class show() method will call.
        To solve this problem we use interface.

        ReplyDelete
        Replies
        1. i was expecting this kind of sample example on this.

          Delete
        2. Hi Rama Shanker, hope you know that there c# basically folly two type of inheritance
          1> Class Base inheritance
          2> Interface Base inheritance .

          C# and JAVA only one support Class Base inheritance but multiple support Interface Base inheritance .
          For Example
          Class Or interface base inheritance ,
          //Class
          public class Person
          {
          public int Id { get; set; }
          public string FirstName { get; set; }
          public string LastName { get; set; }
          public string Type { get; set; }
          }

          //interface

          public interface IApprove {
          bool Approved { get; set; }
          // etc
          }

          //inheritance

          public class Student : Person, IApprove {
          }
          You might also leave the class Approve as such, and have classes with a property of that type:

          public class Student : Person {
          Approve _approve = new Approve();
          public Approve Approve {
          get { return _approve; }
          }
          }


          Or Second Example
          // Class and multiple inheritance

          public interface ISteerable { SteeringWheel wheel { get; set; } }

          public interface IBrakable { BrakePedal brake { get; set; } }

          public class Vehicle : ISteerable, IBrakable
          {
          public SteeringWheel wheel { get; set; }

          public BrakePedal brake { get; set; }

          public Vehicle() { wheel = new SteeringWheel(); brake = new BrakePedal(); }
          }

          Thanks
          Manish Ranjan.




          Delete
        3. Nicely explained Suresh :)

          Delete
      5. Excellent Rama Shanker, u given perfect example... Thanks

        ReplyDelete
      6. Hello Mr.Rama Shanker..i am looking for a windows form which uses the oop building blocks

        ReplyDelete
        Replies
        1. yes, you can use oops concepts in windows form application.Any things which you can implement in console application ,you can implement in windows forms application also.

          Delete

      @2012-2014 msdotnet. All Rights Reserved. Don't Copy