I came across this question where the OP is asking for some inheritance concept. Here is the overview:
One.java
public class One { private void run() { System.out.println("one"); } public void start() { this.run(); } }
Two.java
public class Two extends One { public void run() { System.out.println("two"); } }
Main.java
public class Main { public static void main(String[] args) { Two t = new Two(); t.start(); } }
Output: one
But if I declare
public
the run method in One class I get: two.
One of user answered it and the OP accepted it. Part of the answer:
The run method in Two will not be overriding the private method in One, because it is private and inaccessible to any class outside One.
But I had some doubts about that. t
contains object of Two
but Two
does not contain start()
so it it uses One's
start method which has a body this.run()
. Here this
is current calling object Two's
object it's like new Two().run()
so why didn't it display two
rather than one
?
The above problem are my doubts. I already tried to resolve my doubts by posting a comment but the answerer didn't reply. So what should I do in this case? Should I create a new question or not? I want to clear up my confusion about this, but I don't want to create duplicate question.