Results 1 to 6 of 6
  1. #1
    unclescotty is offline Member
    Join Date
    Jan 2014
    Posts
    3
    Rep Power
    0

    Default What's the big deal of polymorphism?

    Hi,

    I have read that with polymorphism, if I have 3 classes with a foo() method that inherit from the same parent class, I can call the parent class' foo() method and the correct method will be called on the child.

    But, I am trying to understand what is such a big deal about this? Is it just less code to write, so less chance of error? I've heard polymorphism is the essence of OO programming, but I am not sure I really appreciate it.

    Can someone pls explain? Thanks, Scott

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,939
    Rep Power
    8

    Default Re: What's the big deal of polymorphism?

    JPanel is a Java class that creates a component that has a certain size, location, layout, background color, border, etc. It can also contain other components.

    If I want a component that behaves almost exactly like a JPanel, except it shows some custom painting as well, all I have to do is create a class (let's call it MyPanel) that extends JPanel and overrides the paintComponent() method. I don't have to write my own code to give my class a size, location, layout, background color, or border, because all of that functionality is maintained by the parent class. If I want to set a border on my MyPanel class, I can just call MyPanel.setBorder() and it works automatically.

    On the other side of things, the code that tells a JPanel to draw itself when I add it to a JFrame is inside Java itself, so it's not easy for me to change it to support my MyPanel class. Luckily, I don't have to! The existing code can just call JPanel.paintComponent(), and my custom painting code will be called automatically.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    unclescotty is offline Member
    Join Date
    Jan 2014
    Posts
    3
    Rep Power
    0

    Default Re: What's the big deal of polymorphism?

    Wait, I am confused. I thought this was inheritance, where I extend a class and specializes its attributes to my needs, no?

    Quote Originally Posted by KevinWorkman View Post
    JPanel is a Java class that creates a component that has a certain size, location, layout, background color, border, etc. It can also contain other components.

    If I want a component that behaves almost exactly like a JPanel, except it shows some custom painting as well, all I have to do is create a class (let's call it MyPanel) that extends JPanel and overrides the paintComponent() method. I don't have to write my own code to give my class a size, location, layout, background color, or border, because all of that functionality is maintained by the parent class. If I want to set a border on my MyPanel class, I can just call MyPanel.setBorder() and it works automatically.

    On the other side of things, the code that tells a JPanel to draw itself when I add it to a JFrame is inside Java itself, so it's not easy for me to change it to support my MyPanel class. Luckily, I don't have to! The existing code can just call JPanel.paintComponent(), and my custom painting code will be called automatically.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

    Default Re: What's the big deal of polymorphism?

    Inheritance and the selective overriding of methods plus the addition of new methods or features is one way of achieving polymorphic behavior. Another method is programming to an interface. Each subclass or interface implementation may provide their own implementations to change the form of the subclass. Imagine implementing an interface Foo with a method called bar(). Every single class can have a different bar() method. Then you put all of those classes in an array of type Foo and iterate over the entire array calling the bar() method. Each invocation to bar() will behave differently depending on the implementation. IMHO, this is the epitome of polymorphism.

    Regards,
    Jim
    Last edited by jim829; 01-16-2014 at 03:35 AM.
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    unclescotty is offline Member
    Join Date
    Jan 2014
    Posts
    3
    Rep Power
    0

    Default Re: What's the big deal of polymorphism?

    Sorry, but I must seem like a retard. This is exactly what I thought inheritance was all about. I have a class, Animal. Cat and Dog extend Animal with overridden definitions of play(). But, it seems like you both are suggesting that this is polymorphism. So, what's inheritance?

  6. #6
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

    Default Re: What's the big deal of polymorphism?

    Inheritance is a means of achieving polymorphism. It allows you to alter the form (or morph) one class into another. You may overload and/or override methods to change or introduce additional functionality. Here is an excerpt from the introduction of the Java Language Specification.

    "Classes support single implementation inheritance, in which the implementation of each class is derived from that of a single superclass, and ultimately from the class Object. Variables of a class type can reference an instance of that class or of any subclass of that class, allowing new types to be used with existing methods, polymorphically."

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Deal with arraylist
    By kronos in forum New To Java
    Replies: 9
    Last Post: 07-02-2013, 05:00 PM
  2. How do I deal with this error
    By Publiccarbus in forum New To Java
    Replies: 1
    Last Post: 04-13-2012, 04:17 AM
  3. Compile-time Polymorphism or Run-time Polymorphism ?
    By dejulial in forum New To Java
    Replies: 1
    Last Post: 03-06-2012, 07:14 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •