Posted on 01 April 2013.
Note: The real advantage of interface is not for supporting multiple inheritance.
Advantage1: We need to declare an interface when we have series of classes which are not having common behavior, still if you want to mandate a method name along with signature and return type and if you want to leave the implementation to the developers of the class.
Advantage2: Interfaces acts as contracts between two developers ( why or how this feature is helpful ?)
Explanation: Assume Developer D1 writing code in class A, this class A requires to call some of the methods present in Class B. Assume class B’s code has to be written by Developer D2. Assume some delay in writing the code happening from Developer D2. In this situation Developer D1 ccanot be able to complete his code since class A’s code is depending on the Class B’s Code. This kind of dependencied between developers code we can avoid by using interfaces .
Q:How to avoid Dependency?
Ans: Developer 1 D1 has to create an interface I1 with the required method names and signatures along with return types which D1 wanted to use present in Class B, then ask Developer 2 D2 to implement interface I1 in class B.
Public inteface I1
int M1(int x,int y);
Public class A
public int M2(I1 i1)
//wrie some code and call inteface i1′s method M1.
Note: In the above sample D2 has to call the method present in class A by sending his class object as parameter to the method M2.
Posted in .Net Tutorials, C# FAQ, Java
Posted on 22 November 2010.
We have a perception that every standalone program will have main() method when comes to languages C, C++ and Java. However, there is small difference in understanding.
The main() method is starting point in C, C++ languages but not in Java. In Java, we have a concept called Class Loading. We know that we pass class name as an argument when we run any java program. It means that we are telling the interpreter that class name we are passing must be loaded to start the process. So now we can sure that the starting point of java standalone program is class loading and not main() method. In other words, before reaching main() (which is in the class ), the interpreter must load the class.
Now we know where and how the java program starts its execution. Now we have to execute other code in the program without going to main() (you can also remove completely main() method).
There is a possibility called Static block. This block executes immediately once the class loaded into JVM. The block structure is as follows.
This is a separate block which can be written apart of members and methods in a class. The code inside this block executes once the class loads in to JVM. So we can write any sort java code in this bound to our requirements.
However, once the execution of block ends, the JVM looks for the main() method. It does not matter whether you have main() defined or undefined in the program, we can terminate the process completely using System.exit() method. This must be called as a last line of the static block to stop the process intentionally without proceeding further to check for main() method.
System.out.println(“Standalone program execution without main()”);
> javac ABC.java
> java ABC
Standalone program execution without main()
This is how we can run a standalone program without using main() method.
Happy Learning – Java.
Posted in Java