The Child class does not have a method2. SO when p.method2 is called the inherited method2 from the parent will be called.
If you modify the code like this-
Then the Child method2 will be called.
Hope you are clear.
Joined: Jul 28, 2005
hi swapna ...
thats true ..since there is no method2 in the child class its parent class method method2 is called ..after that its there method1...my doubt is why is it calling parent class method1.....inspite of instance being that of child class...
thanks & regards
Joined: Jul 26, 2005
Because Child does not have method2 Parent method2 is called->p.method2();
I dont know the reason why but I coud see that if you change the access modifier of the parent class method1 to public from private then the child class's method1 is called.I suppose somebody can help us out in this
"NewToRanch Forum" Please read our JavaRanch Naming Policy and change your displayed name to comply with it. We require a displayed name in the format <first name><space><family name>, preferably your real names.
As it stands your displayed name will mean your account will be disabled in the near future.
Because the method Parent.method1() is private, the method Child.method1() does not override it. That is, there is no polymorphism involved for method1(). Therefore, in the method Parent.method2() there is only the method Parent.method1() available for it to call. [ September 02, 2005: Message edited by: Barry Gaunt ]
i dont see the reason as to why someone will make a super class method private and have the same method in the subclass(making a method private--no polymorphism) so there is no overriding !!! so if i say super_class obj = new sub_class(); //method - private in super_class //method - public in sub_class //here the compiler intervenes saying unknown method method() //method private in super_class obj.method();
making the method abstract would make more sense
Joined: Aug 03, 2002
Originally posted by Simon Roy James: i dont see the reason as to why someone will make a super class method private and have the same method in the subclass(making a method private--no polymorphism) so there is no overriding !!!
That's the point Roy. Part of studying for the SCJP certification is to learn to recognize these cases and to reason why the code does not work as expected or stated. [ September 04, 2005: Message edited by: Barry Gaunt ]
Joined: Jul 04, 2005
I am using ver 1.4.2
Joined: Aug 03, 2002
Originally posted by sameer inamdar: I am using ver 1.4.2
And what is the error message you get from your JVM?