Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Constructor in interface?

 
vishwanath nadimpally
Ranch Hand
Posts: 116
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 160
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 116
Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic