K
kevin
Hi all:
Sorry to bother you again
Still the question about inheritance,please see this codes below
first:
// codes start
class base{//a base class
// constructor
public base(){
System.out.println("base class construct");
}
// perform
public void perform(){
System.out.println("base class perform");
}
// destructor
public void finalize(){
System.out.println("base class destruct");
}
}
class subbase extends base{// derive from base
// constructor
public subbase(){
System.out.println("sub class construct");
}
// perform
public void perform(){
System.out.println("sub class perform");
}
// destructor
public void finalize(){
System.out.println("sub class destruct");
}
}
public class casting{// test casting class
// constructor
public casting(){
System.out.println("begin casting test");
}
public static void main(String args[]){
base father = new base();
subbase son = new subbase();
father.perform();
son.perform();
father = (base)son; // <1>
father.perform();
son = (subbase)father; // <2>
son.perform();
father = (base)((subbase)father); // <3>
father.perform();
}
}
// codes end
***************************************************
and the execution result is:
// begin
base class construct
base class construct
sub class construct
base class perform
sub class perform
sub class perform // <a>
sub class perform // <b>
sub class perform // <c>
// end
****************************************************
My question is: there are 3 castings in the codes(e.g. <1><2><3>),but
why they perform not as I like, I cast the subclass son to base class
at <1>, but at <a>,father.perform() don't print "base class perform",
the situation is similar in <2><a> and <3><c>,so, why?
****************************************************
And I am confused about how "casting" behave?
for example:
base class: sub class derive from base
+---------------+ +------------------------------+
| int nbase | | int nbase int nsub |
----------------- --------------------------------
| void fbase() | | void fbase() void fsub() |
+---------------+ +------------------------------+
so, if I declare:
base a = new base(); // a should contains nbase and fbase()
sub b = new sub(); // b should contains nbase,nsub and
fbase(),fsub()
base c;
sub d;
c = (base)b; // ? what c contains ? like a or like b
// that is to say if son class upcasting to father
class
// if the member belongs to son class should be
truncated?
d = (sub)c; // ? what d contains ? like a or like b
// that is to say if d get the members belongs to son
class
// automatically
// d == b ? i mean if equal in memory profile
*******************************************************
I am a little confused about how memory allocation changes when
upcasting
or downcasting ?
that's all
thank you!
kevin
Sorry to bother you again
Still the question about inheritance,please see this codes below
first:
// codes start
class base{//a base class
// constructor
public base(){
System.out.println("base class construct");
}
// perform
public void perform(){
System.out.println("base class perform");
}
// destructor
public void finalize(){
System.out.println("base class destruct");
}
}
class subbase extends base{// derive from base
// constructor
public subbase(){
System.out.println("sub class construct");
}
// perform
public void perform(){
System.out.println("sub class perform");
}
// destructor
public void finalize(){
System.out.println("sub class destruct");
}
}
public class casting{// test casting class
// constructor
public casting(){
System.out.println("begin casting test");
}
public static void main(String args[]){
base father = new base();
subbase son = new subbase();
father.perform();
son.perform();
father = (base)son; // <1>
father.perform();
son = (subbase)father; // <2>
son.perform();
father = (base)((subbase)father); // <3>
father.perform();
}
}
// codes end
***************************************************
and the execution result is:
// begin
base class construct
base class construct
sub class construct
base class perform
sub class perform
sub class perform // <a>
sub class perform // <b>
sub class perform // <c>
// end
****************************************************
My question is: there are 3 castings in the codes(e.g. <1><2><3>),but
why they perform not as I like, I cast the subclass son to base class
at <1>, but at <a>,father.perform() don't print "base class perform",
the situation is similar in <2><a> and <3><c>,so, why?
****************************************************
And I am confused about how "casting" behave?
for example:
base class: sub class derive from base
+---------------+ +------------------------------+
| int nbase | | int nbase int nsub |
----------------- --------------------------------
| void fbase() | | void fbase() void fsub() |
+---------------+ +------------------------------+
so, if I declare:
base a = new base(); // a should contains nbase and fbase()
sub b = new sub(); // b should contains nbase,nsub and
fbase(),fsub()
base c;
sub d;
c = (base)b; // ? what c contains ? like a or like b
// that is to say if son class upcasting to father
class
// if the member belongs to son class should be
truncated?
d = (sub)c; // ? what d contains ? like a or like b
// that is to say if d get the members belongs to son
class
// automatically
// d == b ? i mean if equal in memory profile
*******************************************************
I am a little confused about how memory allocation changes when
upcasting
or downcasting ?
that's all
thank you!
kevin