1. Need of interface
*************************
The most non-technical explanation of interfaces is to define behavior i.e. define methods without implementation. Classes that implements interface must have all methods defined in interface. Constants are also define in the interface in order that it can be available for all class that implement it.
By implementing the methods; we are actually sketching the behavior. If Vehicle is the main class, the subclasses can be Mercedes, BMW, Truck, Motorbike etc. but the interfaces will be the one to define behavior. So interface can be called vehicle behavior and can have methods like turnLeft(), turnRight(), accelerate(), decelerate() etc.
for each of its subclasses say Mercedes, BMW, Ford, Truck, Motorbike the methods can be implemented differently as per the class type.
public interface Vehicle {
public void turnLeft( float degree ) throws VehicleException;
public void turnRight( float degree ) throws VehicleException;
public void accelerate( float increase ) throws VehicleException;
public void decelerate( float decrease ) throws VehicleException;
}
In java multiple inheritance is possible only thru interface. Class can extend only one class at a time.
2. Missing constructors in interface
****************************************
A Java Interface is a collection of methods only. An interface does not contain any properties. Interfaces are commonly used as a way of enforcing policy. An interface cannot be constructed. It can only be implemented or declared by the implementing class.
Important Note: A subclass can implement multiple interfaces, but can only inherit from one superclass.
Abstract Interface's methods only contain a signature of the method. The body of the method must be implemented in the implementing class.
Note that all methods in an Interface are implicitly abstract. Thus the abstract keyword in the method declaration is optional.
In brief, interface doesn't create object thus implementation is missing. Methods are present with signature without implementation.So interface don't have constructor.
Example
// File ipizzaStore
public interface IpizzaStore {
// Keyword abstract is optional here
public abstract void run();
}
// File IPizzaHut.java
public class IPizzaHut implements IpizzaStore {
public void run() {
System.out.println("Pizza Hut");
}
}
// File INancys.java
public class INancys implements IpizzaStore {
public void run() {
System.out.println("Nancys");
}
}
// File IMystores.java
public class IMyStores {
public static void main(String[] args) {
// The one and only Interface IPizzaStore variable
IpizzaStore myStore;
// Set myStore to PizzaHut
myStore = new IPizzaHut();
myStore.run();
// Set myStore to Nancys
myStore = new INancys();
myStore.run();
}
}
3 difference between package and interface
******************************************************
An interface is a template or outline that specifies signature of methods. implement ion of methods are not provided by interface, instead class that use interface implements each c methods specified by the interface.
public interface Fruit {
public boolean isEatable();
public String getName();
}
public class Banana implements Fruit {
public boolean isEatable() {
return true;
}
public String getName() {
return "Banana";
}
}
Packages are container. Java interfaces and classes are grouped into packages
Packages are nothing more than the way we organize files into different directories according to their functionality, usability as well as category they should belong to.
Basically, files in one directory (or package) would have different functionality from those of another directory. For example, files in java.io package do something related to I/O, but files in java.net package give us the way to deal with the Network. In GUI applications, it's quite common for us to see a directory with a name "ui" (user interface), meaning that this directory keeps files related to the presentation part of the application. On the other hand, we would see a directory called "engine", which stores all files related to the core functionality of the application instead.
Packaging also help us to avoid class name collision when we use the same class name as that of others. For example, if we have a class name called "Vector", its name would crash with the Vector class from JDK. However, this never happens because JDK use java.util as a package name for the Vector class (java.util.Vector). So our Vector class can be named as "Vector" or we can put it into another package like com.mycompany.Vector without fighting with anyone. The benefits of using package reflect the ease of maintenance, organization, and increase collaboration among developers. Understanding the concept of package will also help us manage and use files stored in jar files in more efficient ways.
4 if coding is done in interface what is results
********************************************************
According to java grammatical rule it will give you error
interface IntStack {
void push(int item); // stores an item
int pop(); // retrieve an item
void calc() {
int num=2;
int tot=0;
tot=num+num;
System.ot.println(tot);
}
}
class tryinterface {
public static void main(String[] args) {
IntStack ob = new InStack();
calc();
System.out.println("Hello World!");
}
}
a. As per rule to call method one need object. which is not possible in interface, because interface don't have constructor.
b. you can call method by defining it static but coding part is not approved by java.
c above all your question is not proper. its rule. when you create object in memory form class you have coding part to implement. while interface is depending upon class for coding .
5.interface program files can be imported into the other java
****************************************************************************
interface program files
***************************
Use Package statement
6. Abstract interface
***************************
Firstly, an interface is abstract. That means you cannot have any implementation in an interface. All the methods declared in an interface are abstract methods or signatures of the methods.
interface means abstract only. You need to know difference between abstract class and interface.
Interface is completely abstract while abstract class is partially abstract
http://forum.java.sun.com/thread.jspa?threadID=700588&messageID=4108344
http://mindprod.com/jgloss/interfacevsabstract.html
http://www.jguru.com/faq/view.jsp?EID=13984
http://www.sap-img.com/java/when-we-go-for-abstract-and-interface.htm
What is the difference between interface and abstract class?
* interface contains methods that must be abstract; abstract class may contain concrete methods.
* interface contains variables that must be static and final; abstract class may contain non-final and final variables.
* members in an interface are public by default, abstract class may contain non-public members.
* interface is used to "implements"; whereas abstract class is used to "extends".
* interface can be used to achieve multiple inheritance; abstract class can be used as a single inheritance.
* interface can "extends" another interface, abstract class can "extends" another class and "implements" multiple interfaces.
* interface is absolutely abstract; abstract class can be invoked if a main() exists.
* interface is more flexible than abstract class because one class can only "extends" one super class, but "implements" multiple interfaces.
* If given a choice, use interface instead of abstract class.
n abstract class can have instance methods that implement a default behavior. An Interface can only declare constants and instance methods, but cannot implement default behavior and all methods are implicitly abstract. An interface has all public members and no implementation. An abstract class is a class which may have the usual flavors of class members (private, protected, etc.), but has some abstract methods.
7.An abstract class can have the constructor?
****************************************************
Yes. Contstructors don't create an instance of the class. They're called as part of the object creation process. Their job is to initialize the object's instance variables.
For every class (except Object, which doesn't have a parent class), the class' constructor calls the parent's constructor--either explicitly or implicitly.
Abstract classes can contain abstract and concrete methods. Abstract classes cannot be instantiated directly i.e. we cannot call the constructor of an abstract class directly nor we can create an instance of an abstract class by using
“Class.forName().newInstance()”
(Here we get java.lang.InstantiationException).
However, if we create an instance of a class that extends an Abstract class, compiler will initialize both the classes. Here compiler will implicitly call the constructor of the Abstract class. Any class that contain an abstract method must be declared “abstract” and abstract methods can have definitions only in child classes. By overriding and customizing the abstract methods in more than one subclass makes “Polymorphism” and through Inheritance we define body to the abstract methods.
Basically an abstract class serves as a template. Abstract class must be extended/subclassed for it to be implemented. A class may be declared abstract even if it has no abstract methods. This prevents it from being instantiated. Abstract class is a class that provides some general functionality but leaves specific implementation to its inheriting classes.
Example of Abstract class:
abstract class AbstractClassExample{
protected String name;
public String getname() {
return name;
}
public abstract void function();
}
Example: Vehicle is an abstract class and Bus Truck, car etc are specific implementations
No! You cannot make an instance of an abstract class. An abstract class has to be sub-classed. If you have an abstract class and you want to use a method which has been implemented, you may need to subclass that abstract class, instantiate your subclass and then call that method.