Thursday, May 24, 2012

Object Oriented Programming Concepts

Object Oriented Concepts :-
Abstraction :- It allows you to show only necessary properties. Example RGB
Encapsulation :- Hide the inner complexity of the objects. Example Add to database, everything should be hided inside.
Abstraction and Encapsulation both will complement each other.
Inheritance :-
Inheritance helps to establish the parent-child relationship between classes. By doing so the child class will have the qualities of its parent plus the new qualities of himself.



Polymorphism :-
Poly mean “Many”. Depending on the condition the objects behavior changes. There are two kinds of polymorphism.
Static Polymorphism
                Method Overloading
                Operator Overloading
    public class Complex
    {
        private int real;
        public int Real
        { get { return real; } }

        private int imaginary;
        public int Imaginary
        { get { return imaginary; } }

        public Complex(int real, int imaginary)
        {
            this.real = real;
            this.imaginary = imaginary;
        }

        public static Complex operator +(Complex c1, Complex c2)
        {
            return new Complex(c1.Real + c2.Real, c1.Imaginary +                    c2.Imaginary);
        }
    }

Dynamic Polymorphism
                Method overriding
    public class Product
    {
        public int GetPrice(int quantity, int price)
        {
            return (quantity * price);
        }

        //Method Overridding
        public virtual string GetProductName()
        {
            return "Compressor";
        }
    }

    public class CommercialProduct : Product
    {
        public int GetPrice(int quantity, int price,int totalDiscount)
        {
            return (GetPrice(quantity, price) - totalDiscount);
        }

        public override string GetProductName()
        {
            return "Refrigerator";
        }
    }
Association, Aggregation and Composition :-
Assoication is nothing but “IS” a relationship between classes. Inheritance are type of IS a relationship.
Aggregation and Composition are nothing but “HAS” a relationship between classes. The difference between Aggregation and Compostion is, the lifetime of the object composite relationship will be same whereas in Aggreation relationship it is not.
Raja has a heart is an example for composition because both cant exist independently.
Raja has a shirt is an example for Aggregation because both can exist independently.

No comments:

Post a Comment