Saturday, 19 November 2011

Sun Certified Java Programmer (SCJP)

Interfaces


An interface is like a public class that has only abstract and public methods. The variables declared in an interface are implicitly public, static, and final. For instance:

interface MyInterface
{


void f();


}


class MyClass implements MyInterface
{


public void f() {}


}


A concrete class implementing an interface has to implement all the methods declared in it.

Interface methods are implicitly public and cannot be declared static. All the overriding rules are applicable for the implemented methods. A class can implement more than one interface. An interface can extend any number of interfaces:

main() method


To run the class as a Java application, there must be a method called main that takes an array of String objects as arguments. The main() method must be public and static and should not return anything. Note that the modifiers static and public can be in any order:

static public void main(String[] abc) {}        // Valid



Command-line arguments


The String array argument of the main() method contains the command line arguments. The length of the array will be equal to the number of command line arguments. If you try to access elements outside the range 0 to length-1, an ArrayIndexOutOfBoundsException is thrown. For instance:

class Test
{


public static void main(String args[])
{


System.out.println(args[0]);


}


}


When you use the java Test Hello command to invoke this code, the output is "Hello."

No comments:

Post a Comment