6/20/2023 0 Comments Java interface rules![]() ![]() Interfaces can be used to enforce a contract- that is, provide a specification that classes must implement certain methods if they want to use that interface - Any Car class must implement certain methods like start, stop, etc.Advantages and Disadvantages of Interfaces in Java Advantages Interfaces cannot extend a class because this would violate the property of an interface that it must contain only abstract methods. The different ways in which an interface can extend other interfaces are given below – This concept can also be “extended” to multiple interfaces, where an interface can extend multiple interfaces. So we can see that instead of having our Canary class implement both interfaces separately, we can extend the Flyable interface from Glidable and make our Canary class simply implement the Glidable interface. This is how a class can implement multiple interfaces, and this is extremely useful when we want to achieve multiple inheritances. In this scenario, we can have another interface, Separable, that can be implemented by the Rocket class along with the Flyable interface. If an interface is made private, or if the methods in it are made private or protected, then a compilation error will be thrown.Īssume that in our space-themed Angry Birds example, we implement a multi-stage Rocket, and we want the different modules of a rocket to separate at some point.If you want a class to achieve multiple inheritances, there is only one way: interfaces.An interface cannot have constructors because we cannot create objects of an interface.On the other hand, abstract classes can be used to achieve anything between 0–100% abstraction. An interface can be used when we want to achieve 100% abstraction.Now the HondaCivic class can extend the DieselCar class and implement the Sedan interface, including the appropriate implementation for the start method. This problem can be solved using an interface, where the Sedan can be an interface that is implemented by the HondaCivic class. This is also the reason why multiple inheritances are not permitted in Java. ![]() Now, if we have the start method in both the DieselCar as well as the Sedan class, we run into a problem! Which start method should this class inherit? This could cause design issues and is also known as the infamous Diamond Problem. In case we also need to include the properties for a Sedan, then we need our HondaCivic class to extend Sedan as well. ![]() Our class will need to extend the DieselCar class. Suppose we want to implement a new class for a Honda Civic Diesel car. To understand why multiple inheritances of classes are not allowed in Java, let us look at an example from our Cars scenario. This is a great example of abstraction! We carry out our task, that is, starting the car, but we don’t need to take care of anything else that happens in the background when starting the car. For example, when we start our car, we just plug in the key and start the car without worrying about the nitty-gritty details of what happens underneath when the car starts. For abstraction: Interfaces help to achieve security, hide certain details, and only show the relevant and necessary information.To solve this, we can use a Java interface to give child classes the freedom to extend whatever parent class they wish (and implement our interface) rather than forcing them to extend a specific base class to use our functionality. Multiple inheritances: In Java, we cannot extend multiple classes because of the famous Diamond problem (explained later in this article).We can take any object that implements the Car interface as a parameter and call the start method on it. This is essentially an interface- a checklist! It is a set of methods you want a class to implement to accomplish a certain task. You leave them a checklist of things to be done to bake a cake. Intuition behind Using an Interface in JavaĪssume you are the head baker of a reputed baking chain, and you want to give instructions to the other bakers under you on the steps they need to follow to bake a cake. These methods are public and abstract by default(you don’t have to explicitly use the “ abstract” keyword), and any class implementing your interface will need to provide implementations of those methods. What is an Interface in Java?Īn interface is a set of abstract methods you would want your class to implement. Interfaces are used to achieve abstraction and implement multiple inheritance.Īs complex as it may be to wrap your head around this concept, this article will unravel the world of Interfaces in Java for you with practical examples. It is the blueprint of a class and contains static constants and abstract methods. Interfaces in Java are a set of abstract and public methods we want our classes to implement. ![]()
0 Comments
Leave a Reply. |