This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
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
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
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]
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.
class K implements G
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.
What do you/can you use interfaces for exactly? And how would you implement them in programming?
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".
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.