• 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: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do we create interfaces
 
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vallabhaneni Suresh Kumar:
Why do we create interfaces





Note the keyword interface instead of class
 
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vallabhaneni Suresh Kumar:
Why do we create interfaces



Hello VS,
Well basically, interfaces are also sort of classes. But note : they are classes without any method bodies.

So basically, as u know that Java is an object oriented language, every real-world situation can be explained by objects.

To do so, we can decide to provide a basic skeleton of such a scenario which we wish 2 depict. The behavior of the skeletal model is defined by interfaces. That's why interfaces are used...to create the skeletal model.

Interfaces do not have method bodies. They simply have method prototypes which can be implemented by different classes to depict different behaviors of the same skeleton.

Hope u get it...
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sherry Jacob:


Hello VS,
Well basically, interfaces are also sort of classes. But note : they are classes without any method bodies.

So basically, as u know that Java is an object oriented language, every real-world situation can be explained by objects.

To do so, we can decide to provide a basic skeleton of such a scenario which we wish 2 depict. The behavior of the skeletal model is defined by interfaces. That's why interfaces are used...to create the skeletal model.

Interfaces do not have method bodies. They simply have method prototypes which can be implemented by different classes to depict different behaviors of the same skeleton.

Hope u get it...



Whooops, I should read more carefully, I read how, not why

Sorry Vallabhaneni, Sherry's got you covered though
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interfaces are one form of abstraction, which just means ignoring details that don't matter so we can focus on details that do matter.

Say I write a method that takes a List as a parameter. You can call my method with any object that implmements the List interface. I ignore the details about exactly what kind of object it is and focus on the detail that it can do List operations.

A class can implement more than one interface. So you could take almost any class, add "implements List" to its declaration, implement the list methods and pass it to my method. This gives you a lot of freedom. Your object doesn't have to be just one thing - the type it extends. It can implement several interfaces.

In more advanced designs we also use interfaces to control dependencies. (Dependency meaning we have to know all about something just to compile.) For example, Sun "owns" the JDBC interfaces and the various database vendors have to implement them. We depend on stable Sun APIs, and we don't depend on individual database vendors.

Powerful stuff, no? Keep asking questions!
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another, more technical view is that we use interfaces because classes don't allow multiple inheritance in Java.
 
What are you doing in my house? Get 'em tiny ad!
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic