Interfaces:rules and explanations
Interfaces are indeed the most important part of the Java OO paradigm.It enables Java to indirectly implement Multiple inheritance.In order to implement an Interface you have to adhere to certain rules defined for its implementation.Interfaces are implemented using "implements" keyword.
Example:-
interface Car_attributes{
void accelerate();
void color();
}
The main point to note here is that whenever you implement an interface you have to implement all the methods described in the interface.
Example:-
public class Subaru implements Car_attributes{
public void accelerate(){// Code}
public void color(){// Code}
public void gears(){// Code}
}
Here in above example Subaru class implements Car_attributes interface then Subaru has to mention all methods of Car_attributes interface.Otherwise compiler will not hesitate in slapping you with an error.But still world is full of exceptions and Interfaces are not a superhero(I hope you have watched Superhero Movie).We will look at those exceptions in a few moments.
The implementation classes must adhere to the some rules and these applies to the non abstract implementation:-
1.Provide non abstract(concrete) implementation for all the methods of the interface.
2.Follow all rules of legal overriding.
3.Maintain the signature of the interface method,and also maintain the same return types.
4.You need not declare the exceptions declared in the interface method declaration.
5.Declare no checked exception on implementation methods other than those declared by the interface method ,or subclass of those declared by the interface method.
Wait wait!! there comes the exception that is "if implementation class is abstract class then the abstract class will pass all its work to its first concrete subclass ".
Lets take a look at following example:-
**Suppose bounceable interface has a method bounce()
abstract class ball implements bounceable{}
class Football extends bounceable{
public void bounce(){// Code}
}
That is since Ball the implementing an interface and it is an abstract class then the sub class of Ball will implement all the methods declared in bounceable interface.
Interface Other Tutorials:-
Interface declaration rules
Interface legal declaration rules and issues
Why Java not support multiple inheritance
Example:-
interface Car_attributes{
void accelerate();
void color();
}
The main point to note here is that whenever you implement an interface you have to implement all the methods described in the interface.
Example:-
public class Subaru implements Car_attributes{
public void accelerate(){// Code}
public void color(){// Code}
public void gears(){// Code}
}
Here in above example Subaru class implements Car_attributes interface then Subaru has to mention all methods of Car_attributes interface.Otherwise compiler will not hesitate in slapping you with an error.But still world is full of exceptions and Interfaces are not a superhero(I hope you have watched Superhero Movie).We will look at those exceptions in a few moments.
The implementation classes must adhere to the some rules and these applies to the non abstract implementation:-
1.Provide non abstract(concrete) implementation for all the methods of the interface.
2.Follow all rules of legal overriding.
3.Maintain the signature of the interface method,and also maintain the same return types.
4.You need not declare the exceptions declared in the interface method declaration.
5.Declare no checked exception on implementation methods other than those declared by the interface method ,or subclass of those declared by the interface method.
Wait wait!! there comes the exception that is "if implementation class is abstract class then the abstract class will pass all its work to its first concrete subclass ".
Lets take a look at following example:-
**Suppose bounceable interface has a method bounce()
abstract class ball implements bounceable{}
class Football extends bounceable{
public void bounce(){// Code}
}
That is since Ball the implementing an interface and it is an abstract class then the sub class of Ball will implement all the methods declared in bounceable interface.
Interface Other Tutorials:-
Interface declaration rules
Interface legal declaration rules and issues
Why Java not support multiple inheritance


















0 comments:
Post a Comment
Post a Comment