C#

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:-
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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

No comments: