• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Interfaces

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 490
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 295
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 802
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 288
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic