Originally posted by Ramya Dhurga:
What is the difference between object that we are creating for class and the instance we are creating for interface
Interface -> you are defining the contracts and leaving the implementation details to class.
class -> Can have general methods & variables. Class can implement n number of interfaces..
Say for example, You can have a customer class
class Customer
{
private
String name;
private String customerID;
private String address;
public String getName(){ }
public String getCustomerID() {}
public String getAddress(){}
.... you will have corresponding getter & Setter methods.
}
and if you are defining CustomerInterface to get the customer ID and Name
class CustomerInterface
{
public String getName();
public String getCustomerID();
}
Now you are defining the CustomerImpl class which this interface
class CustomerImpl implements CustomerInterface
{
private String name;
private String customerID;
private String address;
public String getName(){ }
public String getCustomerID() {}
public String getAddress(){}
}
Now in the client program,
Customer customer1 = new Customer();
CustomerInterface customer2 = new CustomerImpl();
you can call all the methods defined in the customer class but in the customer2 object you can call only the interface methods & not the other methods. if you call getAddress() will give you compile time error.
The basic thing is the second one will make sure that it has implemented the contracts definec the interface.
Regards,
M.S.Raman