An interface cannot be instantiated directly. Here are some key points for defining an interface in Java that must be kept in mind. A Java class can implement any number of interfaces by using keyword implements.Ĩ. Interface can extend an interface and can also extend multiple interfaces. Therefore, they are just public constants. So, we cannot change their value by implementing class once they are initialized.Ħ. The interface is the only mechanism that allows achieving multiple inheritance in java.ħ. So, there is no need to write abstract or public modifiers before them.Ĥ. The fields (data members) declared in an interface are by default public, static, and final. All the (non-default) methods declared in the interface are by default abstract and public. It can contain three types of methods: abstract, default, and static methods.ģ. ![]() It also represents the Is-A relationship.Ģ. Interface provides pure abstraction in Java. There are the following features of an interface in Java. We can also declare default methods and static methods inside interfaces.ĭ) From Java 9 onwards, an interface can also declare private methods. From Java 8 onwards, it is also possible to add a default implementation to an interface method.Ĭ) To support lambda functions, Java 8 has added a new feature to interface. An interface can only declare abstract methods.ī) Java 8 changed this rule. We cannot change their value once they initialized.Ī) Earlier to Java 8, an interface could not define any implementation whatsoever. Therefore, all the variables declared in an interface are considered as public, static, and final by default and acts like constant. Look at the below figure to understand better. Moreover, it also adds public, static, and final keywords before interface variables. Java compiler automatically adds public and abstract keywords before to all interface methods. The variable x declared in MyInterface is like a simple variable. ![]() Void m1() // public and abstract keywords invisibly present.Īs you can see in the above example, both methods m1() and m2() defined in interface are declared with no body and do not have public or abstract modifiers present. Int x = 10 // public static final keyword invisibly present. Let’s understand the declaration of an interface with the help of an example. declare methods that abstract by default.īefore interface keyword, we can specify access modifiers such as public, or default with abstract keyword. It has the following general form: Syntax: It is declared by using the keyword interface followed by interface name. In Java, an interface is declared syntactically much like a class. By using interfaces, we can achieve the functionality of multiple inheritance. It is used to achieve full abstraction in java.ĥ. Programmers use interface to customize features of software differently for different objects.Ĥ. Reproduced courtesy of Bruce Eckel, MindView, Inc.For example, HDFC bank can expose methods or interfaces to various shopping carts.ģ. Using the same method names in different interfaces that are intended to be combined generally causes confusion in the readability of the code, as well. InterfaceCollision.java:23: f( ) in C cannot implement f( ) in I1 attempting to use incompatible return type found : int required: void InterfaceCollision.java:24: interfaces I3 and I1 are incompatible both define f( ), but with different return type When the last two lines are uncommented, the error messages say it all: The difficulty occurs because overriding, implementation, and overloading get unpleasantly mixed together, and overloaded methods cannot differ only by return type. : c08:Adventure.java // Multiple interfaces. The following example shows a concrete class combined with several interfaces to produce a new class: You can have as many interfaces as you want each one becomes an independent type that you can upcast to. You place all the interface names after the implements keyword and separate them with commas. All the rest of the base elements must be interfaces. If you do inherit from a non- interface, you can inherit from only one. In a derived class, you aren’t forced to have a base class that is either an abstract or “concrete” (one with no abstract methods). In Java, you can perform the same act, but only one of the classes can have an implementation, so the problems seen in C++ do not occur with Java when combining multiple interfaces: This is valuable because there are times when you need to say “An x is an a and a b and a c.” In C++, this act of combining multiple class interfaces is called multiple inheritance, and it carries some rather sticky baggage because each class can have an implementation. ![]() Because an interface has no implementation at all-that is, there is no storage associated with an interface-there’s nothing to prevent many interfaces from being combined. ![]() The interface isn’t simply a “more pure” form of abstract class.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |