Overriding strategies in Java is pretty different from C++ as solutions by default in Java can be overridden not like C++. In C++, the concept of overriding functions are taken care of by Digital Table, VTable. While in Java there is some other idea. Before going into the depth, let us see some of the primary factors which a person need to need to know ahead of earning their arms dirty in overriding idea.

Right here is a Basic HelloWorld Application:


course Hello there
community static void main(String[] args)

Procedure.out.println("Hi Bloggers!")


Allows see what the bytecode is producing, javap -c Good day


Compiled from "Hello there.java"
course Hi there extends java.lang.Item
Hello()
Code:
: aload_
1: invokespecial #1 //Approach java/lang/Item."":()V
4: return

community static void major(java.lang.String[])
Code:
: new #2 //course Howdy
3: dup
4: invokespecial #3 //Process "":()V
7: astore_1
8: getstatic #4 //Area java/lang/Process.out:Ljava/io/PrintStream
11: ldc #5 //String Hi it is
13: invokevirtual #6 //Approach java/io/PrintStream.println:(Ljava/lang/Str
ing)V
16: return

Have a search on these traces:


1: invokespecial #1 //Process java/lang/Object."":()V
4: invokespecial #3 //Method "":()V
13: invokevirtual #6 //System java/io/PrintStream.println:(Ljava/lang/Str
ing)V

These are the traces associated to system invocation. So what the heck is this invokespecial and invokevirtual ? Basically JVM made use of 4 unique varieties of directions for system invocation all those are :

– invokevirtual – This is for occasion technique like Method.out.println(“Good day Bloggers!”) here.
– invokestatic – This is for course solutions.
– invokespecial – This is for unique issues. It is used when
– get in touch with , occasion initialization.
– tremendous phone, when you will simply call one thing from super.process
– non-public techniques. As personal techniques are not able to be overridden so we need to have to set this in a exclusive class.
– invokeinterface – invoking occasion process with interface reference(Soon we will see the example)

Now we are really clear that why invokespecial has been made use of at #1 and #3 whereas invokevirtual at #6. Ok, allows generate some code which can see the usages of all four.


interface interfaceForHello
general public void noUse()

course Hello there implements interfaceForHello
public void noUse()
Procedure.out.println("No use")

community static void staticMethod()

Technique.out.println("Static system")

community static void main(String[] args)

interfaceForHello iface = new Good day()
iface.noUse()
Howdy.staticMethod()
Technique.out.println("Good day Bloggers ! ")


And here goes the javap -a Hello there:


Compiled from "Good day.java"
class Hi extends java.lang.Object implements interfaceForHello
Hi()
Code:
: aload_
1: invokespecial #1 //Technique java/lang/Item."":()V
4: return

community void noUse()
Code:
: getstatic #2 //Discipline java/lang/Procedure.out:Ljava/io/PrintStream
3: ldc #3 //String No use
5: invokevirtual #4 //Method java/io/PrintStream.println:(Ljava/lang/String)V
8: return

general public static void staticMethod()
Code:
: getstatic #2 //Discipline java/lang/Process.out:Ljava/io/PrintStream
3: ldc #5 //String Static method
5: invokevirtual #4 //Strategy java/io/PrintStream.println:(Ljava/lang/String)V
8: return

community static void principal(java.lang.String[])
Code:
: 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 //System staticMethod:()V
17: getstatic #2 //Subject java/lang/System.out:Ljava/io/PrintStream
20: ldc #10 //String Hello there Bloggers !
22: invokevirtual #4 //Process java/io/PrintStream.println:(Ljava/lang/String)V
25: return

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

I am the founder and owner of the blog - TechTravelHub.com, always love to share knowledge on test automation,tools,techniques and tips.I am a passionate coder of Java and VBScript.I also publish articles on Travel ideas and great honeymoon destinations.Apart from these, I am a gear-head,love to drive across India. I have shared lots of articles here on How to travel several parts of India.Customization of cars aka car modification is my another hobby.Get in touch with me on [email protected]

Write A Comment