Home > DeveloperSection > Beginner > Introduction to OOPS (Object oriented Programming System)

Introduction to OOPS (Object oriented Programming System)


ASP.Net C#  Java  OOPS 
Ratings:
0 Comment(s)
 1412  View(s)
Rate this:

Introduction to OOPS (Object oriented Programming System)

In this article, I’m explaining about oops  in .Net

 

Objects means a real word entity such as pen, chair, table etc.

Object Oriented Programming is methodology to design a program using classes and Objects.

The object oriented Programming System is providing 4 principles like:

1. Encapsulation

2.  Abstraction

3. Polymorphism

4. Inheritance

Encapsulation:

                Encapsulation is a way of organizing data members (variables, properties) and  member functions (methods)  into  a single unit.

Abstraction:

Abstraction is a process of hiding the implementation details and showing only functionality to the user.

Polymorphism

Polymorphism means many forms (ability to take more than one form). In polymorphism poly means “multiple” and morph “forms” so polymorphism means many forms.

Polymorphism has ability to provide different implementation of methods that are implemented with same name.

 

In polymorphism we have 2 different types

 

1.     Overloading  or static binding  (Compile Time Polymorphism or early binding)

2.     Overriding  or Dynamic binding (Run  Time Polymorphism or Late binding)

 

       Overloading:

                     The same method name with different type of parameters in a class is known as  method overloading.

Example:

namespace OverloadingProgram

{

    class BaseClass

    {

        public int sum(int a, int b)

        {

            return a + b;

        }

 

        public float sum(int a, int b,int c)

        {

            return (a + b);

        }

    }

 

    class Program

    {

        static void Main(string[] args)

        {

            BaseClass objclass = new BaseClass();

            int result= objclass.sum(2, 3);

             int result1=objclass.sum(4,5,6); 

            Console.WriteLine(result);

             Console.WriteLine(result1);

        }

    }

}

                     

Inheritance

Inheritance is the ability to create a class from another class, the "parent" class, extending the functionality and state of the parent in the derived, or "child" class. It allows derived classes to overload methods from their parent class.

Types of inheritance in c#

1) Single inheritance

2) Multilevel inheritance

Single Inheritance

In Single Inheritance, Base Class functionality access in derive class.

namespace InheritanceTask

{public class BaseClass

    {

        public  int width,height;

        public void setWidth(int w)

        {

            width = w;

        }

        public void setHeight(int h)

        {height = h;

}

}

 

 public  class DriveClass:BaseClass

    {public int getArea()

        {return (width * height);}}}

 

Multilevel Inheritance

Example

namespace MultilevelInheritance

{

    public class Account

    {

        string cust_name;

        int acc_no;

        protected Account(string cname, int acno)

        {

            cust_name = cname;

            acc_no = acno;

        }

        protected void display()

        {

            System.Console.WriteLine("Customer Name=" + cust_name);

            System.Console.WriteLine("Account No=" + acc_no);}}

 

//Inherit base class

    public class Saving_Acc : Account

    {

        int min_bal, saving_bal;

        protected Saving_Acc(string cname, int acno, int mbal, int sbal): base(cname, acno)

        {

            min_bal = mbal;

            saving_bal = sbal;

        }

        protected new void  display()

        {

                        base.display();

            System.Console.WriteLine("Minimum Balance: " + min_bal);

            System.Console.WriteLine("Saving Balance: " + saving_bal);

        }

    }

    //inherit super class

      public class Acct_Details : Saving_Acc

    {

        int deposits, withdrawals;

 

        public Acct_Details(int dep, int withdraw): base("priyanka", 1233, 22222, 22222)

        {

         

            deposits = dep;

            withdrawals = withdraw;

        }

 

        public void display()

        {

            base.display();

            System.Console.WriteLine("Deposit: " + deposits);

            System.Console.WriteLine("Withdrawals: " + withdrawals);

        }

    }

}

 

namespace MultilevelInheritance

{

    class Program

    {

        static void Main(string[] args)

        {

 

            while (true) // Loop indefinitely

            {

 

                System.Console.WriteLine("your want to continue y/n :");

                  string exit = Convert.ToString(System.Console.ReadLine());

                  System.Console.WriteLine("Enter  your deposit no:");

                int deposits = Convert.ToInt32(System.Console.ReadLine());

                System.Console.WriteLine("Enter your withdrawals no:");

                int withdrawals = Convert.ToInt32(System.Console.ReadLine());

                Acct_Details objAcctDetail = new Acct_Details(deposits, withdrawals);

                objAcctDetail.display();

                if (exit == "n")

                {

                    break;

                }

               

            }

        }

    }

}

 


Don't want to miss updates? Please click the below button!

Follow MindStick