×

Loading...
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。
Ad by
  • 最优利率和cashback可以申请特批,好信用好收入offer更好。请点链接扫码加微信咨询,Scotiabank -- Nick Zhang 6478812600。

Hi Eric

本文发表在 rolia.net 枫下论坛Hi Eric,
I insert my understanding in what you posted above. Good night!

Polymorphism,
First let's think about how animal eats. Human being use mouth to chew, bird use beek to peck, mosquito use its pipe to drink, eat is one conception, but depends on different animal the eating really do differently when the do the same thing. This is Polymorphism. For example, there is a class Father and a class son derived from Father, they may both have a method work, but the father is a doctor and the son is a programmer.

class Father
{
public void work(){.............} // A doctor
}

class Son extends Father
{
//method work inherited from Father
public void work( ){..........} // programmer
}

Polymorphism here is in an inherited tree, a Father-Son chain.

--------------------------------------------------------------
I think the above is not polymorphism exactly, it is inheritance.
Suppose you are a tailor and have a task to design and sew a cloth which is suitble for the Father you mentioned above and is or will be also suitble for his desendants, his son or daughter or grandson or granddaughter or grandgrandson or grandgranddaughter ....
But you do not know how many desendants the Father have or will have. How can you make your design? Do not worry! OO tells you some rules. By obeying these rules you can complete your task successfully. These rules and some mechanism behide the rules is polymorphism. For Java example:

class Cloth {
WearOn(Father f);
}
Father f;
Son s;
GrandSon gs;
Cloth c;
c.WearOn(f);
c.WearOn(s);
c.WearOn(gs);
........
c.WearOn(any object of Father's desendant);

The rules and mechanisms which ensure the above pseudo-code work well is polymorphism.
-----------------------------------------------

And maybe we could take Polymorphism as many methods with same name but different parameter so they will act differently. Say, there is a class Human, it has method eat.
Class Human
{
public void eat ( Soup A)
{
//Use soup spoon to drink
}
public void eat ( Bread B)
{
//Use finger to pick up
}
}
So when you invoke method eat, depends on different food, you will act differently.

-----------------------------------------------
I think the two "eat" methods in Human class is not polymorphism, it is something called "overloading".
-----------------------------------------------

Interface,
Interface is a place to define a method and constrain the programmer to implement it when use this interface. Inside the Interface, you only need to define the method, then in the class which implement the interface you shoud code for the method.
When we build a class, if it implements interface runnable, it must code for method run( ). But the method run( ) is actually defined in Interface Runnable.

Another one of its usage is to implement "Multiple inheritance". For example, son gets character both from mother and father, but Java does not allow multiple inheritance. So we use Interface to complet this work.
Interface Father
{
public void methodF1( ){}

public void methodF2 ( ){}

public int methodF3 ( ) {}
}

Interface mother
{
public void methodM1( ){}

public void methodM2 ( ) {}
}

class Son implements Father, Mother
{
//Remember you have to implement all
//method defined in Father and Mother here.
public void methodF1(){.............}
public void methodF2 (){............}
public int methodF3 ( ){...........}
public void methodM1(){.........}
public void methodM2(){..........}
}
-----------------------------------------------
I do not think it is a "Multiple inheritance".
-----------------------------------------------

Inner class
This is just a method to write your code. You may put it outside a class and the inner class will not be an inner class any longer.

-----------------------------------------------
Inner class is not a method. Probably I misunderstand what you said.
-----------------------------------------------更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report

Replies, comments and Discussions:

  • 工作学习 / IT技术讨论 / Jabber, HH, and how are you: let me try to say something about Polymorphism, Interface and Inner class, please give me your comments. Thanks.
    本文发表在 rolia.net 枫下论坛Polymorphism,
    First let's think about how animal eats. Human being use mouth to chew, bird use beek to peck, mosquito use its pipe to drink, eat is one conception, but depends on different animal the eating really do differently when the do the same thing. This is Polymorphism. For example, there is a class Father and a class son derived from Father, they may both have a method work, but the father is a doctor and the son is a programmer.

    class Father
    {
    public void work(){.............} // A doctor
    }

    class Son extends Father
    {
    //method work inherited from Father
    public void work( ){..........} // programmer
    }

    Polymorphism here is in an inherited tree, a Father-Son chain.

    And maybe we could take Polymorphism as many methods with same name but different parameter so they will act differently. Say, there is a class Human, it has method eat.
    Class Human
    {
    public void eat ( Soup A)
    {
    //Use soup spoon to drink
    }
    public void eat ( Bread B)
    {
    //Use finger to pick up
    }
    }
    So when you invoke method eat, depends on different food, you will act differently.

    Interface,
    Interface is a place to define a method and constrain the programmer to implement it when use this interface. Inside the Interface, you only need to define the method, then in the class which implement the interface you shoud code for the method.
    When we build a class, if it implements interface runnable, it must code for method run( ). But the method run( ) is actually defined in Interface Runnable.

    Another one of its usage is to implement "Multiple inheritance". For example, son gets character both from mother and father, but Java does not allow multiple inheritance. So we use Interface to complet this work.
    Interface Father
    {
    public void methodF1( ){}

    public void methodF2 ( ){}

    public int methodF3 ( ) {}
    }

    Interface mother
    {
    public void methodM1( ){}

    public void methodM2 ( ) {}
    }

    class Son implements Father, Mother
    {
    //Remember you have to implement all
    //method defined in Father and Mother here.
    public void methodF1(){.............}
    public void methodF2 (){............}
    public int methodF3 ( ){...........}
    public void methodM1(){.........}
    public void methodM2(){..........}
    }

    Inner class
    This is just a method to write your code. You may put it outside a class and the inner class will not be an inner class any longer.

    OK, if what I say is wrong, please give me your criticism .

    And, "how are you", please visit my home page jvericwu.top263.net, maybe it could be help.

    URL: jvericwu.top263.net更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • Polymorphism ----many faces
      Polymorphysm means many faces.

      Polymorphism occurs at two levels. 1) CLASS level; 2) METHOD LEVEL.

      1) A Bird can be a Parrot, or a Swallow, or Crow...

      2) All the Animals can move. A Horse can run. A Fish can swim. A Swan can fly. .... All the
      Animals can move.
      In C++, one needs declare move() method of the Animal class
      to be virtual; In Java, there is no such requirement and one just needs override the move()
      method in the subclass.

      I did not get any difficultties in understanding polymorphism, because I studied "Thinking in
      Java" and "Thinking in C++".
      If anybody feels confused about polymorphism, I sugget lokking into Bruce Eckel's book. Some
      people criticise Bruce's book and say Bruce's book has no depth. I don't care. I just need a book
      that is correct and easy to understand.
      • where can I find the book 'thinking in C++'?thanks
        • www.bruceeckel.com
    • I am so sorry to say that I have no more time to read it in detail today. I will come here after two or three days. Say sorry again!
    • Thank you, Eric Wu. I've downloaded all your website. I should study your course sometimes later. Thanks for your help. I really hope there're more courses in your site.
    • Hi Eric
      本文发表在 rolia.net 枫下论坛Hi Eric,
      I insert my understanding in what you posted above. Good night!

      Polymorphism,
      First let's think about how animal eats. Human being use mouth to chew, bird use beek to peck, mosquito use its pipe to drink, eat is one conception, but depends on different animal the eating really do differently when the do the same thing. This is Polymorphism. For example, there is a class Father and a class son derived from Father, they may both have a method work, but the father is a doctor and the son is a programmer.

      class Father
      {
      public void work(){.............} // A doctor
      }

      class Son extends Father
      {
      //method work inherited from Father
      public void work( ){..........} // programmer
      }

      Polymorphism here is in an inherited tree, a Father-Son chain.

      --------------------------------------------------------------
      I think the above is not polymorphism exactly, it is inheritance.
      Suppose you are a tailor and have a task to design and sew a cloth which is suitble for the Father you mentioned above and is or will be also suitble for his desendants, his son or daughter or grandson or granddaughter or grandgrandson or grandgranddaughter ....
      But you do not know how many desendants the Father have or will have. How can you make your design? Do not worry! OO tells you some rules. By obeying these rules you can complete your task successfully. These rules and some mechanism behide the rules is polymorphism. For Java example:

      class Cloth {
      WearOn(Father f);
      }
      Father f;
      Son s;
      GrandSon gs;
      Cloth c;
      c.WearOn(f);
      c.WearOn(s);
      c.WearOn(gs);
      ........
      c.WearOn(any object of Father's desendant);

      The rules and mechanisms which ensure the above pseudo-code work well is polymorphism.
      -----------------------------------------------

      And maybe we could take Polymorphism as many methods with same name but different parameter so they will act differently. Say, there is a class Human, it has method eat.
      Class Human
      {
      public void eat ( Soup A)
      {
      //Use soup spoon to drink
      }
      public void eat ( Bread B)
      {
      //Use finger to pick up
      }
      }
      So when you invoke method eat, depends on different food, you will act differently.

      -----------------------------------------------
      I think the two "eat" methods in Human class is not polymorphism, it is something called "overloading".
      -----------------------------------------------

      Interface,
      Interface is a place to define a method and constrain the programmer to implement it when use this interface. Inside the Interface, you only need to define the method, then in the class which implement the interface you shoud code for the method.
      When we build a class, if it implements interface runnable, it must code for method run( ). But the method run( ) is actually defined in Interface Runnable.

      Another one of its usage is to implement "Multiple inheritance". For example, son gets character both from mother and father, but Java does not allow multiple inheritance. So we use Interface to complet this work.
      Interface Father
      {
      public void methodF1( ){}

      public void methodF2 ( ){}

      public int methodF3 ( ) {}
      }

      Interface mother
      {
      public void methodM1( ){}

      public void methodM2 ( ) {}
      }

      class Son implements Father, Mother
      {
      //Remember you have to implement all
      //method defined in Father and Mother here.
      public void methodF1(){.............}
      public void methodF2 (){............}
      public int methodF3 ( ){...........}
      public void methodM1(){.........}
      public void methodM2(){..........}
      }
      -----------------------------------------------
      I do not think it is a "Multiple inheritance".
      -----------------------------------------------

      Inner class
      This is just a method to write your code. You may put it outside a class and the inner class will not be an inner class any longer.

      -----------------------------------------------
      Inner class is not a method. Probably I misunderstand what you said.
      -----------------------------------------------更多精彩文章及讨论,请光临枫下论坛 rolia.net
      • Thank you, HH. I fully agree with you on your comments. Since my idea was to help a beginner to get a good start, I mixed overload, override, and polymorphism.