Interface is just like Java class but it contain only abstract method and final attribute.
E.g
public interface Employee{
public void earning();
}
in this "interface" is a keyword and "Employee" is an identifier, in interface all method by default are public and abstract; so you don't need to write otherwise there are also no problem you can also write this
public interface Employee{
abstract public void earning();
}
. Basically interface are used to achieve "Polymorphism".
It's also possible that an interface can inherit / extends to another interface , here is an example
public interface CivilEmployee extends Employee{
public void getRetirementAge();
}
Now here to explain how to use and how to implements the interface and An interface is a "contract" where the class that implements the contract promises to implement the methods.
public class PermanentEmployee implements CivilEmployee{
@Override
public void earning(){
// Do what do you want
}
@Override
public void getRetirementAge(){
//DO what do you want in this methoid
}
}
In this example I use annotation @Override this mean that force to compiler check the method signature with interface method signature ; if it not match then give the compile time error.
Similarly abstract class , object of interface class cannot be instantiate; but major difference between interface and abstract class are
E.g
public interface Employee{
public void earning();
}
in this "interface" is a keyword and "Employee" is an identifier, in interface all method by default are public and abstract; so you don't need to write otherwise there are also no problem you can also write this
public interface Employee{
abstract public void earning();
}
. Basically interface are used to achieve "Polymorphism".
It's also possible that an interface can inherit / extends to another interface , here is an example
public interface CivilEmployee extends Employee{
public void getRetirementAge();
}
Now here to explain how to use and how to implements the interface and An interface is a "contract" where the class that implements the contract promises to implement the methods.
public class PermanentEmployee implements CivilEmployee{
@Override
public void earning(){
// Do what do you want
}
@Override
public void getRetirementAge(){
//DO what do you want in this methoid
}
}
In this example I use annotation @Override this mean that force to compiler check the method signature with interface method signature ; if it not match then give the compile time error.
Similarly abstract class , object of interface class cannot be instantiate; but major difference between interface and abstract class are
- interface contain only all abstract method whereas abstract can contain concrete method
- You can implements more than one interface whereas through abstract class you can extends single abstract calss.
- interface are like rule and abstract classes are guideline.
- interface are use when we know what to do but don't know how to do but opposite in abstract class we know what to do but some how know how to do
Consider we have a Payment class. payment can be done in so many ways like paypal, credit card So we normally take Payment as our interface which contains makePayment() method and CreditCard and PayPal are the two implementation classes.
public interface Payment
{
void makePayment();//by default it is a abstract method
}
public class PayPal implements Payment
{
public void makePayment()
{
//some logic for paypal payment
//like paypal uses username and password for payment
}
}
public class CreditCard implements Payment
{
public void makePayment()
{
//some logic for CreditCard payment
//like CreditCard uses card number, date of expiry etc...
}
}
Similarly like abstract classes are
public abstract class Burger
{
public void packing()
{
//some logic to packing a berger
}
public abstract void price();//here price is different for different catagories.Thats why abstract method
}
public class VegBerger extends Burger
{
public void price()
{
//set price for a veg burger.
}
}
public class NonVegBerger extends Burger
{
public void price()
{
//set price for a non-veg burger.
}
}
No comments:
Post a Comment