An interface is not a class, writing an interface is similar to writing a class, but they are two different concepts. A class describes the attributes and behaviors of an object. An interface contains behaviors that a class implements unless the class that implements the interface is abstract all the methods of the interface need to be defined in the class.
An interface is similar to a class in the following ways
- An interface can contain any number of methods.
- An interface is written in a file with a .java extension and name of the interface matching the name of the file
- The bytecode of an interface appears in a .class files.
- Interfaces appear in packages and their corresponding bytecode file must be in a directory structure that matches the package name.
An interface is different to a class in the following ways
- You cannot instantiate an interface
- An interface does not contain any constructors .
- All of the methods in an interface are abstract
- An interface cannot contain instance fields the only fields that can appear in interface must be declared both static and final
- An interface is not extended by a class. It is implemented by a class
- An interface can extend multiple interfaces.
Interfaces properties
- An interface is implicitly abstract you do not need to use the abstract keyword when declaring an interface.
- Each method in an interface is also implicitly abstract so the abstract keyword is not needed.
- Methods in an interface are implicitly public.
Implementing Interfaces
When a class implements an interface you can think of the class as signing a contract agreeing to perform the specific behaviors of the interface the class must declare itself as abstract.
- A class can implement more than one interface at a time
- A class can extend only one class, but implement many interfaces.
- An interface itself can extend another interface.
Extending Interfaces
An interface can extend another interface similarly to the way that a class can extend another class. The extend keyword is used to extend an interface and the child interface inherits the methods of the parent interface
Extending multiple interfaces
A java class can only extend one parent class. Multiple inheritance is not allowed.
Interfaces are not class however and an interface can extend more than one parent interfaces . the extend keyword is used once, and the parent interfaces are declared in a comma separated list.
No comments:
Post a Comment