Sunteți pe pagina 1din 3

5/11/13

Constructor in interface? (Beginning Java forum at JavaRanch)

A friendly place for programming greenhorns!

Big Moose Saloon


Search

Java FAQ

Recent Topics

Register / Login

JavaRanch Java Forums Java Beginning Java

Author
vishwanath nadimpally Ranch Hand Joined: Jan 25, 2005 Posts: 116

Constructor in interface?
posted 11/18/2006 12:01 AM

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

posted 11/18/2006 12:14 AM

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

www.coderanch.com/t/405422/java/java/Constructor-interface

1/3

5/11/13

Constructor in interface? (Beginning Java forum at JavaRanch)

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
I like...

posted 11/18/2006 12:20 AM

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 Joined: Jul 11, 2001 Posts: 14112

posted 11/18/2006 4:46 PM

Originally posted by C hris 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

www.coderanch.com/t/405422/java/java/Constructor-interface

2/3

5/11/13

Constructor in interface? (Beginning Java forum at JavaRanch)

Granny's Programming Pearls "inside of every large program is a small program struggling to get out" JavaRanch.com/granny.jsp

subject: Constructor in interface?

Similar Threads Abstract class constructor abstract class main() in abstract class What is an interface? How is this Possible?
All times above are in your local time zone & format.T he current ranch time (not your local time) is May 11, 2013 06:33:10 .

Contact Us | Powered by JForum |

C opyright 1998-2013 Paul W he aton

www.coderanch.com/t/405422/java/java/Constructor-interface

3/3

S-ar putea să vă placă și