Programming codex

How Does Java Handle Overriding?

How does Java Handle Overriding?

[ad_1]

Overriding tactics in Java are very different from C++ as methods by default in Java can be overridden, unlike C++. In C++, the concept of overriding functions is 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 the 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:  return

public 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 at 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 the method invocation. So what the heck is this invokespecial and invoke virtual? JVM used 4 different kinds of instructions for method invocation those are :

– invoke virtual – This is for instance method like System.out.println(“Hello Bloggers!”) here.

– invoke static – 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.

– invoke interface – 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 invoking virtual at #6. Ok, let’s write some code that 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:  return

public 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: return

public 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: return

public 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
}

[ad_2]

Source by Vaibhav Kumar Choudhary

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on pinterest
Share on whatsapp

Leave a comment

Your email address will not be published. Required fields are marked *

Search:

Topics

Recent Posts