Problem solving method override

Category: Java SE
 
a415499779
2009-09-04 06:21:34

Sponsored Links
class Super {
private void print () {
System.out.println ("HELLO");
}
public void fun ( ) {
this.print ();
}
}
class Sub extends Super {
void print () {
System.out.println ("WWW");
}
}
public class TestExtends {
public static void main (String arg []) {
new Sub (). fun ();
}
}

answer is: HELLO

this.print () method is not originally looking for first print () method, namely:
void print () {
System.out.println ("WWW");
} way to do this?
why go looking for it the parent class?

Sponsored Links

haixing11
2009-09-04 06:58:59
because the parent class print () method is private, can not be overwritten.
qqww2222
2009-09-04 07:18:04


because you wrote two print () method does not constitute a rewriting relation between,
parent class print () method is private, and your father's things you can take private modify the right, the two are not rewriting relation
So before you call the parent class fun () method when the method is called internal default parent class ; private void print ()
he would know the parent class print ()
subclass if you want to call print (), modify the parent class access
subclass access below the parent class
k1234567890000
2009-09-04 07:52:41
overridden in a subclass access than the parent class. .
sunyong20081009
2009-09-04 08:20:04
1, you call fun Sub class no;
1, Super fun in Sub able to find it in print? Can not!
2, you say looking at the class, the settlement of the class to do? ? ?
ZYX5A1
2009-09-04 08:33:08

where this class is a subclass. . .
K001K
2009-09-04 08:35:43
I understand, with access to the private property of a parent class truth
andrew_cs_cn
2009-09-04 08:50:57
just read a similar problem and this road, this problem becomes more a private
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.