AD MANAGEMENT

Collapse

BEHOSTED

Collapse

What is POLYMORPHISM ?

Collapse

Collapse

Edit this module to specify a template to display.

X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • What is POLYMORPHISM ?

    The subject of polymorphism is probably the most important in OOP. Using classes and inheritance makes it easy to describe a real-life situation as opposed to just a collection of functions and data. They also make it much easier to grow projects by reusing code mainly via inheritance. Also, to write robust and extensible code, you usually want to have as few as possible flowcontrol
    statements (such as if() statements). Polymorphism answers all these needs and more.

    Consider the following code:

    class Cat {
    function miau()
    {
    print "miau";
    }
    }
    class Dog {
    function wuff()
    {
    print "wuff";
    }
    }
    function printTheRightSound($obj)
    {
    if ($obj instanceof Cat) {
    $obj->miau();
    } else if ($obj instanceof Dog) {
    $obj->wuff();
    } else {
    print "Error: Passed wrong kind of object";
    }
    print "\n";
    }
    printTheRightSound(new Cat());
    printTheRightSound(new Dog());
    The output is

    miau
    wuff

    You can easily see that this example is not extensible. Say that you want to extend it by adding the sounds of three more animals. You would have to add another three else if blocks to printTheRightSound() so you check that the object you have is an instance of one of those new animals, and then you have
    to add the code to call each sound method.


    Polymorphism using inheritance solves this problem. It enables you to inherit from a parent class, inheriting all its methods and properties and thus creating an is-a relationship .

    Taking the previous example, we will create a new class called Animal from which all other animal kinds will inherit, thus creating is-a relationships from the specific kinds, such as Dog, to the parent (or ancestor) Animal. Inheritance is performed by using the extends keyword:

    class Child extends Parent {
    ...
    }
    This is how you would rewrite the previous example using inheritance:

    class Animal {
    function makeSound()
    {
    class Animal {
    function makeSound()
    {
    The output is

    miau
    wuff


    You can see that no matter how many animal types you add to this example, you will not have to make any changes to print The Right Sound() because the instanceof Animal check covers all of them, and the $obj->makeSound() call
    will do so, too.

    This example can still be improved upon. Certain modifiers available to you in PHP can give you more control over the inheritance process. They are covered in detail later in this chapter. For example, the class Animal and its method makeSound() can be marked as being abstract, which not only means that you don’t have to give some meaningless implementation for the make- Sound() definition in the Animal class, but also forcing any inheriting classes to implement it. Additionally, we could specify access modifiers to the makeSound() method, such as the public modifier, meaning that it can be called anywhere in your code.
    Commercial AC Repair | Modular Kitchen Services | Home Paint Services | RO Repair and Installation Service | Water Tank Cleaning Services | Sofa Cleaning Services | Modular Kitchen Designers | Kitchen Cleaning Services | Mover and Packer Services

  • #2
    Polymorphism is an object-oriented development proven fact that relates to the ability of a variable, function or object take on several types. A language that features polymorphism allows developers to system in the normal rather than program in the specific.
    Best Rotary Airlock Valves || shower diverter valves

    Comment


    • #3
      A variable with a given name may be allowed to have different forms and the program can determine which form of the variable to use at the time of execution.
      Paytm Login
      Myntra
      Ebay Cashback
      Push notifications

      Comment


      • #4
        Polymorphism is the ability of an object to take on many forms. The most common use of polymorphism in OOP occurs when a parent class reference is used to refer to a child class object.
        Any Java object that can pass more than one IS-A test is considered to be polymorphic. In Java, all Java objects are polymorphic since any object will pass the IS-A test for their own type and for the class Object.

        Polymorphism has two types:
        1) Static Type (Method Overloading)
        2) Dynamic Type (Method Overriding)
        Oracle PL/SQL Training, Oracle RAC Training, PCIe Training, Linux device drivers course, python training

        Comment


        • #5
          In the Object Oriented Programming Polymorphism is refer to the ability of an object to persist in various forms. It is one of the important feature of OOP's.
          Mobile apps development company India | Online game development company | Android apps developer company

          Comment


          • #6
            Originally posted by technodesign View Post
            Polymorphism is an object-oriented development proven fact that relates to the ability of a variable, function or object take on several types. A language that features polymorphism allows developers to system in the normal rather than program in the specific.
            This is a good overview
            Zomex - Providing cPanel Hosting since 2009!
            Responsive WHMCS Templates for hosting providers
            WHMCS Modules | Webmaster Coupons | Decorative Gold

            Comment

            TEXT

            Collapse

            160

            Collapse

            Unconfigured Ad Widget

            Collapse

            GOOGLE

            Collapse

            Announcement

            Collapse
            1 of 2 < >

            FreeHostForum Rules and Guidelines

            Webmaster forum - Web Hosting Forum,Domain Name Forum, Web Design Forum, Travel Forum,World Forum, VPS Forum, Reseller Hosting Forum, Free Hosting Forum

            Signature

            Board-wide Policies:

            Do not post links (ads) in posts or threads in non advertising forums.

            Forum Rules
            Posts are to be made in the relevant forum. Users are asked to read the forum descriptions before posting.

            Members should post in a way that is respectful of other users. Flaming or abusing users in any way will not be tolerated and will lead to a warning or will be banned.

            Members are asked to respect the copyright of other users, sites, media, etc.

            Spam is not tolerated here in most circumstances. Users posting spam will be banned. The words and links will be censored.

            The moderating, support and other teams reserve the right to edit or remove any post at any time. The determination of what is construed as indecent, vulgar, spam, etc. as noted in these points is up to Team Members and not users.

            Any text links or images contain popups will be removed or changed.

            Signatures
            Signatures may contain up to four lines

            Text in signatures is subject to the same conditions as posts with respect decency, warez, emoticons, etc.

            Font sizes above 3 are not allowed

            Links are permitted in signatures. Such links may be made to non-Freehostforum material, commercial ventures, etc. Links are included within the text and image limits above. Links to offensive sites may be subject to removal.

            You are allowed ONLY ONE picture(banner) upto 120 pixels in width and 60 pixels in height with a maximum 30kB filesize.

            In combination with a banner/picture you can have ONLY ONE LINE text link.


            Advertising
            Webmaster related advertising is allowed in Webmaster Marketplace section only. Free of charge.

            Shopping related (tangible goods) advertising is allowed in Buy Sell Trade section only. Free of charge.

            No advertising allowed except paid stickies in other sections.

            Please make sure that your post is relevant.


            More to come soon....
            2 of 2 < >

            Advertise at FreeHostForum

            We offer competitive rates and a many kinds of advertising opportunities for both small and large scale campaigns.More and more webmasters find advertising at FreeHostForum.com is a useful way to promote their sites and services. That is why we now have many long-term advertisers.

            At here, we also want to thank you all for your support.

            For more details:
            http://www.freehostforum.com/threads...eHostForum-com

            More ad spots:
            http://www.freehostforum.com/forums/...-FreeHostForum
            See more
            See less
            Working...
            X