• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

clarification on why the modifier private changes output..

 
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If someone doesn't mind I'd like a little bit of clarification concerning a question from J@Whiz. I'm sorry if this topic has come up as of late and it's been beat to death. If someone can point to a post on this I'll check that. Anyway, I'm confused why declaring method1() in the Parent class causes it to be called vs the method1() in the Child class when the following code is run.

What is displayed when the following is executed?

result prints:
Parent's method 2
Parent's method 1
The explanation given for this is:
The code will compile without any error and also will not give any run time error. The variable p refers to the Child class object. The statement p.method2() on execution will first look for method2() in Child class. Since there is no method2() in child class, the method2() of Parent class will be invoked and thus "Parent's method2()" will be printed. Now from the method2() , there is a call to method1(). Please note that method1() of Parent class is private, because of which the same method (method1() of Parent class) will be invoked. Had this method(method1() of Parent class) been public/protected/friendly (default), Child's class method1() would be called.

I'm really confused about this last part concerning why it is different when the Parent method1() is declared private.
Thanks for any help.
Rick
 
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Becuase the method is private the child class doesn't
inherite the method. So, overriden method in the child class
doesn't work. Also, since it is private and it is not inhertied
there is no polymorphism. But you can still hide the parent's method within the chlid class. For example:
// this hide the parent class method1;
Child p = new Child();
p.method2();
 
Fei Ng
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh oh.... sorry, forgot something.
if you take away the private of private void method1()
in Parent class.
The method1() in Child will get invoked.
Hope this help.
 
Rick Reumann
Ranch Hand
Posts: 281
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much. That clears it all up. I appreciate it.
 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi FEI NG:
Could you explane why if removing private keyword -->method1 in Child class is called?
I think : because of static/dinamic binding method1() in parent class or child class was called
[b]Method with private/static are static binding
am I wrong?
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi lu
The thing is that private method's are not inherited i.e they can not be overridden. when there is no overriding so no quetion of dynamic binding.
so when u remove private, method become elegible for overriding and then if that method is overriden , in this case mathod1() , will be called of corresponding object.
CMIW
------------------
Regards
Ravish
[This message has been edited by ravish kumar (edited November 07, 2001).]
 
Fei Ng
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks ravish,
not inherited --> no dynamic binding.
if no inherited the compile check which method to use right?
not sure after that.
 
lu v thuan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ravish and FEI Ng:
If no dynamic binding why this code give compile time error

Note : This proves that when compling Java compiler checks the existence of method1() in Parent class
But if you remove Parent's method1() comment. It compliles sucessfully and when running method1 in Child is called even though in compile time the compiler checked if thod1 in class Parent
------->Dynamic Binding Exists

Could you please explane why method1 in class Child not in class Parent is called ?
[This message has been edited by lu v thuan (edited November 07, 2001).]
 
Fei Ng
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correct me if i am wrong..
The compiler has no way of knowing what object reference P is denothing. it knows the class of reference. And the Parent class doesnt have that menthod which would be a compiler error.
Little change on the code and it give the same error but clear.
public static void main(String args[]){
Parent p = new Child();
// change in here from p.method2(); to p.method1();
// compiler sees no method1 in p (Parent class)
p.method1();
}
And The acutal invoked method is determined by dynamic method lookup at runtime. I don't understand what you mean by "If no dinamic binding why this code give compile time error"
 
lu v thuan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi FEI NG:
This is corect:type checking
The compiler has no way of knowing what object reference P is denothing. it knows the class of reference. And the Parent class doesn't have that menthod which would be a compiler error.
This is dynamic binding
And The acutal invoked method is determined by dynamic method lookup at runtime.
Sorry A mistake
I don't understand what you mean by "If no dynamic binding why this code give compile time error"

Little change on the code and it give the same error but clear.

Complie time error because class member with private keyword is visible only in enclosing classThis time compiler knows the class of reference, method1 is private

ex: static bindingremove private keyword from Parent class.Using public static
method1() in Parentclass and Child class. This time method1() in
Parent class is called not in Child class
Note:private/static/final method use static binding

out put :
Parent's method 2
Parent's method 1
You see this time the actual object is of Child type
(Parent p = new Child()) but method1 in Child not called
method1 in Parent worked -->Static Binding
am I wrong?

 
Fei Ng
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you explane why if removing private keyword -->method1 in Child class is called?
I think : because of static/dinamic binding method1() in parent class or child class was called
[b]Method with private/static are static binding
If no dinamic binding why this code give compile time error
sorry.. didn't read your posts carefully! My mistake.
ah... static binding on static and private methods.
I thought u were say it gives compile time error becuase of
dynamic binding. My fault, You are right!!!
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I enjoy this qus

[This message has been edited by Hiep Nguyen (edited November 07, 2001).]
 
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
This was a good discussion. I learned what I had forgotten from C++ fundamentals :-)
As in C++, Java does dynamic binding. So, when we inherit child class from parent it replaces all the method pointers in parent part of the child (we can consider that child = parent area + new area defined by the child) to point to the child methods if the methods are overridden. if methods are not overridden it will point still the parent methods.
So, as in the original problem,
child has virtual method table like,
{ parent part:
{method2();
} ,
child part: { method1(); // newly defined by the child as it's not overridden
}
}
But still method2() pointing to method1() has pointer for parent's method1() which is logical. if we would have override method2() in child then it would have point to method1() of child :-)
So, parents method2() causes method1() of parent to be called instead of child's.
Thanks for refreshing my memory.
Maulin
 
Maulin Vasavada
Ranch Hand
Posts: 1873
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
just a clarification....
i said if we would have override method2() in child it would have pointed to method1(). here i assumed that we are calling method1() from child's method2() in the case we have defined it in child to override parent's method. if we dont call it it will not be called.
-maulin.
 
R K Singh
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by lu v thuan:
Hi ravish and FEI Ng:
If no dynamic binding why this code give compile time error
<pre>
class Parent{
/*public void method1()
{
System.out.println("Parent's method1()");
}*/
public void method2() {
System.out.println("Parent's method2()");
method1(); // U remove(comment) this line u won't get compile time error
}
}

class Child extends Parent{
public void method1(){
System.out.println("Child's method1()");
}

public static void main(String args[]){
Parent p = new Child();
p.method2();
}
}
</pre>
Note : This proves that when compling Java compiler checks the existence of method1() in Parent class

But if you remove Parent's method1() comment. It compliles sucessfully and when running method1 in Child is called even though in compile time the compiler checked if thod1 in class Parent
------->Dynamic Binding Exists

Could you please explane why method1 in class Child not in class Parent is called ?
[This message has been edited by lu v thuan (edited November 07, 2001).]


Hi lu
as I mention in comment that if you do not call method1() in class Parent then you won't get any compile error as compilier will find that yes method1() exist in class Parent.(This will true even if you comment out class Child). You tell me yourself ... when you are calling method1(), is it declared/defined? No, it is not declared and defined(as you have commetned it) so how can compiler assume that u are going to declare and define method1() in subclass , so it gives u error.
When you remove the comment, compiler finds method1() and compiles. Runtime it checks first in the object, then in refernce, if it finds in object then it execute that method, else it will execute refrence's method.
NOTE: This is called dynamic binding.
HTH
CMIW
------------------
Regards
Ravish
[This message has been edited by ravish kumar (edited November 07, 2001).]
[This message has been edited by ravish kumar (edited November 07, 2001).]
 
lu v thuan
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ravish :
as I mention in comment that if you do not call method1() in class Parent then you won't get any compile error as compilier will find that yes method1() exist in class Parent.(This will true even if you comment out class Child). You tell me yourself ... when you are calling method1(), is it declared/defined? No, it is not declared and defined(as you have commetned it) so how can compiler assume that u are going to declare and define method1() in subclass , so it gives u error.
When you remove the comment, compiler finds method1() and compiles. Runtime it checks first in the object, then in refernce, if it finds in object then it execute that method, else it will execute refrence's method.
NOTE: This is called dynamic binding.
You're right in this case.

But I know that method with private/static/final modifier is static binding
Note:using public static method1 in Parent and Child class .You will get Parent's method1 is called even though the actual object is an object of Child class
Ex:
The out put :
Parent's method2
Parent's method1
--------->static method is static binding
You see the actual object is Child object -->if it was dynamic binding the method1 in Child class would be called
am i wrong?
 
Fei Ng
Ranch Hand
Posts: 1246
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi lu v thuan, ravish kumar
Ravish didn't say you , lu v thuan, r wrong.
Like what i did before, we were explaining Dynamic Binding.
The reason for the output becuase there was no Dynamic Binding.
result prints:
Parent's method 2
Parent's method 1
Becuase that was the question and since the person who asked the
question thought that becuase of Dynamic Binding the output
should be Parent method 2 and Child method 1.
ravish kumar showed there wasn't Dynamic Binding.
You, lu v thuan, showed there was Staic binding.
Both of you are right!! It is just a misunderstanding.
Am i wrong?? Am i wrong on this??!!!
hehehhehe....
 
R K Singh
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Fei Ng and lu
so sweet of u.
I was justfying myself, why compiler is giving error. and I think everyone here is trying to answer himself only.
and yes lu you are right private/static/final modifiers are for static binding as they can not be overridden. if you try to override then compiler will give you error.
thats all.... Thanks lu ... you refershed my fundas abt overriding
CMIW

------------------
Regards
Ravish
 
It's a beautiful day in this neighborhood - Fred Rogers. Tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic