Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Upward Casting _ doubt

 
rchopp
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class Base{
int i = 99 ;
public void amethod(){
System.out.println("Base.amethod()") ;
}
Base(){
amethod() ;
}
}
public class RType extends Base {
int i = -1 ;
public static void main(String arg[]) {
Base b = new RType() ;
System.out.println(b.i) ;
//b.amethod() ;
}
public void amethod(){
System.out.println("RType.amethod()") ;
}
}
The output of the above code is :
RType.amethod()
99
How is the first RType.amethod() getting printed ?
Thanks,
Ravi.
 
asim wagan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is happening because you are overriding the class Base's method in RType. Now this is a rule in java that interpreter detects the first narrowest encapsulating class for the object, though it is declared higher up in hirarchy.
In your case:
[CODE]
class Base{
int i = 99 ;
public void amethod(){
System.out.println("Base.amethod()") ;
}
Base(){
amethod() ;
}
}

public class RType extends Base {
int i = -1 ;
public static void main(String arg[]) {
Base b = new RType() ; //<---RType is declared higher up in hirarchy
// When the Base class constructor is
//called it calls the most nearest method
//to RType and in this case it is in
//RType so that will be called
System.out.println(b.i) ;
// b.amethod() ;
}
public void amethod(){
System.out.println("RType.amethod()") ;
}
}
[CODE]
 
shailesh sonavadekar
Ranch Hand
Posts: 1874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
welcome to JavaRanch.
May I remind you PROPER NAMES ARE NOW REQUIRED!!
Please Read the JavaRanch naming policy for more details. http://www.javaranch.com/name.jsp
Javaranch appreciates your cooperation to comply with the official naming policy.
SHAILESH
 
rchopp
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
asim,
Thanks for the reply. But my doubt was that when u r invoking
Base b = new RType() , why the base class constructor is called. Is it not the RType() constructor that is called ?
sailesh,
Where can I change the name in my profile. guide me.
Thanks,
Ravi.
 
Cindy Glass
"The Hood"
Sheriff
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As soon as you said "Base b" the Base class was loaded and i was set to 99 (note that this is NOT done inside the constructor). The space in memory was allocated and initialized. Then you said "= new RType()", so an object of type RType was placed into the already loaded Base class. That action does not change the fact that i was set to 99 at load time. All of the class variables of the declared class (Base) will be kept but the methods of the instance object (RType) will over-ride the methods of the declared class at run time.
The RType constructor was called and it printed the value of i as 99.
[This message has been edited by Cindy Glass (edited November 18, 2000).]
[This message has been edited by Cindy Glass (edited November 18, 2000).]
 
shailesh sonavadekar
Ranch Hand
Posts: 1874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much Ravi for compling with the naming policy of the site & help maintain the decorum of the site. These things will make the environment of the forum more richer. You & all other javaranchers will benefit from these things.
Regarding registeration , you will have to reregister with proper name. Once , you are through , you let me know. The previous account will be disabled.
Thanks.
YOUR FRIENDLY BARTENDER
SHAILESH
[This message has been edited by shailesh sonavadekar (edited November 18, 2000).]
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Whenever a new instance is created Java calls the default constructors of all the superclasses, unless a specific this() or a specific super() reference is used to another superclass constructor. (See JLS §12.5).
When you created the new instance using:

The following happened:

  1. memory is allocated for all instance variables in RType and in Base
  2. the instance variables in RType and Base are assigned
  3. the no-arg RType constructor is invoked, as none exists the no-arg Base constructor is invoked using super()
  4. any instance initializers in Base are executed
  5. the remainder of the Base() constructor body is executed. This includes a method call to amethod(). The JVM uses dynamic binding, so RType is checked for an overridding method. As one exists, the amethod() in RType is executed.
  6. any instance initializers in RType are executed

  7. If you change the code to:

    The output will change to:

    In this case, the only output difference is the instance variable i. Java does not use dynamic binding on fields; instead it uses the declared type at compile time.
    In the first example, b was declared as type Base so the value of i in Base was used.
    In the second example, b is declared as type RType and the value of i in RType is used.
    Hope that helps.

    ------------------
    Jane
    The cure for boredom is curiosity.
    There is no cure for curiosity.
    -- Dorothy Parker
 
asim wagan
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you have studied the code comments which i have inserted it would not have been difficult for you to understand that java calls all the constructors in superclasses.
 
rchopp
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Cindy,Jane,asim
I'm delighted to see very good responses. Now, I'm very much clear about that. The explanations were elaborate.
Thank u very much.
As Shailesh asked, I'll change my username, and see me as Ravikiran Choppalli
 
shailesh sonavadekar
Ranch Hand
Posts: 1874
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much , Ravikiran. Why don't you jump in & start with your new register name , rather than rchopp.
SHAILESH.
 
Dipali Desai
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi
i tried out ur code .
See what happens is at:
Base b = new RType() ;
since u have specified in ur code Base b , it calls the constructor of Base class. Now within the constructor u have given a call to the method amethod().
since method calling is determined by the Dynamic object type & in this case the RHS of the Base b = new RType(); i.e it calls the amethod function in the RType class.
NOTE: if within the constructor of Base class u comment the call to amethod(), it won't print RType.amethod() to the o/p.
Now , it prints 99 , when u say b.i , i.e b'cause the variable calling is determined by the Static object i.e Base b , the object of Base , hence it prints the value of i initialised in Base.
I hope the reply i gave u is write. if i am wrong can some one correct it.
Dipali
Originally posted by rchopp:
class Base{
int i = 99 ;
public void amethod(){
System.out.println("Base.amethod()") ;
}
Base(){
amethod() ;
}
}
public class RType extends Base {
int i = -1 ;
public static void main(String arg[]) {
Base b = new RType() ;
System.out.println(b.i) ;
//b.amethod() ;
}
public void amethod(){
System.out.println("RType.amethod()") ;
}
}
The output of the above code is :
RType.amethod()
99
How is the first RType.amethod() getting printed ?
Thanks,
Ravi.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic