×

Loading...

Topic

  • 工作学习 / IT技术讨论 / Sos! help me from Polymorphism, Interface & Inner classes...
    本文发表在 rolia.net 枫下论坛Sos! help me from Polymorphism, Interface & Inner classes...

    According to Jaber's instruction, I began "Thinking in Java" one month ago. It's not so easy as most of the people think. After object, program flow, constructor and reusing class, I arrived in Polymorphism, Interface & Inner classes last week. However, I couldn't pass through these 3 parts. I read the book again and again and typed the source codes again and again, it's still in the book instead of in my brain.

    I know Polymorphism is the core knowledge of OO so I have to know it well. I wonder whether my study method need a little improvement. I remembered that I was always studying a language through a real project before. Is that be more helpful for me to do a real Java project? If so, can any expert give me a topic?

    For example, when someone was willing to learn FoxPro, I will tell him try to write a program about Salary Management. After a simple Salary Management, he should understand database, table, menu, function, procedure, macro and many fundamental phenomenon. After that, he could study more by himself. Can Java be learned like this? If so, can anyone tell me what can I do now except "hello world".

    Would you please tell me something more about how to learn Polymorphism, Interface & Inner classes?

    Thank you very much in advance. Your help will be great appreciated by all the learners.

    Thank you!更多精彩文章及讨论,请光临枫下论坛 rolia.net
    • Some suggestions.
      Yes. U r right and polymorphism is the key of OO. And it is also the base of CORBA and COM. So maybe you can read some basic topics about COM or CORBA.
      • Thank you very much. Do you mean COM and CORBA maybe helpful for understanding the polymorphism?
    • u need to learn more basic knowledge
      u need get to know what is virtual function and pure virtual function before u read the part of Polymorphism.
      • tks. would you please give me some details about virtual function and pure virtual function? I have no idea about them. Can you tell me one address or book to learn such functions? thanks.
        tks. would you please give me some details about virtual function and pure virtual function? I have no idea about them. I'm so sorry to say it's the first time for me to hear such phenomenon. Can you tell me one address or book to learn such functions?
        thanks.
        • Some ideas.
          Virtual Functions are always used for interface definition, e.g. COM is based on virtual funcions list. Please read some documents about the principle of COM. It is very useful for U to understand virtual functions.
      • For "How are you"....
        I think cli's suggestion is to the point. You have better slow down. I suggest you download "Thinking in Java" and look into these concepts.
        • tks. I will go back the first chapter of Think in java again in two days.
          Dear Jaber,

          thank you!

          I downloaded Think in Java 2nd edition(from the address you told someone here). I had the book Think in Java Chinese edition(1st edition). I have read both of them several times before polymorphism. i think most of the phenomenon before polymorphism i had some sense.

          I will try to finish all the excercises and examples before polymorphism in two days in the 2nd edition. Maybe it's helpful to understand the concepts.

          thank you!
      • It's said that Virtual is a keyword in C++ to use dynamic binding. If so, knowing Virtual must be very helpful for polymorphism. But, I'm so sorry to say I don't know C++ or C either. Would you please show me another way? thanks.
        thanks.

        I read Thinking in Java again last night.

        It's said that Virtual is a keyword in C++ to use dynamic binding. If so, knowing Virtual must be very helpful for polymorphism. But,

        I'm so sorry to say I don't know C++ or C either. Would you please show me another way? thanks.
        • Let me try.
          Let me try to explain in English. I try to make things simple and hope it will be a little help for you and not make you confuse more.

          As far as I know, virtual function is used in C++ to realize the polymorphism, not in Java. So I do not disccuss about virtual function here. But it is definitely helpful for you to understand polymorphism if you understand virtual function in C++.

          Simply to say, polymorphism in Java is one way that enables you to define methods with objects (let us call them object parameter) as their parameters and you can pass any objects which is in the inheritant tree rooted from an object parameter to the methods.

          For example:

          base calss A;
          derived class B : A;
          derived class C : A;

          class D {
          method(A a) {
          ...;
          }
          }

          You can call D::method(A a) as following:

          B b;
          C c;
          D d;
          d.method(b);
          d.method(c);
    • About interface and inner-class.
      本文发表在 rolia.net 枫下论坛Let me try to explain in English.
      I think you have read a lot about Java. Here I try to explain what you concerned in my way of understanding things. Hope not to make any confuse.

      Interface is a definition about how a entity to communicate with other entities. It does not matter how the interface is immplemented.

      For example of PCI slot in personal computer: it is a interface between the PCI cards and the mainboard. But different mainboards and different PCI cards have their own immplementations. If the immplementations conform the interface definition they will work well.

      In Java the Interface is a declaration of a set of methods and some named constants in a class, their immplementations are not defined or specified. Just like a PCI slot in personal computer.
      You can immplement an interface (call it PCISlot) as following:

      class PCICard implements PCISlot {
      ......
      }

      or

      class MainBoard implements PCISlot {
      ......
      }

      As far as I know, inner-class was introduced from Java JDK 1.1and later and just is a nonstatic class defined within another class

      class OutClass {
      class InnerClass {
      ......
      }
      }

      Please pay some attentions to some access limitations on the inner class.

      I am not sure if these make things clear. Hope not to make you confuse again.更多精彩文章及讨论,请光临枫下论坛 rolia.net
      • I really appreciate your example. Just one addendum.
        Interfaces have many uses in Java. One is to introduce new data types. For instance, java.lang.String is an Object, but it is also a Serializable, a Comparable.

        By the way, it is not necessary for an interface to contain some methods and/or static final constants. It may be empty! The example is Serializable, Cloneable. Such interfaces just introduce Data types, not any methods or final static constants.
        Such a design is called the Marker Pattern.
        • Thank you very much! You make things more clear.
          Thank you! You make things more clear.
          However let me try to make a cunning explanation about "a set of methods ...".
          1. In computer terminologies, a set (not a non-empty set) of something means the number of elements in the set can be 0, 1, 2, ..., n.
          2. If you designe a container class which abstracts concept of a set (a collection of somethings), I am definitely sure you will define and implement a method: bool IsEmpty() -- Is the set empty?
          The above is just my cunning explanation. You make things more clear.
    • 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, please visit my home page jvericwu.top263.net, maybe it will be help.更多精彩文章及讨论,请光临枫下论坛 rolia.net
      • Thank you, Jabber, HH and Eric Wu. With your kind help, I got an idea about polymorphism which is just overloading and overriding based on inheritence. Maybe include some upcasting, just like HH and Mr. Wu's perfect examples. thank you very much.
        Thank you, Jabber, HH and Eric Wu. With your kind help, I got an idea about polymorphism. But i'm not sure whether it's right. polymorphism is just overloading and overriding. Maybe include some upcasting. I feel it's based on inheritence, just like HH and Mr. Wu's perfect examples. I think I can get more feeling with your instruction. thank you very much.
    • Did you code using C++?
      find some C++ book and just read the basic concepts of C++, I think you can understand it easily. Polymorphism is not a difficult concept, and I suggest you read some good books of C++ in Chinese first,e.g. early C++ books published by Tsinhua university.

      Interface and inner class are under the same situation. I think your problem is you cannot understanding english well.
      • Sorry. I have no idea about C or C++. In fact, I just know well in FoxPro for DOS and know some Visual FoxPro. So Java maybe more difficult to me.