Dynamic traffic control



  • class A {
        void callme() {
            System.out.println("   callme() из класса А");
        }
    }
    
    class B extends A {
        void callme() {
            System.out.println("   callme() из класса B");
        }
    }
    
    class C extends A {
        void callme() {
            System.out.println("   callme() из класса C");
        }
    }
    
    class Dispatch {
        public static void main(Strin args []) {
            A a = new A();
            B b = new B();
            C c = new C();
    
            A r; // <--
    
            r=a;
            r.callme();
    
            r=b;
            r.callme();
    
            r=c;
            r.callme();
        }
    }
    

    Question: For what reason in the line marked by the shooter, it is the object А?



  • You've got to take down some sort of objects. ABC One variable. So this variable should have a type that is a common ancestor of these types. These types are two: A and Object

    But if you want to call a method callmeObject It doesn't fit, it's not defined. A A Right, that's the method. callme (which then overlaps in derivative classes).


Log in to reply
 


Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2