1

I've heard that all Java functions are implicitly virtual, but I'm still not sure if this will run how I want.

Suppose I have a class A, with child B. both A and B have functions called foo(), so B's definition is overriding A's.

Suppose also that A has a function called that takes an instance of A as a parameter:

If I pass in an instance of B to the function, which definition of foo() will it call, A's or B's?

Stevie Kideckel
  • 1,928
  • 2
  • 17
  • 21
  • i think private functions are not virtual, you can have two `private foo()` methods and each class will call its own – hoaz May 17 '13 at 20:29
  • 1
    @hoaz they are indeed virtual but can't be overridden. By the way, in the subclass you don't override the `private` method instead you just *hide* it. This is known as hiding. More info: http://docs.oracle.com/javase/tutorial/java/IandI/override.html – Luiggi Mendoza May 17 '13 at 20:30
  • don't you think you contradict to yourself? :) if it is virtual, what virtual properties does it have? – hoaz May 17 '13 at 20:32
  • @hoaz http://stackoverflow.com/q/4547453/1065197 by the way, don't ask me why Java was designed that way, ask James Gosling about it :) – Luiggi Mendoza May 17 '13 at 20:37
  • 1
    that says the same thing, private methods are final and thus non-virtual – hoaz May 17 '13 at 20:41
  • The word `private` is not mentioned in the question - anywhere! Come on guys!!!! – OldCurmudgeon May 17 '13 at 21:20
  • 1
    @OldCurmudgeon Nor is any other access level -- and the answer depends on the access level. – Andy Thomas May 17 '13 at 21:38
  • It should be A.foo() due to polymorphism of passing object of type B as type A in the method parameter. If you want B.foo() to be invoked, you should cast the passed parameter as B then invoke, or you should changed the parameter type from class A to an interface defining foo() that A implements, where B extends A. Then whatever implementation of the interface is passed will have its' foo() method invoked. – Antony Booth Feb 06 '14 at 05:20

2 Answers2

4

As I mentioned in my comment private functions are not virtual and I want to demonstrate it using following example:

class A {
    public void foo() {
        System.out.println("A#foo()");
    }

    public void bar() {
        System.out.println("A#bar()");
        qux();
    }

    private void qux() {
        System.out.println("A#qux()");
    }
}

class B extends A {
    public void foo() {
        System.out.println("B#foo()");
    }

    private void qux() {
        System.out.println("B#qux()");
    }
}

Now lets run following code:

A foobar = new B();
foobar.foo(); // outputs B#foo() because foobar is instance of B
foobar.bar(); // outputs A#bar() and A#qux() because B does not have method bar 
              // and qux is not virtual
hoaz
  • 9,883
  • 4
  • 42
  • 53
  • private methods are limited to the scope of the class. protected, to the package public, everyone Therefore, a private member of a class has no external accessibility, even for inherited classes, therefore, it being virtual serves no purpose as it can't be accessed by anything other than it's containing class. – Antony Booth Feb 06 '14 at 04:32
3

B's implementation will be called.
That's exactly what virtual means.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964