Consider the following syntax to declare the interface. In java, the interface keyword is used to declare the interface. Here, we should also consider that any class that implements the interface must define its every method. However, classes implement the interface in their way. Interface, in java, is also used to achieve loose coupling.Īs we have already discussed, the interface is the blueprint of a class, i.e., it is implemented by a class to define its methods.The body of the interface method is provided by the implement class: Example. We can achieve abstraction by using interface. class with the implements keyword (instead of extends ).We can achieve multiple inheritance by using interface.There are the following reasons for which the interface is mainly used in java. However, fields are static, public, and final in the interface, whereas methods are public and abstract. We cannot define the method body in the interface.Īn interface is different from abstract classes, i.e., an interface can't be instantiated, just like the abstract class. For every public method in the Object class, there is an implicit public and abstract method in an interface. We can also achieve multiple inheritance in java using interface. An interface does not and cannot extend Object class, because an interface has to have public and abstract methods. By using the interface, we can achieve abstraction in java. An interface can have abstract methods and static constants. The interface in Java can be defined as the blueprint of the class. However, java provides new keyword to create an object of the class.Ĭonsider the following example to create an object of the class and using the class's behavior with the object. However, it can be achieved with interfaces, because the class can implement multiple interfaces. 2) Java does not support 'multiple inheritance' (a class can only inherit from one superclass). In Java, a class is only a blueprint, and the memory is occupied only when the object of the class is created. 1) To achieve security - hide certain details and only show the important details of an object (interface). We can define instance variables, constructors, methods, nested classes and interfaces in the class body. An interface cannot extend classes because it would violate rules that an interface can have only abstract methods and constants. Class body: the class body starts with an opening brace.The keyword implements is used to implement any interface in our class. Interface (If required): A class may implement any interface if required.The keyword extends used to inherit any base class in our class. Superclass (if required): A class may inherit any base class, if any.Class name: the class name should follow the Java naming conventions and must be in CamelCase.class keyword: it is mandatory to use the class keyword to declare a class.Access Modifier: this defines the access modifier for the class, i.e., whether the class is public, private, protected, or default.The class declaration may contain the following components defined in the sequence. The 'class' keyword is used to declare a class.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |