2

I'm confused with polymorphism and I'm wondering if this is consider polymorphism?

I feel it looks kind of weird but it still compiles correctly.

public class Family {
    void FamilyInfo() {
        System.out.println("This is a family super class");
    }
}

public class Grandparents extends Family {
    void FamilyInfo() {
        System.out.println("Graparents are the elders in the family and they are the sub class of family");
    }
}

public class Parents extends Grandparents {
    void FamilyInfo() {
        System.out.println("The parents are the children of the grandparents and they are the sub sub class for family");
    }
}

public class FamilyDemo {
    public static void main(String ary[]) {
        Grandparents Gp = new Grandparents();
        Parents P1 = new Parents();


        Gp.FamilyInfo();
        P1.FamilyInfo();

    }
}
1
  • I think to show polymorphism you should create a Family variable, assign it a new Parents() object and call its FamilyInfo() method. This way you see that the object's method is called, not the variable type's method. As an aside, you should learn Java naming conventions and use it in your code. Variable and method names should begin with a lower case letter while class names should begin with an upper case letter. Dec 18, 2012 at 4:13

8 Answers 8

3

Your method FamilyInfo is being overridden in all three classes in the hierarchy. This is one example of polymorphism.

When you call Gp.FamilyInfo();: It will call the method implemented in Grandparents class and print Graparents are the elders in the family and they are the sub class of family while P1.FamilyInfo(); will call the method in Parents class and print The parents are the children of the grandparents and they are the sub sub class for family.

Thus you can see that same method FamilyInfo() has two different behaviors, which is polymorphic behavior.

Your example is very similar to one mentioned in the tutorial here: Java Tutorial : Polymorphism. So don't get confused.

1

The example does not demonstrate polymorphism,rather i can just see simple object oriented inheritance.In order that the concept of polymorphism be used the code should be the following.

public class FamilyDemo 
{
    public static void main(String ary[]) 
    {
        Family Gp = new Grandparents();
        Family P1 = new Parents();

        Gp.FamilyInfo();
        P1.FamilyInfo();
    }
}

Even though Gp is of type Family, it behaves like type Grandparents because it is initialized with an object of that type.

Then,the following may be expected: Graparents are the elders in the family and they are the sub class of family. The parents are the children of the grandparents and they are the sub sub class for family.

0
1

Our trainer said that using extends is more of an example of inheritance. But if we use implements(interface), we can say that it is polymorphic because we can implement many interfaces.

e.g.

interface Horse {
    void run();
}

interface Eagle {
    void fly();
}

public class Pegasus implements Horse, Eagle {
    // Implement methods
     public void run() {
         // do run
     }
     public void fly() {
         // do fly
     }
 }
0

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

The basic concept is for a given object to act like another. This is achieved through the use of interfaces and inheritance in Java.

A better example of this would be (with you code as a base)

public class FamilyDemo {
    public static void main(String ary[]) {
        Family gp = new Grandparents();
        Family p1 = new Parents();

        dump(gp);
        dump(p1);
    }

    public static void dump(Family family) {
        family.FamilyInfo();
    }
}

This basically allows Gradparents and Parents to "act" as they are Family

0

1.What is polymorphism?

In object-oriented programming, polymorphism (from the Greek meaning "having multiple forms") is the characteristic of being able to assign a different meaning or usage to something in different contexts - specifically, to allow an entity such as a variable, a function, or an object to have more than one form.

2. Two Types of polymorphism

a) Static or Compile time Polymorphism

Which method is to be called is decided at compile-time only. Method overloading is an example of this.for example

public class calculation
{
    public int add(int x, int y)
    {
        return x + y;
    }
    public int add(int x, int y, int z)
    {
        return x + y + z;
    }
}

here you can see there are two functions with the same name but different signatures

b)Dynamic or Runtime Polymorphism.

Run time polymorphism is also known as method overriding. In this mechanism by which a call to an overridden function is resolved at a Run-Time (not at Compile-time) if a base Class contains a method that is overridden.

Class BaseClass
{
Public void show ()
{ 
Console.WriteLine("From base class show method");
}
}
Public Class DynamicDemo : BaseClass
{
Public void show()
{
Console.WriteLine("From Derived Class show method");
}
Public static void main(String args[])
{
DynamicDemo dpd=new DynamicDemo ();
Dpd.show();

}
}
0

Technically speaking, this is polymorphism. However, you have chosen a poor example and it seems like you are not quite understanding the idea behind polymorphism. A better example would be something like this.

public abstract class Shape {
  public abstract void drawShape();
}

public class Rectangle extends Shape {
  public void drawShape() {
    // code for drawing rectangle
  }
}

public class Circle extends Shape {
  public void drawShape() {
    // code for drawing circle
  }
}

public class FilledRectangle extends Rectangle {
  public void drawShape() {
    super.drawShape();
    // code for filling rectangle
  }
}

Then a class that is responsible for the drawing doesn't need to know how to draw each individual shape. Instead, it can do this

public void drawAllShapes(Shape[] myShapes) {
  for (int i = 0; i < myShapes.length; ++i) {
    myShapes[i].drawShape();
  }
}

The goal is to abstract away the concrete implementation and all the details that go with and instead only present a common interface. This makes it a lot easier to work with different classes, as you can see in the last method above.

0

A good example for polymorphism would be:

public static void print(Family[] family){
    for(int i=0; i< family.length; i++){
        family[i].FamilyInfo();
    }
}

public static void main(String args[])
{
    Family[] family = new Family[2];
    Grandparents Gp = new Grandparents();
    Parents P1 = new Parents();
    family[0] = Gp;
    family[1] = P1;

    //and then send the array to another method which 
    //doesn't "know" which entry in the array is a parent and which is grandparent
    //and there you can loop the array calling family[i].FamilyInfo();
    //THIS is the whole idea of polymorphism in a nutshell
    print(family);

}
0

Yes, in your example program you are using inherit and polimorphism, infact both are closed related.

You are using inherit because you extend once Family from Grandparents class, and once Parents class extending Grandparents and you are also using polimorphism because you are writing in your subclasses a method void FamilyInfo which is written in the super class.

You should use @Override in this way:

public class Parents extends Grandparents {
    @Override
    void FamilyInfo() {
        System.out.println("The parents are the children of the grandparents and they are the sub sub class for family");
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.