What is the Output of the following Java Program

Public interface I1
{
Static String x="inside I1";
public void someMethod();
}

class A implements I1
{
public static void main(String args[[])
{
System.out.println("Calling I1 var ..."+I1.x);
}
}

Questions by khadarzone   answers by khadarzone

Editorial / Best Answer

abuthahir.d  

  • Member Since Jan-2008 | Apr 14th, 2008


Compilation error due to 2 reasons.
1. All variables defined in the Interfaces are implicitly public static final. So need not specify "Static" in interface.

2. If any of the member functions of a Interface is not Overriden , a compilation error will be thrown

Showing Answers 1 - 36 of 36 Answers

abuthahir.d

  • Apr 14th, 2008
 

Compilation error due to 2 reasons.
1. All variables defined in the Interfaces are implicitly public static final. So need not specify "Static" in interface.

2. If any of the member functions of a Interface is not Overriden , a compilation error will be thrown

Dear All, 
  
  This will be a compilation error because method some method must be impelemented. Variable x inside interface I1 is fine.. Even we need not have to write static key word for varibale x. Because by default all the variables of interface is public static and final. So in above written example just correct syntex and write method body public void someMethod(); inside class A, it will run.


public interface I1
{
static String x="inside I1";
public void someMethod();
}

public class A implements I1 {

 public static void main(String args[]) {
 System.out.println("Calling I1 var ..."+I1.x);
 }
 public void someMethod() {};
}

Regards,
Ashish Kumar

vaneet

  • Jun 3rd, 2008
 

The output will be a compilation error, but not because of the static(Static written though.. i ignore) word used, but because the abstract method has not been implemented in the class nor has it been declared as abstract.

public int x = 1;
int x = 1; 
static int x = 1; 
public final int x = 1;
public public static int x = 1;

public final int x = 1;
static final int x = 1
public static final int x = 1;

Above combinations all are legal & same.

AND
all declerations(var) are Constants

interface Foo {

int BAR = 42;

void go();

}

class Zap implements Foo {

public void go() {

BAR = 27;

}

}
is wont compile because BAR is read only type.

  Was this answer useful?  Yes

The above program gives 2 compilation errors:-

1. the keyword is static and NOT Static. Did you notice the erroneous Capital S.

2. If your class implements an interface which has a method declaration, that method has to be defined/implemented within your class or else compilation error will occur.

An interface variable is by default public static final which means, its a constant and that , it has to be initialized with a value. It doesn't matter whether you use all or either of the three access specifiers.

Sarje

  • Aug 18th, 2009
 

This source code will not compile and generate following error

class A is not abstract and does not override someMethod()

If you make class A abstract or override someMethod() then it will compile and produce following result whatever is String literal after execution.

Compilation error will come because you must have to override the abstract method of the implememted interface. After the implementation of the abstract method the program will compile and run properly and print "inside l1".

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions