Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Top Posters

Who's Online (2)

Powered by Vanilla. Made with Bootstrap.
OOP versus Procedural Programming
  • Hey Guys !!!! Normally I am busy. But today I got some free time so I
    think I should write something about the OOP programming approach and
    Procedure Programming approach. Because many new programmer like me
    always get confuse between OOP because we create primarily procedural
    code.

    Lets understand these one by one and understand the differences. :)

    In procedure programming approach or procedure oriented approach, the
    problems is viewing  in a sequence of things or tasks. And these tasks
    are accomplished by the functions or we can say that we write the
    functions to accomplish these tasks. This approach basically consists of
    writing a list of instructions and organizing these instructions into
    group known as function. In this approach because we concentrate on the
    development of the functions thats why very little attention is given to
    the data.

    While in the OOP data is treated as a critical elements in the program
    and does not allow it to flow freely in the system. OOP allows
    decomposition of a problem into a number of entities called objects and
    then build data and functions around these objects. So this the basic
    definition of OOP and Procedure programming or we can say that basic
    difference between them. Now lets get some more about these:-

    Functions are Code-Centric which means that data first sent as input and
    then return as output. While OOP is Data-Centric which means that data
    is internally represent and contains the functionality or method. In OOP
    objects contain data and behavior simultaneously while Function contain
    data and function separated.

    OOP require a different way of thinking about how you construct your
    applications.OOP is basically all about Classes and Objects. Lets
    understand the concept of OOP one by one.

    To understand OOP let us take an example of a car. A car have its own
    color,weight, manufacturer, gas tank etc. Those are its characteristics.
    While a car can accelerate,stop,sound for horn etc. Those are its
    behavior. And a car is itself a class which have characteristics and
    behavior. A class is basically nothing  but a unit of code. Now lets
    come again on our example of a car.

    The characteristics of car are known as its properties. Properties have a
    name and value and they may be vary or not. Like we can change the
    color of a car by giving its new paint job. So, what are properties in
    OOP these are nothing but the variable declared within a class. Which
    have its own name and value. These properties can be variable or
    constant or static. And the behavior of a class is also known as method.
    And we can say that a method is nothing but it its similar to a
    function which we normally used in procedural programming.

    Some concept of OOP are given below:-

    Classes:-As we say that classes are nothing but the
    code which defines the properties and method. Class in PHP contains
    three main components: a members,method and a constructor. A member is a
    piece of data that an object can contains. In our car example color and
    manufacturer are members of class car. Methods are the services which
    are provided to client by the object or we can say that method are
    functions. A method is also known as member function. And a constructor
    is special method that initialize the objects into its ready state.

    Example of how to define a class in PHP is given below:-

    <?php

    class demo {     // class is keyword used in php to declare a class and demo is class name here.

    }

    ?>   

    Objects:-
    Objects are the entities constructed by a class.
    It is an instance of the class. The process of building an object from a
    class is known as instantiation.A class can have more than one object.
    An example for object is given below:-

    <?php



    $objdemo=new demo();  // here we define a object for class demo using keyword new. New keyword is used to define a object for a class.



    ?>

    Inheritance:- Inheritance is process by which objects of one
    class acquire the properties of the objects of another class or we can
    say that parent class. Inheritance provides the idea of reusability. We
    can reuse a code either by copy it and paste it in another or by using
    Inheritance. The copy paste method have major problem is that if we
    found an error in our main code then we have to debug all the pasted
    code. Thats why its good to use Inheritance and by using it we can
    modify a code without changing its main properties. Extends keyword is using for Inheritance. Example of Inheritance is given below:-

    <?php

    class media {

    ---declare some members----

    function somefun(){

    --some code---

    }

    }

    class books extends media {

    ----some new members for books class which is subclass of media class---

    function somenewfun()

    {

    --code for new methods for books class--

    }

    }

    ?>

    Here in this example I used two class one is media which is our parent
    class and another is book which is our subclass. The subclass book have
    all the properties and methods from the parent class media and it is
    also have its own properties and methods which we declare in the book
    class. The  extands keyword is used to Inherit all the properties and methods of parent class to its subclass.

    NOTE:- Don't forget to change the lines --declare
    members-- or --code for method-- to your original logic before running
    the program in PHP.



    I think this is sufficient for this section. Others properties like
    polymorphism and access methods for a class is given to the next
    section. Thanks for tolerating me this long time..:p;).. Hope this will
    be helpful for some of you guys..:).. Thank You all..

  • Xin
    Posts: 3,251
    Great article ! Thanks cyberpirate
    Xin
  • mpzz..:p
  • Deque
    Posts: 78
    These properties can be variable or
    constant or static.


    They can be variable or constant and static or non-static. You should explain what that means too.

    Inheritance is process by which objects of one
    class acquire the properties of the objects of another class


    That is wrong. Not the objects aquire properties of other objects. The class aquires (some) behaviour (= methods) and attributes of the parental class.