GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 473 of 502    Print  
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);
}
}



  
Total Answers and Comments: 11 Last Update: November 19, 2009     Asked by: khadarzone 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: abuthahir.d
 
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

Above answer was rated as good by the following members:
interviewprep9, r.praveenkumar, rinumca, er.mukesh_208, james.vinett, bino75, deepkrisna
  Sorting Options  
  Page 1 of 2   « First    1    2    >     Last »  
April 14, 2008 03:25:49   #1  
vraman2006 Member Since: April 2008   Contribution: 1    

RE: What is the Output of the following Java Program
Compilation error coz in interface class u can not declare only a static variable insted u can make it static final
 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 2Overall Rating: -2    
April 14, 2008 06:38:32   #2  
raja_manoo Member Since: May 2006   Contribution: 2    

RE: What is the Output of the following Java Program
Above answer is correct
 
Is this answer useful? Yes | No
April 14, 2008 07:39:59   #3  
abuthahir.d Member Since: January 2008   Contribution: 9    

RE: What is the Output of the following Java Program
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

 
Is this answer useful? Yes | NoAnswer is useful 5   Answer is not useful 1Overall Rating: +4    
April 14, 2008 10:34:53   #4  
khadarzone Member Since: September 2007   Contribution: 17    

RE: What is the Output of the following Java Program
I Think (ii) option of Abutanhir is right since declaring static in interface is legal.

Please correct me if i am wrong.

 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
April 14, 2008 10:38:44   #5  
abuthahir.d Member Since: January 2008   Contribution: 9    

RE: What is the Output of the following Java Program
thats correct ...... my point (1) is wrong. i tried
 
Is this answer useful? Yes | No
May 02, 2008 07:55:25   #6  
ashishpro_us Member Since: April 2008   Contribution: 3    

RE: What is the Output of the following Java Program

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


 
Is this answer useful? Yes | NoAnswer is useful 2   Answer is not useful 0Overall Rating: +2    
June 03, 2008 05:40:37   #7  
vaneet Member Since: July 2007   Contribution: 9    

RE: What is the Output of the following Java Program
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.
 
Is this answer useful? Yes | No
July 08, 2008 01:57:39   #8  
Naveenjavasun Member Since: July 2008   Contribution: 6    

RE: What is the Output of the following Java Program
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.


 
Is this answer useful? Yes | No
March 30, 2009 11:42:10   #9  
shouvanik1979 Member Since: March 2009   Contribution: 5    

RE: What is the Output of the following Java Program
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.

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
August 18, 2009 08:56:34   #10  
Sarje Member Since: August 2009   Contribution: 62    

RE: What is the Output of the following Java Program
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.


 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
  Page 1 of 2   « First    1    2    >     Last »  


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape