This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Interfaces question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Interfaces question" Watch "Interfaces question" New topic

Interfaces question

Carlos Failde
Ranch Hand

Joined: Oct 20, 2000
Posts: 84
Which of the following are true?
a) An interface can extend another interface
b) You only need provide implementations for those methods that you use when using an interface
c) Interfaces can only contain constant data
d) Interfaces cannot be instantiated
My answer: a),d)
My reasoning:
I think b) is false as you must provide implementations for all the methods in an interface (eventually you do.. you can declare the class as abstract and leave implementations of the method to an eventual subclass)
I think c) is false - the data must be constants, that's true, but interfaces can contain methods too.
a) and d) I know to be obviously true, just by consulting this handy "Java in a Nutshell" book I keep referring to in frustration at the stupid questions being thown at me by my tutor. (well it's not the questions that are stupid to be precise it's the guy's answers... I'm thinking I should be setting the course and he be the student.)

Joined: Jan 16, 2001
Posts: 29
Interface member variables are implicitly final. So you cannot change them. Hence option C is correct.

Option B is wrong. When u implement an interface, u need to provide atleast an empty implementation for them eventhough u don't use them. e.g., event listener interfaces.
Hope this helps.
Carlos Failde
Ranch Hand

Joined: Oct 20, 2000
Posts: 84
Originally posted by natchit:

Interface member variables are implicitly final. So you cannot change them. Hence option C is correct.

I agree that that member variables are implicitly final. But the way I'm viewing the statment is this:
c)Interfaces can only contain data (which must be constant)
hey that's just my quibble
[This message has been edited by Carlos Failde (edited February 01, 2001).]
[This message has been edited by Carlos Failde (edited February 01, 2001).]
Hungson Le
Ranch Hand

Joined: Jan 25, 2001
Posts: 32
Just a tips to what Natchit has said:
Interface member variables are final and static. They're usually used to implements the idea of enum in C++.
interface Month
String Jan="01"; .....; int Dec="12";
then we can use:
String month = Month.Jan;
Hungson Le

Hungson Le<BR>
krishna amit

Joined: Dec 12, 2000
Posts: 5
i think options a and c are definitely true.
all data in an interface are final.
the 3rd option being false because you have to
implement all methods of an interface else the compiler
complaints for the class not being declared abstract.
about 4th option i am not sure but somewhere i have read
that we can instantiate an interface.
i will confirm it.
Shailendra Guggali
Ranch Hand

Joined: Feb 01, 2001
Posts: 86
You can instantiate an Interface
Runnable a = new Runnable()
public void run()
Thread t = new Thread(a);
said to define an Anonymous Runnable Inner Class
Anyone knows better pls EXPLAIN
Kathy Rogers
Ranch Hand

Joined: Aug 04, 2000
Posts: 103
You can't instantiate an interface like that. What you can do is this:-
Thread t = new Thread(new Runnable(){public void run(){...method body . . .}});
It looks like you're instantiating an interface but what you're really doing is creating an anonymous class (a class with no name) that implements Runnable - and you give run() a method body at the same time. It's very convenient to do this with listeners too.
Also if you have a class that implements Runnable
public class MyClass implements Runnable
- obviously it has to provide a method body for run(). But you can then do something like this:-
Runnable a = new MyClass()
but again, really you're creating an instance of the class and not just the interface.
Hope this helps,

Mahesh Bansal
Ranch Hand

Joined: Nov 28, 2000
Posts: 82
What is the outcome of this discussion. Can we say
a, c and d are the right options.
I agree. Here's the link:
subject: Interfaces question
Similar Threads
Interface question
Some basic questions - part 2
my question about interface?
Listeners.Frank, Frank, Frank Please help me!!!!!!!!!!