• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

crack this quick  RSS feed

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Consider this code from jchq by Marcus Green

guess what would be the output.

RType.amethod()
99
RType.amethod()

completely baffled me.
i thought it would be:

99
RType.amethod

so i just change the declaration Base b= new RType(); to RType b= new RType();
output:
RType.amethod()
-1 //an obvious and anticipated change here
RType.amethod()

can anyone be kind enough to tell me why or how the first RType.amethod() appreared.

here's the javap of the first piece of code


!!! I'm beginning to doubt my capabilities now!

( tags added and improved formatting)
[ August 27, 2005: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by akshay again:
Consider this code from jchq by Marcus Green

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 argv[]){
Base b = new RType();
System.out.println(b.i);
b.amethod();
}
public void amethod(){
System.out.println("RType.amethod()");
}
}



In the above code method amethod() is overidden in the child class RType . In the line Base b = new RType(); you are assigning RType object to the reference b which is of type Base. However, since the method is overidden at run time the amethod of RType is called because the contents of b reference are that of RType.

Remember simple rule,

If the method is overidden to find out which method will be called look at the contents of the reference and one more thing this rule applies only to overidden methods and not variables, because variables are not overidden.



Hope that explains your doubt.
[ August 26, 2005: Message edited by: Jay Pawar ]
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi aksay,
Class Base has been explicitly initialised with a default constructor which contains the amethod() method . The RType class has an implicit default constructor which automatically overrides the super class constructor.When Base b = new RType() is run in the main method "new RType()" is overridng the superclass constuctor , so amethod() is being called , but becuase Base b references an object of RType,the amethod in RType is called hence the first line of output.Heres a simple program to demonstrate.

[ August 26, 2005: Message edited by: Daniel .J.Hyslop ]
 
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When an instance method is invoked, it uses the class of the current object.
Whereas when you access a method's fields or static methods, it uses the class of the type declaration


therefore

Super super = new Sub();
System.out.println(super.instanceVarible) <---- prints super's instance variable
System.out.println(super.staticMethod()) <-- prints super's static method
System.out.println(super.nonStaticMethod()) <-- prints sub's non static method
 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Daniel .J.Hyslop:
Hi aksay,
Class Base has been explicitly initialised with a default constructor which contains the amethod() method . The RType class has an implicit default constructor which automatically overrides the super class constructor.When Base b = new RType() is run in the main method "new RType()" is overridng the superclass constuctor , so amethod() is being called , but becuase Base b references an object of RType,the amethod in RType is called hence the first line of output.Heres a simple program to demonstrate.


[ August 26, 2005: Message edited by: Daniel .J.Hyslop ]



Yeah I think I got what you're saying. But tell me why the constructor of the superclass is being called?

for example, i changed R r= new C(); to C r=new C(); and even now the constructor to the base class is being called.

My doubt was not in the overriding, I understand that, instead my problem was why the constructor of the superclass was being called even when I was creating an object of the subclass. To clarify things further, I even put a constructor in C class( which was quite unecessary) and even then, the constructor to the superclass was being called. This is my problem.

Maybe I had overlooked it so long, but does the constructor of a subclass call the constructors of all the superclasses? this is something new, I would be thankful if someone addressed this issue at length.

thanks to all of you who've tried to help
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty sure that a subclass will default to running its superclass's constructor when instantiated. But I believe this can be avoided by making the superclass's constructor private. Someone correct me if this is wrong.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Originally posted by Akshay Kiran:
[CODE]Maybe I had overlooked it so long, but does the constructor of a subclass call the constructors of all the superclasses?



Correct. For the default () contructor, the constructer of the superclasses execute first, before the constructer of the class.
 
Richard Green
Ranch Hand
Posts: 536
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I'm pretty sure that a subclass will default to running its superclass's constructor when instantiated. But I believe this can be avoided by making the superclass's constructor private. Someone correct me if this is wrong.


your subclass won't compile in that case
 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi akshay,
When using inheretence you create a class heirachy each subclass has one superclass >When the program is called from the main method ,in constuctor terms the first one to execute is the superclass .each subclass constructor contains an implicit call to super()(if there are no explicit calls) . so when you changed the object initialisation to c r = new c()the subclasses constructor is calling super()implicitly. if you have a heirachy of four classes one extending the next the superclass will be the first constructor to be called then each constuctor in turn will be called from top to bottom each containing an implicit call to super();
 
Karthik Hariharan
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so is there no way to prevent the subclass from calling the super class constructor first? I thought there was a way to do this.
 
Daniel .J.Hyslop
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi karthik,
An initialisation block is executed before a constructor .An initialisation block is a block of code that gets executed before an instance of a class is created.So therefore it will be executed before a constructor , the constuctor will be executed next. A static block is only executed once when the class is loaded into the jvm and can be used to access static members and instance variables of a class .A non static block is initialised every time an instance of a class is created this also executes before the constructor of a class
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!