java

by 7 replies
9
what is the real meaning of polymorphism in java???
#programming #java
  • Polymorphism is a very general programming concept that is employed in many modern programming languages including C++, Java, Python, PHP, and more.

    The word itself is a hybrid of "poly" (many), and "morph" (form) -- put them together, and you get "multi-form-ism." In reality, this isn't far off; roughly speaking, polymorphism refers to treating a programming construct (such as an object, a variable, a type, or a function) as if it has multiple "forms" or "types."

    This comes up everywhere in many programming languages. Here are a few examples of polymorphism, with a bias towards Java (since that's what you titled your thread).

    Functional overloading is a type of polymorphism. "Functional overloading" is the process of creating multiple functions that have the same name, but a different number of arguments. So, for example, I might want to make two different functions to print out a person's name, depending on whether they provided one name or two:
    Code:
    void printName(String name)
    {
        System.out.printf(name);
    }
    void printName(String first, String last)
    {
        System.out.printf(first + " " + last);
    }
    This is an example of polymorphism, because the function printName() has two different "forms."

    Another example of polymorphism is parametric polymorphism. "Parametric polymorphism" occurs when a programmer would like to pass a variable as a parameter to a function without specifying the variable's type. In Java, this is handled by the "generic" keyword. This is an example of polymorphism because the parameter that is sent to the function could be any type, or "form."

    Finally, one last example of polymorphism in Java is may be referred to as inheritance method polymorphism, and occurs within a class inheritance model. For example, a programmer may have a base class Animal with a method talk(). Next, the programmer implements two classes Bird and Dog that are derived from the Animal class. Thus, Birds and Dogs are both Animals. However, birds and dogs do not talk the same, so when we call Bird::talk(), we expect different behavior than when we call Dog::talk(). Perhaps when a Bird talks, we expect "tweet" to be printed, but when a Dog talks, we expect "woof" to be printed. This is an example of polymorphism, because the method Animal::talk() has different functionality based on whether the Animal that is talking is a Dog or a Bird.
    • [1] reply
    • Or simply - it allows you to override a function to have a different behavior from the original.

      Polymorphism is one of the pillars of Object Oriented Programming.
  • Polymorphism is an object oriented concept widely used in Java. In Java polymorphism is supported along with Abstraction, Encapsulation and Inheritance. Polymorphism is something which can take many forms. Polymorphism in Java is the use of common interface instead of concrete implementation while writing code.
  • Polymorphism is one object which have many forms.
    For ex-
    public interface Vegetarian{}
    public class Birds{}
    public class Parrot extends Birds implements Vegetarian{}

    Now, the Parrot class is considered to be polymorphic since this has multiple inheritance. Following are true for the above example:

    A Parrot IS-A Bird

    A Parrot IS-A Vegetarian

    A Parrot IS-A Parrot

    A Parrot IS-A Object

    now if we make a object of Parrot class then the following declarations are legal

    Parrot d = new Parrot();
    Birds a = d;
    Vegetarian v = d;
    Object o = d;

    all the reference variables refer to same Parrot object in the heap
    • [1] reply
  • Polymorphism

    The dictionary definition of polymorphism refers to a principle in biology in which an organism or species can have many different forms or stages.

    This principle can also be applied to object-oriented programming and languages like the Java language. Subclasses of a class can define their own unique behaviors and yet share some of the same functionality of the parent class.

    source: Polymorphism (The Java™ Tutorials > Learning the Java Language > Interfaces and Inheritance)
  • Banned
    hi, Polymorphism in java is not one things it contain.because poly is taken from greek word its meaning is many so it contain many forms in one structure like abstraction,encapsulation etc.

Next Topics on Trending Feed