Results 1 to 3 of 3
- 10-10-2011, 09:09 PM #1
Member
- Join Date
- Oct 2011
- Posts
- 3
- Rep Power
- 0
constructor vs overridable method
Hi,
I have a question about overridable method call in constructor.
For example:
class A {
public A() {
fun();
prv_fun();
}
public void fun() {
System.out.println("A");
}
private void prv_fun() {
System.out.println("PRV A");
}
}
public class B extends A {
public B() {
fun();
}
public void fun() {
System.out.println("B");
}
public static void main(String[] args) {
B obj = new B();
}
}
the result is:
B
PRV_A
B
Why does the constructor of class A invoke class B fun() method and class A prv_fun() private method ?
Constructor A bytecode:
public <init>()V
L0
LINENUMBER 2 L0
ALOAD 0
INVOKESPECIAL java/lang/Object.<init>()V
L1
LINENUMBER 3 L1
ALOAD 0
INVOKEVIRTUAL A.fun()V
L2
LINENUMBER 4 L2
ALOAD 0
INVOKESPECIAL A.prv_fun()V
L3
LINENUMBER 5 L3
RETURN
L4
LOCALVARIABLE this LA; L0 L4 0
MAXSTACK = 1
MAXLOCALS = 1
and it contains command INVOKEVIRTUAL A.fun()V,
so why does the constructor of class A invoke B.fun() ?
Thanks
-
Re: constructor vs overridable method
- 10-10-2011, 10:26 PM #3
Member
- Join Date
- Oct 2011
- Posts
- 3
- Rep Power
- 0
Similar Threads
-
Final variable-constructor method.
By D4rkNrG in forum New To JavaReplies: 9Last Post: 10-13-2010, 11:18 AM -
Method and Constructor
By swikar.java in forum New To JavaReplies: 7Last Post: 10-21-2008, 02:41 PM -
Method & Constructor
By swikar.java in forum New To JavaReplies: 4Last Post: 10-21-2008, 01:31 PM -
Differences between constructor and method
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 08:53 AM -
Java constructor method
By carl in forum New To JavaReplies: 2Last Post: 07-31-2007, 02:15 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks