Interface legal declaration rules and issues
Interfaces follow certain rules which you need to know in order to become an Java expert.There are two rules mainly for interfaces implementation and declaration.
1.A class can implement more than one interface.That is the following declaration is perfectly legal:-
public class Subaru implements Colorable,speedable{// Horrible code here}
It is evident that you can extend only one class ,but you can implement many interfaces.The implemented interface describes the role the class will play.
2.An Interface can extend other Interfaces,but never implement anything.
Following is perfectly legal:-
interface Colorable extends Paintable{ ... }
This requires some thinking from you for a moment.Ok,lets see what happens in this case,here the first concrete (non abstract) implementation class of Colorable must implement all the methods from Colorable and also we have to implement all the buck of Paintable interface.
IMPORTANT:-An Interface can extend more than one Interface.
whoa!! this is strange we have never seen a class who can extend more than one class but what a mess Interface does this.It needs severe attention ...
For example :-
interface Colorable extends Paintable,Sprayble,Movable{ ... } // Perfectly legal
public class Subaru implements Colorable { ... }
The more interfaces the implemented interface will extend more messy it will for first concrete implementation of the interface.That is it has to implement all the methods of Colorable and also of all other extended interfaces.In this case of Paintable,Movable,Sprayble.
It doesn't matter whether you provide some thing inside implemented methods.You can left the implemented methods empty.
If the first implementation is an abstract class then the first concrete subclass of abstract class has to do all the work,implementation of all the methods.
Other Interface tutorials:-
Interface declaration rules
Interface rules and explanations
Why Java not support multiple inheritance
1.A class can implement more than one interface.That is the following declaration is perfectly legal:-
public class Subaru implements Colorable,speedable{// Horrible code here}
It is evident that you can extend only one class ,but you can implement many interfaces.The implemented interface describes the role the class will play.
2.An Interface can extend other Interfaces,but never implement anything.
Following is perfectly legal:-
interface Colorable extends Paintable{ ... }
This requires some thinking from you for a moment.Ok,lets see what happens in this case,here the first concrete (non abstract) implementation class of Colorable must implement all the methods from Colorable and also we have to implement all the buck of Paintable interface.
IMPORTANT:-An Interface can extend more than one Interface.
whoa!! this is strange we have never seen a class who can extend more than one class but what a mess Interface does this.It needs severe attention ...
For example :-
interface Colorable extends Paintable,Sprayble,Movable{ ... } // Perfectly legal
public class Subaru implements Colorable { ... }
The more interfaces the implemented interface will extend more messy it will for first concrete implementation of the interface.That is it has to implement all the methods of Colorable and also of all other extended interfaces.In this case of Paintable,Movable,Sprayble.
It doesn't matter whether you provide some thing inside implemented methods.You can left the implemented methods empty.
If the first implementation is an abstract class then the first concrete subclass of abstract class has to do all the work,implementation of all the methods.
Other Interface tutorials:-
Interface declaration rules
Interface rules and explanations
Why Java not support multiple inheritance


















0 comments:
Post a Comment
Post a Comment