Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Is Interface a class?

 
karthick sathya
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ,

i have some basic idea about class ,abstract class and interface. but still i'm not sure about the interface concepts.

i know interface is to achieve multiple inheritence and cannot create instances .
it also has some properties like class like interface will also compile into .class file.
 
Balasubramanian Chandrasekaran
Ranch Hand
Posts: 215
Firefox Browser Linux VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by karthick sathya:
hi ,

i have some basic idea about class ,abstract class and interface. but still i'm not sure about the interface concepts.

i know interface is to achieve multiple inheritence and cannot create instances .
it also has some properties like class like interface will also compile into .class file.


An interface is a list of methods that must be implemented. A class that implements an interface must define all those methods. The method signatures (prototypes) are listed in the interface. Interfaces may also define public static final "constants". An interface is essentially the same as an completely abstract class.
 
Michael Breuer
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to make it more concrete
An interface is a list of methods that must be implemented. A class that implements an interface must define all those methods. The method signatures (prototypes) are listed in the interface. Interfaces may also define public static final "constants". An interface is essentially the same as an completely abstract class.


A class can implement an interface without defining its methods when it is an abstract class. So the first class which shall be instanciated has to implement all methods which are not yet implemented in any superclass.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12122
30
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
an interface is an agreement that states what methods a concrete object must implement (or inherit an actual implementation). It is NOT a class, but you can sort of pretend that objects that implement a certain interface are objects of that type.

The idea is to make code more flexible. the code that RECEIVES the object can pretend it's an object of the interface type.

Let's use a Pet interface. I write my code to accept Pets. Originally, my vendor only sends me Dogs, Cats, and Birds, all of which implement a Pet interface. In a year, the vendor can now also start sending me Fish, Ferrets, and Snakes, again all of which implement the interface. My code doesn't need to be changes AT ALL, since I'm only dealing with Pets.

Similarly, the vendor wants to change their Dog class. they are going to completly change how the whole thing works. They can create a Canine class. As long at that new class implements the Pet interface, nothing needs to be changed.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You might be interested in: Interface vs. Abstract Class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic