I know my name doesnt conform the naming policy, but hey I'm Akshay Kiran and lost my login!!! can someone help me retrieve it?
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()");
}
}
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.
Cheers,<br />Jay<br /> <br />(SCJP 1.4)<br />Heights of great men were not achieved in one day, they were toiling day and night while their companions slept.
an island in the sun <br />with a language of many tongue?
MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
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 ]
"It's not enough that we do our best; sometimes we have to do<br />what's required."<br /> <br />-- Sir Winston Churchill
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?
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.
MCSD, SCJP, SCWCD, SCBCD, SCJD (in progress - URLybird 1.2.1)
an island in the sun <br />with a language of many tongue?
an island in the sun <br />with a language of many tongue?