• 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

the same instance has different method?

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
//Parent.java
class Parent{
private void method(){
System.out.println("Parent");
}
public static void main(String args[]){
Parent p=new Parent();
Child c=new Child();
Parent d;
d=c;
p.method();
c.method();
d.method();
System.out.println("c="+c+" d="+d);
}
}
class Child extends Parent{
public String method(){
System.out.println("Child");
return "result";
}
}
===========the output is:
Parent
Child
Parent
c=Child@256a7c d=Child@256a7c
============
The last line shows that "c" and "d" point to
the same instance.but why invoke method "method()"
will output different result?
Thanks in advance!
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
method() is private in Parent, so it is not inherited in Child. Since it's not inherited, method() is not overridden in Child.
The compiler makes a static link to method() at compile time, based on the type of the reference you are using to invoke the method.

Rob
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
in case of method calls:
call is made against the object type (right hand side)
in case of variables:
call is made against reference type (left hand side)
so
class T1{
public String s = "T1";
public void tst(){ System.out.println(s);}
}
class T2 extends T1{
public String s = "T2";
public void tst(){ System.out.println(s);}

public static void main(String [] argv){
T1 LeftSide = new T2();
T1.tst(); // will print T2
System.out.println(T1.s); // will print T1
}
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Tarik Makota:
in case of method calls:
public static void main(String [] argv){
T1 LeftSide = new T2();
T1.tst(); // will print T2
System.out.println(T1.s); // will print T1
}


You meant to write
T1 leftSide = new T2(); //please use correct style for identifiers!!
leftSide.tst();//will print T2;
System.out.println(leftSide.s);
these members aren't static, so you can't access them via the class name. Also, although your statement is true, it doesn't deal with the original question


Rob
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the type of object reference determines what variables are accessed, and the underlying object determines what methods are accesed, why doesn't the child's method get called when you say:

d = c;
d.method();
???

Wouldn't that mean that for d, the Parent variables are accessed, and Child's methods are used?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
/*
class Parent{
private void method(){
System.out.println("Parent");
}
public static void main(String args[]){
Parent p=new Parent();
Child c=new Child();
Parent d;
d=c;
p.method();
c.method();
d.method();
System.out.println("c="+c+" d="+d);
}
}
class Child extends Parent{
public String method(){
System.out.println("Child");
return "result";
}
}
*/
Can someone explain how we can call c.method() without assigning return value (String) to some variable?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Mike Beaty:
If the type of object reference determines what variables are accessed, and the underlying object determines what methods are accesed, why doesn't the child's method get called when you say:

d = c;
d.method();
???

Wouldn't that mean that for d, the Parent variables are accessed, and Child's methods are used?



Yes, this is true, BUT, look at the Parent's declaration of method(). It is PRIVATE .
That means, it is NOT inherited by any subclasses. When the compiler is compiling this line of code:
d.method();
It first looks at the reference type of d to see if it really has a method named method() that is accessible. Now, because you are calling this method within the class (main is in the class, and so has access to private members) it is legal for you to access private members of d. BUT, because the type of d is a Parent, and the method() is private, the compiler KNOWS that this method cannot be overridden in any possible sublcasses of Parent, since it's private. SO, it doesn't have to create bytecode for runtime binding; it creates a fixed, static call to the method() in the Parent class.
IF this method had not been private, then it would have been inherited by subclasses of Parent, so the compiler would have had to create bytecode to dynamically bind this method call at runtime. But that is not the situation here.

Rob
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Abraham,
in java it is legal to ignore the return value of a method.
int foo()
{
return 5;
}
You can write:
foo()
or
int anInt = foo();
Both are ok.

Rob
 
If you two don't stop this rough-housing somebody is going to end up crying. Sit down and read this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic