13

I have two question on this code

public class Override {
    private void f() {
        System.out.println("private f()");
    }
    public static void main(String[] args) {
        Override po = new Derived();
        po.f();
    }
}

class Derived extends Override {
    public void f() {
        System.out.println("public f()");
    }
} 

/*
* Output: private f()
*/// :~

1) How is function f is visible on the reference of Override po;

2) Why is output "private f()"

Rahul Garg
  • 8,410
  • 8
  • 33
  • 28

3 Answers3

24
  1. The main method is inside class Override, so ofcourse it can see the private members of class Override.

  2. You are not overriding method f in class Derived, there is no polymorphism. The type of the variable po is Override, so it will take method f from class Override.

Note that method f in class Override is not visible at all in class Derived. The method f in class Derived is a different method, that doesn't have anything to do with the method f in the superclass.

Vinay Sajip
  • 95,872
  • 14
  • 179
  • 191
Jesper
  • 202,709
  • 46
  • 318
  • 350
  • But when the binding is done at runtime then po will have object of Derived class so how its is able to call a private method of super class. – Rahul Garg Aug 28 '09 at 11:40
  • 3
    Nowhere in your code there is a call to a private method of a superclass. When you call po.f(), the f of Override is called because the compile-time type of po is Override. Since there is no overriding, there is no dynamic binding (no polymorphism) - Java does not look at the type at runtime. – Jesper Aug 29 '09 at 05:54
2
Override po = new Derived();
po.f();

You are accessing Override's own method even if the object is derived because as per scope rules, the private members of class are considered first, and as its written in scope of Override it is referencing the private f, and since its private its not overriden in Derived class at all, they will only override if method signature is same.

Derived po = new Derived();
po.f(); 

Thsi is the correct code which will call Derived's f

Akash Kava
  • 39,066
  • 20
  • 121
  • 167
0

The override of method has three conditions.child class must has the same name and parameters and returned value as its super class.But if both of the parameter and returned value are vary,so the override is not exist!even if the two method are different method!ok!like this:

public class Parent {
          public  int addV(int a,int b){
        int s;
        s = a + b;
        return s;
    }
}

class Child extends Parent{
    public  void  addV(){
       //do...something
    }
}

Eclipse will not talk error! because the method addV in class Child is different with the method addV in class Parent.As your instance!

thkala
  • 84,049
  • 23
  • 157
  • 201
  • Only method's name and list of args must be exactly the same. Return value of child could be subclass of parent's return value.And there are two more rules you have omitted. The method in child must be at least as accessible as the one in parent. And subclass cannot throw new checked exception or broader ones. – pkkoniec Oct 21 '16 at 17:48