File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Constructor in interface? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Constructor in interface?" Watch "Constructor in interface?" New topic

Constructor in interface?

vishwanath nadimpally
Ranch Hand

Joined: Jan 25, 2005
Posts: 116
I am preparing for SCJP and have 2 doubts regarding interfaces.

1)An interface is like a 100% abstract class. For an abstract class, the compiler provides a default constructor when a concrete subclass of the abstract class is instantiated.

So similarly does the compiler provide a default constructor for the interface when the first concrete class that implements an interface is instantiated?

2) What is the class heirarchy for an interface? Like every object in java extends java.lang.Object, is there a superclass for an interface (Not the class that is implementing the interface).

Appriciate any help.
Ganpi Srinivasan
Ranch Hand

Joined: Aug 17, 2005
Posts: 160
1)Interfaces do not have constructors - in fact, interfaces define the methods that should be implemented by which ever class that "implements" it. Conceptually, constructors should not be mixed with interfaces - constructors belong in implementations. An interface, on the other hand, is a "contract" for a class that implements it.

2) This again goes back to the concept of interface. You can create an object of a class that implements an interface. Example:

public interface Vehicle {

public class Car implements Vehicle {


Car corvette = new Car(); // or
Vehicle mustang = new Car();

Both are valid since the class Car is a type of Vehicle (because of the interface implementation). Now only the Car class implicitly extends java.lang.Object since the class Object is the root of the class hierarchy (no place for interface in here!)

Hope this was non-confusing & helpful
[ November 17, 2006: Message edited by: Ganpi Srinivasan ]
Chris Beckey
Ranch Hand

Joined: Jun 09, 2006
Posts: 116

You are right in that an interface is like a pure abstract class, though it is not a class at all. An interface is simply a contract that a class agrees to support. Think more in terms of a written specification (the interface) versus the application that implements the specification (a realizing class).
So the answer to your first question is no, there is no constructor for an interface. There is also no single root interface for all interfaces, like Object for class derivation.
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Chris Beckey:
There is also no single root interface for all interfaces, like Object for class derivation.

Notice, though, that interfaces *behave* as if there was one.

That is, you can call all methods implemented by Object on an arbitrary interface reference, even though the interface doesn't declare them.

The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
I agree. Here's the link:
subject: Constructor in interface?
jQuery in Action, 3rd edition