Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Interfaces

 
Vallabhaneni Suresh Kumar
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why do we create interfaces
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Vallabhaneni Suresh Kumar:
Why do we create interfaces




Note the keyword interface instead of class
 
Sherry Jacob
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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!
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another, more technical view is that we use interfaces because classes don't allow multiple inheritance in Java.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic