I am creating this post to discuss and explain java tricky programming.This is for experience java programmer who has good knowledge of java programming. Anyone programmer who is having
a hands on experience in Java can understand these tricky program, but many of the program in this post will be tough enough to challenge even the most experienced programmer.
So let us dive into to java tricky programming world :) .
Java Tricky 1.
a hands on experience in Java can understand these tricky program, but many of the program in this post will be tough enough to challenge even the most experienced programmer.
So let us dive into to java tricky programming world :) .
Java Tricky 1.
1: package com.java.puzzle;
2: /*
3: *
4: *
5: *
6: *
7: * */
8: class ParentA{
9: public void puzzleMethod(Object obj){ //method 1
10: System.out.println("Object called");
11: }
12: public void puzzleMethod(double[] array){ // method 2
13: System.out.println("double array called");
14: }
15: }
16: public class OverlodingPuzzle {
17: /**
18: * @param args
19: */
20: public static void main(String[] args) {
21: // TODO Auto-generated method stub
22: ParentA parent=new ParentA();
23: parent.puzzleMethod(null);
24:
25: }
26: }
When puzzleMethod() of ParentA class is called we expect that output from method1 should get called and it should print "Object called"If you run this code , Output will be "double array called" .
Tricks : 1
According to JVM, on invocation of overloaded method, java overloaded mechanism identify all the overloaded method in the class based on rule and then call the most specific of the overloaded method
Java's overload resolution process operates in two phases. The first phase selects all the methods or
constructors that are accessible and applicable. The second phase selects the most specific of the methods . A overloaded method is less specific than another if it can accept any parameters pased to the other
Here all double[] array is Object but all the Object can not be double[] array. So, double[] array is more specific against Object class.
Tips :
to force the compiler to select a specific overloaded method, cast actual parameters to the declared types of the formal parameters.
example :
parent.puzzleMethod((Object)null);
output : "Object called"
Java Tricks 2
1: package com.java.puzzle;
2: /*
3: *
4: *
5: *
6: *
7: * */
8: class ParentA{
9: public void puzzleMethod(Object obj){ //method 2
10: System.out.println("Object called");
11: }
12: public void puzzleMethod(String obj){ // method 1
13: System.out.println("String called");
14: }
15: public void puzzleMethod(double[] array){ // method 3
16: System.out.println("double array");
17: }
18: }
19: public class OverlodingPuzzle {
20: /**
21: * @param args
22: */
23: public static void main(String[] args) {
24: // TODO Auto-generated method stub
25: ParentA parent=new ParentA();
26: parent.puzzleMethod(null);
27: // method 1 and method 3 is ambiguous
28: }
29: }
Here code looks absolutely correct. We can have many overloaded method in the class but it is not the case in above example.
Above code will not compile . Method 1 and method 2 is ambiguous.
Thanks for your informative article. Java is most popular programming language used for creating rich enterprise, desktop and web applications. Keep on updating your blog with such informative post. J2EE Training in Chennai | JAVA Training in Chennai|
ReplyDeleteI found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog.
ReplyDeleteangularjs-Training in velachery
angularjs Training in bangalore
angularjs Training in bangalore
angularjs Training in btm
angularjs Training in electronic-city
angularjs Training in online
Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing....
ReplyDeletepython training in velachery | python training institute in chennai
You made such an interesting piece to read, giving every subject enlightenment for us to gain knowledge. Thanks for sharing the such information with us
ReplyDeleteJava training in Chennai | Java training in Bangalore
Java online training | Java training in Pune
After seeing your article I want to say that the presentation is very good and also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
ReplyDeleteData Science course in rajaji nagar | Data Science with Python course in chenni
Data Science course in electronic city | Data Science course in USA
Data science course in pune | Data science course in kalyan nagar
great java tips At SynergisticIT we offer the best best java training in california
ReplyDelete