• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

interface confused help

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You don't. Why would you want to?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic