Interfaces is a reference that a class can implement.
Rules:
1. All its abstract methods must be implemented. Abstract methods are those methods without a body and end up in a semi colon.
2. Interfaces cannot contain fields or properties unless it is constant. An enum is a constant.
3. You can't create an object in an interface but an interface can be implemented to a class or extended by other interfaces.
4. Only default methods and statics methods can have a body.
5. Nested types are allowed.
======================
class InterfaceDemo1 {
interface CannedGoods {
enum Products {
MEAT("meat"), FISH("fish"), DAIRY("dairy"),
VEGETABLES("vegetables"), PORK("pork"), CHICKEN("chicken");
final String productName;
Products(String stringName) {
productName = stringName;
}
}
default void soldCannedGoods(Products products){
CannedProducts goods = new CannedProducts();
System.out.println("Sold: " + goods.getCannedGoods(products));
}
static void listOfCannedGoods(){
System.out.println("List of Canned Goods:");
for(Products products: Products.values()){
System.out.print(products.productName + " ");
}
System.out.println("");
}String getCannedGoods(Products products);
class CannedProducts implements CannedGoods{
@Override
public String getCannedGoods(Products products) {
return products.productName;
}
}
}
class ProductSold implements CannedGoods{@Override
public void soldCannedGoods(Products products) {
CannedGoods.super.soldCannedGoods(products);
}@Override
public String getCannedGoods(Products products) {
return products.productName;
}
void displayGetCannedGoods(Products products){
System.out.println("Get me: " + getCannedGoods(products));
}
}public static void main(String[] args){
CannedGoods.listOfCannedGoods();
InterfaceDemo1 demo1 = new InterfaceDemo1();
InterfaceDemo1.ProductSold sold1 = demo1.new ProductSold();
sold1.soldCannedGoods(CannedGoods.Products.CHICKEN);
sold1.displayGetCannedGoods(CannedGoods.Products.VEGETABLES);
}
}======================
Results:
List of Canned Goods:meat fish dairy vegetables pork chicken
Sold: chickenGet me: vegetables
6. Interface is package private by default. So, if you want it access to any class you must declare in public.
7. Interface can extend lots of interfaces unlike a class which can extend only one superclass.
====================
interface1 extends interface2, interface3, interface4...{ // abstract method }
==================
8. Abstract, default, and static method of an interface are implicitly public. It means that it is set as public.
9. All constant values inside the interface are implicitly public, static and final.
10. A class can implement more than one interface.
====================
class ClassA implements interface1, interface100, interface200... {//statements}
====================
11. You can used an interface like any other data type there is and You can just used type casting if you want to used the class that implements it.
=====================
Interface YourInterface { //....}
class YourClass implements YourInterface {
.....
void displaySomething(YourInterface yourInterface){
YourClass yc = (YourClass) yourInterface;
}
}
===================
12. A default method can be overridden or redeclared as an abstract method in another interface. If that default method was redeclared as abstract then the class that implements this interface must implement this method.
Note:
When using an interface, you have to make sure that you have declared all the necessary abstract methods in that interface in the beginning or else you will be making huge changes when you want to add another abstract method. One option though is to use a default method instead of an abstract method so that there will be no necessary changes needs to be made in the implementing class. Another option is to use a utility method or a static method since it works the same way as the default method.
YOU ARE READING
Java Programming
Casualehttps://docs.oracle.com/javase/tutorial/ This is the link where I learned my java lessons online. I choose java as first programming language. I am a career shifter. I just want to start something new. I choose to write it here in Wattpad so that I...