• 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
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Interfaces vs. classes

 
Ranch Hand
Posts: 99
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

S.T.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Abstraction:

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]

Interface:
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;
add();
}
class K implements G
{
int c;
public add()
{
---
}
}



Classes::

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.


Example:


Class T
{

public int a=0;
public add()
{
--
}
}
 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In order to appreciate the usefulness of interfaces, you must appreciate the power of abstraction.
 
Greenhorn
Posts: 23
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sam Thompson wrote:Hi everyone...

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

S.T.



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!
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Greenhorn
Posts: 23
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 got this tall by not having enough crisco in my diet as a kid. This ad looks like it had plenty of shortening:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic