This time we will touch the issue of declaration of interfaces, if you want to check the other topics you can go to the index
if you're reading from a feed reader like Google Reader or Google buzz and you wonder where is the code , the answer is here
if you're reading from a feed reader like Google Reader or Google buzz and you wonder where is the code , the answer is here
Declaring Interfaces
start by defining an interface as a contract or commitment that a class can do but not how to do it, for example flying define the interface consisting of methods off (), land () defined in code looks like this:
then any class that implements this interface is required to implement the methods take off and land.
interfaces can also be implemented by any class, regardless of who inherits, allowing classes that do not share the same inheritance tree to share a particular behavior, in this case the behavior of an object that can fly and which is defined by the methods declared in the interface example
Say we have two classes the class Avion Ave and both share the flying behavior defined by having a way of taking off and landing (although each is implemented differently) And all this despite not sharing the same inheritance tree, extending Ave Animal class and the class Airplane extends Vehicle but both can be treated as "objects that can fly" and that java would be something like "Objects can invoke methods taking off and landing "
Interfaces can be taken as 100% abstract class because as we saw above abstract classes can not be instantiated, But unlike an abstract class can have abstract methods, not abstract, an interface can have only abstract methods, in addition to much more stringent rules with regard to your statement
- All methods of interface are implicitly public and abstract (modifier public and abstract ) so it is not necessary that you include the modifiers public and / or abstract in his statement
- An interface can declare attributes but these will be public static and final (public modifiers , static final and ) ie can only be constant.
- interface methods can not be static (static modifier )
- As his methods are abstract they can not put the final modifiers, strictfp or native
- An interface can extend one or more interfaces
- An interface can not extend anything but
- interfaces An interface can not implement any interface
- must be declared with the keyword interface
- Interfaces can be used as polymorphic
after these definitions we see an example of an interface declaration:
as I mentioned earlier, an interface can be taken as a 100% abstract class, so the abstract modifier is not necessary. so the following statements are legal
public modifier is required unless you want it or package access by default. Now let
methods:
all methods in an interface are public and abstract (modifiers public and abstract ) so use these switches is redundant, any of the following statements are equal and legal for a method in the interface:
another modifier added a method on an interface will cause an error at compile time, for example
Declaring constants in
interface is allowed to place attributes in interface, however it should be noted that the same value should be available to any class that implements the interface, it only allows you to declare constants. Recall that a constant is achieved by applying the modifiers public , static final and yet this is already implicit in an interface so it is not necessary to put these switches in their attributes, it can lead to confusion you have to be much attention on whether the attribute is declared in an interface (making it a constant and having the modifiers public , static final and implicitly) or if the attribute is declared in a class which makes it an instance variable and not necessarily a constant.
take for example the declaration of an attribute in an interface, all statements are identical:
All these declarations within an interface are equivalent to:
if a class that implements the interface tried to modify the value of the constant altitudMaxima would cause an error at compile time.
0 comments:
Post a Comment