• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Doubt in Mocktest

 
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Person {
private void say(String s){
System.out.println("I'm saying: " + s);

}
public static void main(String[] args) {
Person p = new Director();
p.say("Hello");
}
}
class Director extends Person {
public void say(String s){
System.out.println("I'm the director");
}
}
Here the output should be I'm the director because at run time the overiding method invokes the Directors's method.
But the answer is I'm sayinng Hello.Why???
 
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

say method not over-ridden due to private scope in base class
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
But private access modifier is not visible out the class.
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi seema,

Due to inheritance when you create Child, base Object will also be created.

Parent p = new Child();

p.method();

method() is called on parent Object when it is not over-ridden.
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

But private access modifier is not visible out the class.



Hi Seema,
But in your example, the private method say() is called in the same class where it was declared (Person)...
 
Seema Sharma
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
At runtime, say() method of Class Director will be called. If this method was not present in this class, then say() method of Class Person would have been called.(Since say() method in Class Person is having private as the access modifier, there should be no chance for say() method to be called.) Hence the output for the above should be "I'm the director".
Let me know where i am wrong in my understanding.

Regards,
Seema
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Seema,
Lets go step by step
a) Can I invoke say() in this example. The answer is Yes because as say() is marked private its visible only to class Person and we are accessing say method in main() which is a memeber of Person class. so yes i can access say method. But since its getting invoked in static method it must be invoked via a instance and thats what we are doing here createing a instance of Director and assigning it to "p".
b) Is say() inherited by class Director. The answer is NO as the method is marked private its not inherited.
c) Now is say() in class Director overriding say() in Person : The answer is NO because A method can be overridden by a subclass only if it can be inherited. There are many more rules for legal overrides. Method overriding rules
d) So now the big question is
Person p = new Director();
p.say() which say() will be invoked. As say method is not overriden in Director class and hence say() of Person class is invoked and hence the result
"I am saying hello" .
Hope this clears the confusion.
Thanks
Deepak
[ August 12, 2007: Message edited by: Deepak Jain ]
reply
    Bookmark Topic Watch Topic
  • New Topic