the fact is u r not overridding the method1() since u declared it as private in the super class.when u r not overriding a method , it willbe invoked by the reference not by the objects. method1() in subclass is totally new to that class. so, when u r callig that method it will call the super class method. Parent p = new Child(); p.method2(); take my lock test : www.geocities.com/krishna4java/index.html
Clearly the parent's method2() is being called (since the Child doesn't have a method2) but inside that method is the call of method1(). Since no object is used, the current instance (ie, "this") is used. If you print out the type of "this" with: Object o = this; System.out.println("this is object of type: " + o.getClass().getName()); Then it prints out it is of type "Child", not parent. So then why isn't the Child's method1() called? Krishna S., you said that it was because the Parent's method is private. Why would that be enough to call it instead of calling the child's method? Since the object is a Child, shouldn't the Child's method be called? (I did try this out, and the behavior does match with what you said it would, but I don't understand it.) Tony
okay Tony i'll try to explain the issue here... think this way, 1. Parent has two methods, method1 and method2 and method2 is calling method1 which is private 2. this means method2 has a pointer to method1 in Parent 3. child also has method1 BUT here method1 is "private" in Parent hence doesn't get "inherited" to child AND THATS WHY child has two copies of the method1 (one from the parent and one child wrote). 4. this implies that parent's method2 is still pointing to Parent's method1 (as method1 didn get overridden) BUT if u make method1 public in Parent then child inherits that method and as child also re-defines method1 it "overrides" the method1 and so method2 in Parent will call method1 in child instead of parent. u get that? if i try to put it in some dummy real scenario, 1. consider person C and his/her parent P where both of them are business man 2. P offers one service called "deal with government for some matter" (public method) and helds private practices (preparing paper work etc) to complete the public service 3. C decides to offer "preparation of papers" as service (public method) BUT also announces that he CAN "deal with government for some matter" as he can delegate the service to his parent P (and client never knows about that trick). Here, C doesn't know that what P does to perform public service of dealing with goverment (of course the business secret) 4. Now, when client calls C's service that is dealing with the government, C will call P and requests him to do the service Now, at this step what do we expect? do we expect P to use C's service for preparing paper work and all for performing the "dealing with government"? NO. why? because P might be doing something which "he only knows" and he can't delegate it to C because C's methodology is publicly known...you see...so P still uses "his private" techniques to perform the service rather than asking C to do it... i don't mean to point to any ethical issues here but i just tried to come up with an example..