It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Interfaces Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interfaces" Watch "Interfaces" New topic
Author

Interfaces

Vallabhaneni Suresh Kumar
Ranch Hand

Joined: Mar 01, 2004
Posts: 68
Why do we create interfaces
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
Originally posted by Vallabhaneni Suresh Kumar:
Why do we create interfaces




Note the keyword interface instead of class
Sherry Jacob
Ranch Hand

Joined: Jun 29, 2005
Posts: 128
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...


<strong><br />Cheers !!<br /> <br />Sherry<br /></strong><br />[SCJP 1.4]
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
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
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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!


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Another, more technical view is that we use interfaces because classes don't allow multiple inheritance in Java.


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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Interfaces
 
Similar Threads
"Top Ten Topics that Everyone Thinks are on the SCJP Exam, but Aren't "- Bert Bates
interface extends interface ?
Bridge Design Pattern...
Does every interface extend class Object?
EJB 3 Client View