Skip to content

Latest commit

 

History

History
57 lines (35 loc) · 888 Bytes

e93f.md

File metadata and controls

57 lines (35 loc) · 888 Bytes

Back to questions

e93f: Apparent and actual types

Consider the following classes:

public class A {

}

public class B extends A {

}

public class C {

  public void accept(A a) {
    System.out.println("Accepted object of type A.");
  }

}

public class D extends C {

  public void accept(B b) {
    System.out.println("Accepted object of type B.");
  }

}

Does the accept method in D override the accept method in C? Explain your answer.

What will the following code print?

public class Test {

  public static void main(String[] args) {

    B b = new B();

    C c = new D();
    c.accept(b);

    D d = new D();
    d.accept(b);

  }

}

Note: The point of this question is that you should try to figure this out without simply typing in the program and running it! (Though you can of course do this to check your answer.)