• 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

Reg. invoking private methods

 
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Following is an example from JWhizTrail Version :
class Parent
{
private void method1()
{
System.out.println("Parent's method1()");
}
public void method2()
{
System.out.println("Parent's method2()");
method1();
}
}
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();
}
}
OUTPUT :
Parent's method2()
Parent's method1()

I don't understand as to why calling method1() from method2()
in the Parent class will call the private method in the Parent class ?
The explanation states :
<quote>
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
</quote>
Now is this some sort of rule ? I am not getting the reason behind
it !
 
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you invoke Parent's method from Child, it was the Parent inside the Child that was invoking that method. And hence, any private methods inside the Parent method will be called.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since method1() in the Parent() class is private, the method1() in the Child class is not overriding method1() in Parent class. Thus, even if the actual Object is a Child, method1() in parent still gets called from method2().
The explanation states that had method1()been public.. then it would have been the method1() in Child that would have been executed, this is because then, the method1() in Child will be overriding the method1() in Parent and so the actual object's method will be invoked.
I hope this helps.
 
Ranch Hand
Posts: 153
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well said Cherry Sta. Romana,
I think the explanation is apt and clears the funda.
Better watch for such traps in exam.

tvs sundaram
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Everybody,
I think Cherry's explanation lacks the main point,thus, I am adding what i understood.
Child is inheriting parent so child is getting method2 and since method 1 is private it is not being inherited.Now child object is being assigned to parent reference variable, which is absolutely permissible as we all know.This is dynamic dispatch where the type is resolved at run time.Now when we are calling method2 it searches for method1 and finds that method1 is private thus it takes method1 from the base class only(remember this is always the case when member is private).It is irrelevant even if you override the member with the same signature.
Try this
class Parent{
private void method1(){
System.out.println("Parent's method1()");
}
public void method2(){
System.out.println("Parent's method2()");
method1();
}
}
public class Child extends Parent{
private void method1(){
System.out.println("Child's method1()");
}
public static void main(String args[]){
Parent p = new Child();
p.method2();
}
}
here 'private void method1()' is overriden in class child.Then too the
result is same:
Parent's method2
Parent's method1
After trying this example if you look at explanation quoted by Angela
"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""
This will make it clear...
But when the method is of some other signature run time interpreter looks for the method first in the list of derived class members and then in the list of base class members.
Hope this will clear all the doubts.
And thanks for bringing such interesting topic Angela(It added more understanding to my concepts).
Bindesh Vijayan

 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good Discussion
I just wanted to add a small thought...
Basically I believe you can never override a private method
Each private method is closely associated to the class itself
So in this case, like Bindesh pointed out, whatever be the access
modifier of the overriding method, if the parent method is private, then its better be private and not be overridden. The output of the program confirms it
Thank you
 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ragu,
Thanks Ragu.
Bindesh Vijayan
 
Angela Narain
Ranch Hand
Posts: 327
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,
That was a good discussion .
I wanted to add to Bindesh's expl., that isn't it the same
rule that whenever a method is declared as "static" in the Base class
and if we override this method, invoking the method
like ( Base b = new Derived(); ) will always cause the Base class method to be invoked.
So if we change method1() in Base class as :

public static method1(){
System.out.println("Parent's method1()");
}

And method1() in Child class as :

public static method1(){
System.out.println("Child's method1()");
}

And the following code in the main() method
...
Parent p = new Child();
p.method1();

The output will be :
System.out.println("Parent's method1()");

Correct me if wrong .
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
You cannot 'override' private or static methods as they are not inherited. The compiler, on encountering the 'private' or 'static' keyword does not defer method resolution until runtime. It simply writes out the bytecode to invoke the given method...it does not create bytecode that will look for a possible overriding method in a subclass.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited August 24, 2001).]
 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jane.
Actually I too reached the same conclusion,but was not confirm.
And thanks everybody.
 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jane,
Initially I shared your idea,but when I looked carefully, here is what I found:
You said static methods are not inherited ,I tried this:
class stat{
static void Mystat(){
System.out.println("Mystat from stat");
}}
class statDemo extends stat{
public static void main(String[] arg){
Mystat();}}
Result:MyStat from stat
This means it did inherit,isn't it ?(doubts are always there).
Now what I think is,firstly, when the member is private 'the parent method is taken in the derived', and is not overriden(you should'nt as you said).
Secondly, when the member is static I believe, Dynamic Dispatch does'nt work, may be bcoz it is class method and not instance method.The reason why Iam saying is bcoz of this
class Base{
static void BaseStat(){
System.out.println("Base.");
} }
class Gbase extends Base{
static void BaseStat(){
System.out.println("Gbase.");
}}

class Dbase extends Gbase{
static void BaseStat(){
System.out.println("Dbase.");
}}
public class Derived{
public static void main(String[] arg){
Base b1=new Base();
System.out.println("Base b1=new Base():");
b1.BaseStat();
b1=new Gbase();
System.out.println("Base b1=new Gbase():");
b1.BaseStat();
b1=new Dbase();
System.out.println("Base b1=new Dbase():");
b1.BaseStat();
}}
RESULT:
Base b1=new Base():
Base.
Base b1=new Gbase():
Base.
Base b1=new Dbase():
Base.
Please correct me if Iam wrong.
Thanks
 
Ranch Hand
Posts: 356
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bindesh,
In the example below, you are trying to call a method you have not defined so the compiler looks for that method in the super class and when it find it, it runs that method, try to make that method in stat class private and see through the compiler error statement.
============================================================
class stat{
static void Mystat(){
System.out.println("Mystat from stat");
}}
class statDemo extends stat{
public static void main(String[] arg){
Mystat();}}
Result:MyStat from stat
==============================================================
In the second example, you have proved yourself that static method cant be overriden, try making the methods non-static and see how overridding works.
==============================================================
class Base{
static void BaseStat(){
System.out.println("Base.");
} }
class Gbase extends Base{
static void BaseStat(){
System.out.println("Gbase.");
}}

class Dbase extends Gbase{
static void BaseStat(){
System.out.println("Dbase.");
}}
public class Derived{
public static void main(String[] arg){
Base b1=new Base();
System.out.println("Base b1=new Base():");
b1.BaseStat();
b1=new Gbase();
System.out.println("Base b1=new Gbase():");
b1.BaseStat();
b1=new Dbase();
System.out.println("Base b1=new Dbase():");
b1.BaseStat();
}}
RESULT:
Base b1=new Base():
Base.
Base b1=new Gbase():
Base.
Base b1=new Dbase():
Base.
===============================================================
Jane, waiting for your input, please correct me if Iam wrong.
Thanks,
--Farooq.

 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Farooq,
I want to ask you a question,I am quoting you
"you have not defined so the compiler looks for that method in the super class and when it find it, it runs that method," ...shoud'nt we call it inheritance,since super is being inherited by derived. An about private method, whats the big deal about it, I know private methods are not iherited whether it is java or c++.
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bindesh,
A 'static' method or field belongs to the class. Which means it is available to every object created using the class. It is not something an instance 'inherits'.
Imagine you're an electrical appliance that can be connected to a cable system. Every such electrical appliance has a behaviour that lets it access the cable system BUT it doesn't inherit the cable system itself.
Something similar happens with a class. All the instances have the means to access the class data and behaviours but they don't actually 'inherit' them. They don't carry the class internals around as part of themselves; they just have the inherent ability to access them.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
[This message has been edited by Jane Griscti (edited August 26, 2001).]
[This message has been edited by Jane Griscti (edited August 26, 2001).]
 
Bindesh Vijayan
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thamks Jane.
I was eagerly waiting for a reply.I got the concept.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic