jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes interface confused  help Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "interface confused  help" Watch "interface confused  help" New topic
Author

interface confused help

deva raj
Greenhorn

Joined: May 28, 2004
Posts: 6
package scjp;

abstract interface Myinter {
static final int i = 5;
void fun();
}

public class interface2 implements Myinter{

static int i=7;

public void fun(){
System.out.println(i);
}
public static void main(String[] args){

Myinter m = new interface2();
m.fun();
System.out.println(m.i);
}
}


The answer is 7 and 5.


My question is all the variables in an interface is implicitly public,static and final.Then how can we change the valiue of i in class interface2 which implements the interface Myinter.I am confused.please help


Thanks
Darin Niard
Ranch Hand

Joined: Jun 08, 2004
Posts: 118
You don't. Why would you want to?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
When the compiler looks at println(m.i) it sees you've declared variable m as type myInter, so it looks at myInter to find variable i and finds the static final which happens to hold 5. The fact that variable m actually points to an object of type interface2 doesn't come into play because the compiler looks at the declared type.

You actually have a second variable i in the class myInter. You could print 7 by declaring m as myInter or by casting to myInter: println( (myInter)m.i );

This kind of confusion is a good reason to never "shadow" variables by creating new ones with the same name and different scope. As a test question it's good just to make sure you know why it's such a bad programming practice.

BTW: Class and interface names start with capital letters by Java convention ... Interface2 and MyInter would be better.
[ June 20, 2004: Message edited by: Stan James ]

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
deva1,

Welcome to JavaRanch!

We ain't got many rules 'round these parts, but we do got one. Please change your display name to comply with The JavaRanch Naming Policy.

Thanks Pardner! Hope to see you 'round the Ranch!


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jon Poulton
Greenhorn

Joined: Jun 09, 2004
Posts: 27
Hi,
Adding to what has been said already, its useful to understand the purpose of an interface. The purpose of an interface is to define behaviour for a subclass. Given this, some people think (including me) that its bad form to put ANY kind of variable in an interface, even if you want to use it as a static constant.
If you want to be able to change the values of the variables in your interface, then you shouldnt be using an interface; you should be using an Abstract class which you then subclass.
Jon
Jon Poulton
Greenhorn

Joined: Jun 09, 2004
Posts: 27
Actually, I could have phrased that a little better. We should say:

A class implements an interface. A class cannot subclass an interface, as an interface is not a class!

A class can subclass another class or an Abstract class. That is, it inherits all of the methods and variables, and must implement all of its abstract methods, unless of course, it is itself an Abstract class.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: interface confused help