An interface is a reference type in Java. It is similar to class. It is a collection of abstract methods. A class implements an interface, hence inheriting the abstract methods of the interface.
With abstract methods , an interface contain constants, default methods, static methods, and nested types. 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.
How interface is similar to a 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, with the name of the interface matching the name of the file. java training center bangalore
- The byte code of an interface appears in a .class
- Interfaces appear in packages, and their corresponding bytecode file must be in a directory structure that matches the package name.
How interface is different from the class ?
An interface is different from a class in following ways −
1)We cannot create the object of an interface.
2)An interface does not contain any constructors.
3)All of the methods in an interface are by default abstract .
4)An interface cannot contain instance fields. The only fields that can appear in an interface must be declared both static and final. java course in bangalore
5)An interface is not extended by a class; it is implemented by a class.
6)An interface can extend multiple interfaces.
Declaration of Interface :
The interface keyword is used to declare an interface.
Implementing Interfaces :
When a class implements an interface, all methods of the interface should be implemented by the class . Otherwise declare the class as abstract .A class uses the implements keyword to implement an interface.
Extending Interfaces :
An interface can extend another interface in the same way that a class can extend another class. The extends keyword is used to extend an interface, and the child interface inherits the methods of the parent interface. advanced java training in bangalore
Extending Multiple Interfaces :
A Java class can only extend one parent class. Multiple inheritance is not allowed using class . Interfaces are not classes, however, and an interface can extend more than one parent interface. So multiple inheritance is possible using interface .
The extends keyword is used once, and the parent interfaces are declared in a comma-separated list.
Tagging Interfaces :
The most common use of extending interfaces occurs when the parent interface does not contain any methods.
An interface with no methods is known as a tagging interface. There are two reason for tagging interfaces – best core java training in bangalore
Creates a common parent − As with the EventListener interface, which is extended by dozens of other interfaces in the Java API, we can use a tagging interface to create a common parent among a group of interfaces. For example, when an interface extends EventListener, the JVM knows that this particular interface is going to be used in an event delegation scenario.
Adds a data type to a class − This situation is where the term, tagging comes from. A class that implements a tagging interface does not need to define any methods (since the interface does not have any), but the class becomes an interface type through polymorphism. Java/j2ee classes bangalore
Why we use interface ?
Interface is mainly used for abstraction. Since methods in interfaces do not have body, they have to be implemented by the class before we can access them. The class that implements interface must implement all the methods of that interface. Also, java programming language does not support multiple inheritance using class , using interfaces we can achieve this .
Some important points for interface :
- interface is the keyword that is used to create an interface in java.
- We can’t create object for an interface in java.
- Interface contains only abstract methods. Java training center bangalore
- Interfaces can’t have constructors
- By default any attribute of interface is public, static and final
- An interface can’t extend any class but it can extend another interface..
- implements keyword is used by classes to implement an interface.
- A class implementing an interface must provide implementation for all of its method unless it’s an abstract class.
Author : Infocampus is the best java training center bangalore . It provides java course with the 100% placement assistance and live project .Free demo classes are available for java/j2ee classes . For best core java training complete practical classes are provided by the infocampus . Every weekend demo classes are available for advanced java training . In java training syllabus is according to getting a job easily .
To book the seat for free demo class on java training call at : 9738001024 or to get the syllabus of java course enquire at : http://www.infocampus.co.in/java-training-bangalore.html