3)oop'concepts

Upload: sangeetha-sangu-bc

Post on 06-Jul-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/17/2019 3)OOP'Concepts

    1/6

    INTRODUCTION:

    OOP is a design philosophy. It stands for Object Oriented Programming. Object-

    Oriented Programming (OOP ) uses a different set of programming languages than old

    procedural programming languages (C, Pascal , etc.). Everything inOOP is grouped as self 

    sustainable objects. !ence, you gain re-usability by means of four main object-oriented programming concepts.

    In order to clearly understand the object orientation, let"s ta#e your $hand % as an

    e&ample. 'he $hand % is a class. our body has to objects of type hand, named left hand

    and right hand. 'heir main functions are controlled* managed by a set of electrical

    signals sent through your shoulders (through an interface). +o the shoulder is an

    interface hich your body uses to interact ith your hands. 'he hand is a ell

    architected class. 'he hand is being re-used to create the left hand and the right hand by

    slightly changing the properties of it.

    The object oriented programming  ill give the impression very unnatural to a

    programmer ith a lot of procedural programming e&perience. In Object Oriented

    programming Encapsulation is the first pace. Encapsulation is the procedure of covering

    up of data and functions into a single unit (called class). n encapsulated object is often

    called an abstract data type. In this article let us see about it in a detailed manner.

    What are Objects?

    It is a real time entity.

    n object can be considered a thing that can perform a set of related activities. 'he

    set of activities that the object performs defines the objects behavior. or e&ample, the

    hand can grip something or at!dent (object) can give the name or address. In

    pure OOP terms an object is an instance of a class

    'he above template describe about object t!dent

    /lass is composed of three things name, attributes, and operations

    Class:

     It is a collection of objects.

  • 8/17/2019 3)OOP'Concepts

    2/6

    'he above template describe about object t!dent

    /lass is composed of three things name, attributes, and operations

    public class student

    0

    1

    student objstudent2ne student ()3

    ccording to the above sample e can say that t!dent object, named objst!dent" has

    created out of the student class.

    In real orld you ill often find many individual objects all of the same #ind. s an

    e&ample, there may be thousands of other bicycles in e&istence, all of the same ma#e

    and model. Each bicycle has built from the same blueprint. In object-oriented terms, e

    say that the bicycle is an instance of the class of objects #non as bicycles. In the

    softare orld, though you may not have reali4ed it, you have already used classes. or

    e&ample, the Te#tbo# control, you alays used, is made out of the Te#tbo# class,

    hich defines its appearance and capabilities. Each time you drag a Te#tbo# control,

    you are actually creating a ne instance of the Te#tbo# class.

    $ncaps!lation" Inheritance"Pol%morphism  and abstraction are main pillars o& 

    OOPs. 'hese have been described belo.

    N$$D 'OR $NC(PU)(TION:

    'he need of encapsulation is to protect or prevent the code (data) from accidental

    corruption due to the silly little errors that e are all prone to ma#e. In Object oriented

    programming data is treated as a critical element in the program development and data

    is pac#ed closely to the functions that operate on it and protects it from accidental

    modification from outside functions.

    Encapsulation provides a ay to protect data from accidental corruption. 5ather than

    defining the data in the form of public, e can declare those fields as private. 'he Private

    data are manipulated indirectly by to ays. 6et us see some e&ample programs in /7

    to demonstrate Encapsulation by those to methods. 'he first method is using a pair of

    conventional accessor and mutator methods. nother one method is using a named

    property. 8hatever be the method our aim is to use the data ith out any damage or

    change.

  • 8/17/2019 3)OOP'Concepts

    3/6

     private type member is one that can only be accessed by members ithin the sametype. or e&ample, if the BankAccount  class has a private member, only other membersof the BankAccount  class can access or call that member.

    lthough the default access for type members is private, I prefer to be e&plicit about myintentions hen declaring type members and include the access modifier, rather thanrely on defaults. I thin# it ma#es the code easier to read and ma#es it clear to other

    developers hat my true intention is. 6isting 9:-; shos ho to use the private accessmodifier and offers an e&ample of hy you ould ant to use it.

    $#:

    using +ystem3

    class ame

      0  get 0 return m=name3 1

      set 0 m=name 2 value3 1  11

    Its common to encapsulate the state of your type ith properties. In fact, I alays rapmy type state in a property. In 6isting 9:-;, you can see ho the name of the customeris held in the m_name field, but it is rapped (encapsulated) ith

    the CustomerName property. et does not support multiple classes Inheritance.+ynta&@ -public class /lass>ame@ +uper/lass>ame0+tatements---------

    1Constr!ctors in Inheritence

    this

    It is a 5eference Object of /urrent /lass. It refers to the Aembers of the /urrent /lass.

    base

  • 8/17/2019 3)OOP'Concepts

    4/6

    It is a 5eference Object of +uper /lass. It refers to the Aembers of the +uper /lassNote: *

    'he first call in a /lass is /lass /onstructor'he first call in a Inheritance is +uper /lass /onstructor

    What is Pol%morphism?

    Polymorphism means same operation may behave differently on different classes.

    E&ample of /ompile 'ime Polymorphism@ Aethod Overloading

    E&ample of 5un 'ime Polymorphism@ Aethod Overriding

    $#ample o& Compile Time Pol%morphism

    +ethod O,erloading

    - Aethod ith same name but ith different arguments is called method overloading.

    - Aethod Overloading forms compile-time polymorphism.- E&ample of Aethod Overloading@

    class 9

    0

    void hello()

    0 /onsole.8rite6ine($!ello%)3 1

    void hello(string s)

    0 /onsole.8rite6ine($!ello 0B1%,s)3 1

    1

    $#ample o& R!n Time Pol%morphism

    +ethod O,erriding

    - Aethod overriding occurs hen child class declares a method that has the same type

    arguments as a method declared by one of its superclass.

    - Aethod overriding forms 5un-time polymorphism.

    - >ote@

  • 8/17/2019 3)OOP'Concepts

    5/6

    static void main()

    0

    parent objParent 2 ne child()3

    objParent.hello()3

    1

     **Output

    !ello from /hild.

    (bstractionAbstraction means that you have some class that is more common than others thatextend it. By example, if you have classes Triangle and Rectangle:

    class  Triangle

    {

      public double a;  public double b;

      public double c;

      public double Area

      {

      get { return triangles area !

      !

    !

    class Rectangle

    {

      public double a;

      public double b;

      public double Area

      {

      get { return rectangles area !

      !

    !

     These classes have something in common " they have property called Area plus alsosides #a and b$ but % dont see any reason to ma&e sides abstract. 'ets add also (ircleand )e have no point of sides at all. *o) lets generali+e these classes and lets createone more common class called hape. Also, lets ma&e it abstract class so this classcannot be created separately " it can be created only through extending.

    public abstract class hape

    {

      public double Area#$;

    !

    And no) lets )rite previous classes so they extend hape class.

  • 8/17/2019 3)OOP'Concepts

    6/6

    class  Triangle : hape

    {

      public double a;

      public double b;

      public double c;

      public double Area

      {

      get { return triangles area !

      !

    !

    class Rectangle : hape

    {

      public double a;

      public double b;

      public double Area

      {

      get { return rectangles area !

      !

    !

    o, )hats the )in, you may as&- &ay, not of these classes use hape as their baseclass. o does (ircle. %n the context )here )e don t care about speci/c properties ofob0ect )e can handle all extended ob0ects as hape. By example, lets calculate totalarea of hapes in list.

    'ist1hape2 shapes 3 'isthapes#$ 44 contains circles, triangles and rectangles

    double area 3 5;

    foreach#hape shape in shapes$

      area 63 shape.Area;

    44 do something useful )ith area here

     

    http://www.blogger.com/email-post.g?blogID=32696142&postID=5673566674377040270http://www.blogger.com/email-post.g?blogID=32696142&postID=5673566674377040270