Interface in java with example programs. Earlier we discussed about abstract class which is used to achieve partial abstraction(hiding irrelevant details from user). In this tutorial we are going to discuss about interfaces, which are used for achieving full abstraction. In this post, we will discuss what is an interface? An interface can have methods and variables just like the class but the methods declared in interface are by default abstract (only method signature, no body). Also, the variables declared in an interface are public, static & final by default. We will discuss these points in detail, later in this post. What is the use of interfaces? As mentioned above they are used for abstraction. Since methods in interfaces do not have body, they have to be implemented by the class before you 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 interfaces we can achieve this as a class can implement more than one interfaces, however it cannot extend more than one classes. Declaration. Interfaces are declared by specifying a keyword . E. g.: interface My. Interface. . It has to provide the body of all the methods that are declared in interface. Note: class implements interface but an interface extends another interface. My. Interface. . It has to extend the other interface if required. There are several advantages in utilizing the features of Interfaces in general programming. As you may already know, interfaces define a set of functionality. Interface in java with example programs. EventListener, Remote(java.rmi.Remote) are tag interfaces. These interfaces do not have any field and methods in it. Java Basic Syntax - Learn Java in simple and easy steps starting from basic to advanced concepts with examples including Java Syntax Object. When we consider a Java program. In this interview, Erich Gamma, co-author of the landmark book, Design Patterns, talks with Bill Venners about two design principles: program to an interface, not an implementation, and favor object composition over. The three books in the Java series aim to give the learner a deep understanding of the Standard Edition (SE) Application Programming Interface (API) of the Java programming language. Designing with interfaces. In Java, interfaces solve all these ambiguities caused by the diamond problem. This provides a listing a brief descriptions of each of the J2EE Application Program Interfaces (APIs).See the below example where we have two interfaces Inf. Inf. 2. Inf. 2 extends Inf. If class implements the Inf. Inf. 1 and Inf. 2. Inf. 1. For example Serializable, Event. Listener, Remote(java. Remote) are tag interfaces. These interfaces do not have any field and methods in it. Read more about it here. Nested interfaces. An interface which is declared inside another interface or class is called nested interface. They are also known as inner interface. For example Entry interface in collections framework is declared inside Map interface, that’s why we don’ use it directly, rather we use it like this: Map. Entry. Read more about nested interface along with examples at the link: Nested Interface with examples. Key points: Here are the key points to remember about interfaces: 1) We can’t instantiate an interface in java. Interface provides complete abstraction as none of its methods can have body. On the other hand, abstract class provides partial abstraction as it can have abstract and concrete(methods with body) methods both. While providing implementation in class of any method of an interface, it needs to be mentioned as public. Class implementing any interface must implement all the methods, otherwise the class should be declared as “abstract”. Interface cannot be declared as private, protected or transient. All the interface methods are by default abstract and public. Variables declared in interface are public, static and final by default. Try. . Here we are implementing the interface “Try” which has a variable x. When we tried to set the value for variable x we got compilation error as the variable x is public static final by default and final variables can not be re- initialized. Sample implements Try. Class implements interface and interface extends interface. A class can implement any number of interfaces. If there are. A class can extend only one class but can implement any number of interfaces. It saves you from Deadly Diamond of Death(DDD) problem.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |