OOPs is a problem solving methodology. It decompose larger program into smaller unit called classes. It is easy to use, modify

Object orientation- Focus will be an object rather than whole system.

Class- it is like a blueprint or template for its object. It wrap member function and data into single unit.it is the logical representation of some real world entity or object.

Object-It is an instance of the class/physical representation of the logic class. We can access the member of the class by its object

using System;           
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication6
{
    class employee
    {
        public int empno;
        public string empname;
    }
   
    class Program
    {
 
        static void Main(string[] args)
        {
            //to create object
            employee emp = new employee();
            emp.empno = 1001;
            emp.empname = "Mindstick";
            Console.WriteLine(emp.empno);
            Console.WriteLine(emp.empname);
            Console.ReadKey();
        }
    }
}
 

Pillar of OOP:

1 Encapsulation- it is the feature of data hiding with help of access modifiers, so that it cannot by accessed by the outside world here outside world means outside the class.

  • Access modifiers
  • Public-accessible everywhere (in class where defined, child class by object)
  • Protected-In class in child class
  • Private-only in class
  • Default-only in class
  • Default-private

Encapsulation example.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
                       
namespace ConsoleApplication6
{
    class employee
    {
        public int empno;
        protected string empname;
        private int age;
    }
   
    class Program
    {
 
        static void Main(string[] args)
        {
            //to create object
            employee emp = new employee();
            emp.empno = 1001;
            emp.empname = "Mindstick";// this line shows error
            emp.age = 20;// this line shows error due to protection level
            Console.WriteLine(emp.empno);
            Console.WriteLine(emp.empname);
            Console.ReadKey();
        }
    }
}
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication6
{
    class employee
    {
        public int empno;
        protected string empname;
        private int age;
    }
    class salary:employee
    {
        public void display()
        {
            empno = 1002;
            empname = "Mindstick";
            age=10//error
        }
    }
   
    class Program
    {
 
        static void Main(string[] args)
        {
            //to create object
            employee emp = new employee();
            emp.empno = 1001;
            emp.empname = "Mindstick";// this line shows error
            emp.age = 20;// this line shows error due to protection level
            Console.WriteLine(emp.empno);
            Console.WriteLine(emp.empname);
            Console.ReadKey();
        }
    }
}
 

For accesing private member ex.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace ConsoleApplication6
{
    class employee
    {
                private int age;
                public void setage(int x)
                {
                    if (x >= 18 && x <= 100)
                    {
                        age = x;
                        Console.Write("age set!");
                    }
                    else
                        Console.WriteLine("age not set");
                }
    }
  
   
    class Program
    {
 
        static void Main(string[] args)
        {
            //to create object
            employee emp = new employee();
            emp.setage(10);
               emp.setage(20);
            Console.ReadKey();
        }
    }
}


OOP (Object Oriented Programming)

Inheritance:

It is the feature of OOP .To inherited the property of one class into another class.

It support single inheritance, multi-level inheritance, hierarchical inheritance

Example to show inheritance

  class A              
    {                   
        public int x;            }
    class B:A //single level
    {
        int y;
    }
    class c:B//multilevel
    {
        int c;
    }
    class D : A//heirarchical
    {
    }


You can also visit these related articles and blogs

Introduction to OOPS (Object oriented Programming System)

What is a information hiding in object oriented programming

What is difference between object oriented programming language and object based programming language?


  Modified On Mar-17-2018 03:12:06 AM

Leave Comment