7 Replies Latest reply: Dec 15, 2007 1:42 AM by 807601 RSS

    Interfaces and subclasses

    807601
      Hi all,

      The following code compiles and runs fine when I replace line
      B b1 = new A();
      by
      A b1 = new A();
      I think the former line should also work fine as it is pointing to object of type A. But that is not happening and it givin a compile time error. Please clarify me whats wrong.
      interface I{
        int interface_variable = 2;
      void print();
      }
      
      class B{
      }
      
      public class A extends B implements I{
      
      public static void main(String [] args){
      
      B b1 = new A(); // b1 points to object of type A and class A implements interface I, I declares print() -- 9
      
      b1.print(); // cannot resolve symbol error generated at this line. WHY?
      
      }
      
      public void print( ){ System.out.println("Value of interface variable = "+interface_variable);}
      
      }
      thanks in advance
        • 1. Re: Interfaces and subclasses
          807601
          The type B does not have a method called print. You're referring to the instance of A through a B reference, so you only have the methods defined by B and the methods it's inherited. By the way, I know you think using names like B, A and I will simplify things for us, but it won't, it's easier to think around something meaningful. Use actual, meaningful names for things, even in noddy sample apps like this
          • 2. Re: Interfaces and subclasses
            807601
            Tha is right. put a method in class B like this

            class B{
                 void print(){}
            }
            • 3. Re: Interfaces and subclasses
              807601
              Ahadu wrote:
              Tha is right. put a method in class B like this

              class B{
                   void print(){}
              }
              While that would make the above code compile, it's not necessarily the correct solution. Almost certainly not, in fact. Best thing as far as I can see is to declare a reference to I, rather than B (horrible names)
              • 4. Re: Interfaces and subclasses
                807601
                Ahadu wrote:
                Tha is right. put a method in class B like this

                class B{
                     void print(){}
                }
                This is a bad suggestion. The print() method is part of the "I" interface.

                If in your code you know b1 contains an instance of A, you can cast it to A and then call print(): ((A)b1).print()
                But if you're only going to use instances of A in b1, just declare b1 to be of type A.
                • 5. Re: Interfaces and subclasses
                  807601
                  interface I{
                    int interface_variable = 2;
                  void print();
                  }
                  
                  class B{
                  public void print(){System.out.println("inside B");}
                  }
                  
                  public class A extends B implements I{
                  
                  public static void main(String [] args){
                  
                  B b1 = new A(); // b1 points to object of type A and class A implements interface I, I declares print() -- 9
                  
                  b1.print(); // cannot resolve symbol error generated at this line. WHY?
                  
                  }
                  
                  public void print( ){ System.out.println("Value of interface variable = "+interface_variable);}
                  
                  }
                  I still have doubts...

                  When I define print() method in class B, b1.print() should call the method print() in class B. Because b1 is a reference of type B. But here b1.print() is calling the print method in class A.

                  Correct me if Iam wrong.

                  Sorry about the horrible name. I like to keep it shor. But will try to give meaningful names next time.
                  • 6. Re: Interfaces and subclasses
                    807601
                    Doesn't matter what kind of reference you have, the method called will be the one on the class instantiated. You haven't instantiated B, you've instantiated A, so A.print gets invoked. In any case, the idea is, you use a reference to an I, so calling code doesn't care whether it points to an A or a B. That's polymorphism, innit

                    Short names suck. The reasoning behind using them is (almost) always wrong. Code should communicate what it does. In particular, when a total stranger - such as we forumites - have to evaluate your snippets, making them so abstract and lacking in context makes it harder for us to visualise what's going on. Patronise us with names like Dog and Cat :-)
                    • 7. Re: Interfaces and subclasses
                      807601
                      OK. done!! :-)