jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Interfaces vs. classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Interfaces vs. classes" Watch "Interfaces vs. classes" New topic

Interfaces vs. classes

Sam Thompson
Ranch Hand

Joined: Jul 05, 2011
Posts: 93
Hi everyone...

I just have a general question regarding the differences between interfaces and classes.

What I would like to know is what exactly is/are the differences between classes and interfaces. I understand how classes are used, but what makes the interfaces so different?

What do you/can you use interfaces for exactly? And how would you implement them in programming?

Thanks a lot for your help everyone

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11957

an interface is nothing more than a contract. the person who writes the interface says "anything that implements me must have THESE methods defined".

Unless and until some other class has the 'implements XXXX', the interface by itself is worthless.

once they do, anyone can use that class and not care what the object type REALLY is - they can treat it as the interface type.

Why is this useful? I may tell my vendor or another team "here is the interface I need". They can write a class to do what I need and implement my interface, and provide a special method to get me an instance of it.

Then, in a year or two, they may come up with a newer, better way to do what I need. They can write a brand new class I've never heard of. As long as they implement my interface in the new class, I don't need to know what it is - my code does not need to be touched, since I can still call the same methods as before. That is guaranteed since they must abide by the contract.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
abhay jain
Ranch Hand

Joined: Jun 03, 2011
Posts: 130
Why is this useful?

A java class can not be subclass of more than one super class (i.e. multiple inheritance) , it can implement more than one interface , thereby enabling us to create classes that build upon other classes without problem created by multiple inheritance

note : unlike java C++ allows multiple inheritance
Shubham Chhabra

Joined: Jul 19, 2011
Posts: 9


Abstraction is property of showing the essential details while hiding the background details..Actually abstaction means hiding something.Abstact classes are used in java to implement the concept of abstraction , but they are not full implementation of abstraction.
Abstract classes have abstract methods by default i.e they have not body part.Abstract classes declare the methods...other classes by extending the abstract classes override the methods of abstract classes, if it does not overide the methods while extending, it has to be declare itself as abstract.
Abstract classes are like GUIs in applications.

Diffrence between Intefaces and classes[/b][/b]

Interfaces are the full implementation of abstraction. It is basically like a contract. The fields present in interface are public, static ,final by default, evenif we do not append these keywords with fields. The methods present in interface are public and abstract by default i.e methods has only declaration part they can not have body . So if you are implementing the interfaces you have to override all the methods of interface and you to attach the keywords with the methods while overiding so. Because overidind the methods, some rules have to be followed for an example : if overidden method is public, the overriding method has to be public.

interface G
int m;
class K implements G
int c;
public add()


Classes are the collection of similar objects. They are the datatype for their objects. Classes are for implementation of encapsulation concept of object oriented era. In classes the fields have not any keyword attached with them by default, we have to specify explicitly whether field is public, final or anything else.


Class T

public int a=0;
public add()
Ogeh Ikem
Ranch Hand

Joined: May 13, 2002
Posts: 180
In order to appreciate the usefulness of interfaces, you must appreciate the power of abstraction.
James E Baker

Joined: Jul 28, 2011
Posts: 23
Sam Thompson wrote:Hi everyone...

What do you/can you use interfaces for exactly? And how would you implement them in programming?


Hey Sam!

There's alot of good answers already so I'll just try to build on it. As was mentioned, Java doesn't allow multiple inheritances so your class can only have one superclass. However, you can also "implement" an interface which gives (actually requires) that your class has certain methods, fields, etc. But how does it help you?

Let's say you work for Amazon. They ship all sorts of products - computers, books, etc. These items may all be derived classes in your Java code (ie. super class = Computer, subclass = MacBookPro). Now, Amazon is going to ship all of these items so they need to have certain pieces of information attached to all their goods - weight, dimensions, insured value, etc. Now, you may ask - why not just have those values stored in the a superclass "Inventory" of which computer is derived? ( Inventory -> Computer -> MacBookPro ) Well, Amazon doesn't just ship goods to customers. Perhaps the ship equipment & supplies internally from one warehouse to another. They may track their scanning gun in Java as something like Equipment -> ScanGun. Well, the scanning gun isn't something Amazon sells so it's class isn't derived from "Inventory". However, we can implement an interface called "Shipment" to both the MacBook and the scanning gun.

Not only does the interface "Shipment" add the proper variables and methods we need to process a MacBook or scanning gun as a shipment, but it allows BOTH to be treated in Java AS a Shipment. So we can write a method in Java called something like shipThis(Shipment item) and it can send a MacBook, scanning gun or whatever else implements "Shipment".

Hope this makes some sort of sense. Ha ha!
Kalyan Naveenan

Joined: Jul 23, 2011
Posts: 12
i too have a doubt here. Is it a necessity that interface should hold the abstract or concrete method that needs to be implemented ?
James E Baker

Joined: Jul 28, 2011
Posts: 23
Kalyan Naveenan wrote:i too have a doubt here. Is it a necessity that interface should hold the abstract or concrete method that needs to be implemented ?

I'm not completely sure what you are asking. An Interface only has abstract methods though it can have variables that are initialized with a value.
zain hoda

Joined: Jul 23, 2011
Posts: 6
Intefaces only hold abstract methods, i mean the methods are declared there , not defined. So there is no chance of concrete methods there.

Whenever a class implements an interface, it has to define all the methods of the interface it implements
There is an exception to this rule but that is the case when the class is itself abstract.
In that case, the abstract class can define some methods and leave some to their declaration only like interfaces.

hope this is helpful
I agree. Here's the link: http://aspose.com/file-tools
subject: Interfaces vs. classes
jQuery in Action, 3rd edition