Overriding tactics in Java is very different from C++ as methods by default in Java can be overridden unlike C++. In C++, the concept of overriding functions are handled by Virtual Table, VTable. Whereas in Java there is some other concept. Before going into the depth, let's see some of the basic things which one should need to know before making their hands dirty in overriding concept.
Here is a Simple HelloWorld Program:
class Hello {
public static void main(String[] args)
{
System.out.println("Hello Bloggers!");
}
}
Lets see what the bytecode is generating, javap -c Hello
Compiled from "Hello.java"
class Hello extends java.lang.Object{
Hello();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."":()V
4: returnpublic static void main(java.lang.String[]);
Code:
0: new #2; //class Hello
3: dup
4: invokespecial #3; //Method "":()V
7: astore_1
8: getstatic #4; //Field java/lang/System.out:Ljava/io/PrintStream;
11: ldc #5; //String Hello it is
13: invokevirtual #6; //Method java/io/PrintStream.println:(Ljava/lang/Str
ing;)V
16: return
}
Have a look on these lines:
1: invokespecial #1; //Method java/lang/Object."":()V
4: invokespecial #3; //Method "":()V
13: invokevirtual #6; //Method java/io/PrintStream.println:(Ljava/lang/Str
ing;)V
These are the lines related to method invocation. So what the heck is this invokespecial and invokevirtual ? Actually JVM used 4 different kinds of instructions for method invocation those are :
- invokevirtual - This is for instance method like System.out.println("Hello Bloggers!") here.
- invokestatic - This is for class methods.
- invokespecial - This is for special things. It is used when
- call , instance initialization.
- super call, when you will call something from super.method
- private methods. As private methods can't be overridden so we need to put this in a special category.
- invokeinterface - invoking instance method with interface reference(Soon we will see the example)
Now we are very clear that why invokespecial has been used at #1 and #3 whereas invokevirtual at #6. Ok, lets write some code which can see the usages of all four.
interface interfaceForHello {
public void noUse();
}class Hello implements interfaceForHello {
public void noUse() {
System.out.println("No use");
}
public static void staticMethod()
{
System.out.println("Static method");
}
public static void main(String[] args)
{
interfaceForHello iface = new Hello();
iface.noUse();
Hello.staticMethod();
System.out.println("Hello Bloggers ! ");
}
}
And here goes the javap -a Hello:
Compiled from "Hello.java"
class Hello extends java.lang.Object implements interfaceForHello{
Hello();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."":()V
4: returnpublic void noUse();
Code:
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #3; //String No use
5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: returnpublic static void staticMethod();
Code:
0: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
3: ldc #5; //String Static method
5: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
8: returnpublic static void main(java.lang.String[]);
Code:
0: new #6; //class Hello
3: dup
4: invokespecial #7; //Method "":()V
7: astore_1
8: aload_1
9: invokeinterface #8, 1; //InterfaceMethod interfaceForHello.noUse:()V
14: invokestatic #9; //Method staticMethod:()V
17: getstatic #2; //Field java/lang/System.out:Ljava/io/PrintStream;
20: ldc #10; //String Hello Bloggers !
22: invokevirtual #4; //Method java/io/PrintStream.println:(Ljava/lang/String;)V
25: return
}
Post a Comment