• Post Reply Bookmark Topic Watch Topic
  • New Topic

Interfaces  RSS feed

 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone,
I wonder why we are not able to instantiate interfaces.
If anyone know the answer, please explain why .
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by memati bas:
Hi everyone,
I wonder why we are not able to instantiate interfaces.
If anyone know the answer, please explain why .


Let's say you can instantiate an interface. What would you expect to happen when you call a method of this object that you just instantiated?

Henry
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An interface has methods but no implementation details.

The can use an interface name as a reference to a class that implements that interface.

Comparable comp = new Student();

That is valid as long as student implements Comparable.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by memati bas:
Hi everyone,
I wonder why we are not able to instantiate interfaces.
If anyone know the answer, please explain why .


For the same reason you cannot instantiate an abstract class. You can't create an object from a class definition that's incomplete. By design, all of the methods listed in an interface are abstract.
[ February 26, 2006: Message edited by: Keith Lynn ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another way to approach this: If you really want to instantiate it, then you really don't want an interface.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66207
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another take: an interface is a contract regarding methods that an object that claims to implement the interface is obligated to provide.

You cannot instantiate the contract, but you can instantiate an object that abides by the contract.
 
Ramaswamy Srinivasan
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear All,

Originally posted by marc weber:
Another way to approach this: If you really want to instantiate
it, then you really don't want an interface.


-- Excellent explanation.

One more view point would be "When you do not need an instance, why create one?"

Cheers,
Ram.
 
Arun Boraiah
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Srinivasan,
I am not sure what causing you these doubts. For a moment forget about java interface, just try recollecting OO concept you would have learnt (If not please read some good book on OO concept) this should answer your questions.
 
Justin Fox
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the best thing to do is just know...that you can't instatiate an interface.

dont worry why you can't, just know..

the only good thing the interface is for, is if you have multiple methods

that you want use in multiple classes, but each has diff. char.

Monk.
 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ohhh, thanks for your all explanaitory replies. You make sure that they are all beneficial for me. Thanks again. However, something still makes me confused about the interfaces.

What is the relationship between " interface " word and "interaction " word ?
 
Paul Clapham
Sheriff
Posts: 22509
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by memati bas:
What is the relationship between " interface " word and "interaction " word ?
They both start with "inter", which means "between" in Latin.
 
Rusty Shackleford
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Monk Fox:
the best thing to do is just know...that you can't instatiate an interface.

dont worry why you can't, just know..

the only good thing the interface is for, is if you have multiple methods

that you want use in multiple classes, but each has diff. char.

Monk.



Why is the most important question to ask. Knowing why something is or isn't is extremely important.

Interfaces are very useful. Try to write a well-written loosely coupled program without them, expecially if you need to mimic multiple inheritance.
 
ak pillai
author
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java does not support multiple inheritances because it causes more problems than it solves. Instead Java supports multiple interface inheritance, which allows an object to inherit many method signatures from different interfaces with the condition that the inheriting object must implement those inherited methods. The multiple interface inheritance also allows an object to behave polymorphically on those methods.

A 10:In design, you want the base class to present only an interface for its derived classes. This means, you don�t want anyone to actually instantiate an object of the base class. You only want to upcast to it (implicit upcasting, which gives you polymorphic behaviour), so that its interface can be used. This is accomplished by making that class abstract using the abstract keyword. If anyone tries to make an object of an abstract class, the compiler prevents it.

The interface keyword takes this concept of an abstract class a step further by preventing any method or function implementation at all. You can only declare a method or function but not provide the implementation. The class, which is implementing the interface, should provide the actual implementation. The interface is a very useful and commonly used aspect in OO design, as it provides the separation of interface and implementation and enables you to:

-- Capture similarities among unrelated classes without artificially forcing a class relationship.
-- Declare methods that one or more classes are expected to implement.
-- Reveal an object's programming interface without revealing its actual implementation.
-- Model multiple interface inheritance in Java, which provides some of the benefits of full on multiple inheritances, a feature that some object-oriented languages support that allow a class to have more than one superclass.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by ak pillai:
Java does not support multiple inheritances because it causes more problems than it solves. Instead Java supports multiple interface inheritance, which allows an object to inherit many method signatures from different interfaces with the condition that the inheriting object must implement those inherited methods. The multiple interface inheritance also allows an object to behave polymorphically on those methods.


This assumes that "inheritance" means "concrete behaviour inheritance" - a hangover from Smalltalk, which is not quite true. Java does support multiple inheritance - multiple contract inheritance.

The assertion that "multiple inheritances [sic] because it causes more problems than it solves" is true indeed, but then what is often ignored (since it not well known and clouded by various agendas - in fact, I doubt you'll google it up) is that "static contract inheritance causes even *more* problems". Java implements static contract inheritance using interfaces (and interface inheritance). Java does not support dynamic contract inheritance - oh well - in fact, I don't know of any existing type-safe language that supports dynamic contract inheritance.

The reasoning for the flaws (and also axioms) associated with static contract inheritance is quite lengthy.
 
memati bas
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your all replies again.
So, since interfaces can be used in different class so there can be implemented by different class that's why interface is related with interactions. Different classes can implement it.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!