The course is part of this learning path
In this course, we will learn the concepts of microservice and spring framework with a focus on inheritance.
- Inheritance in Java
- Beginner Java developers
- Java developers interested in learning how to Build and Deploy RESTful Web Services
- Java Developers who want to develop web applications using the Spring framework
- Java Developers who want to develop web applications with microservices
- Java Developers who wish to develop Spring Boot Microservices with Spring Cloud
- Basic Java knowledge
In this video, we will talk about the Interfaces in Java. Interfaces are a set of common behaviors that are implemented by multiple classes. Interfaces can only have constants and methods without a body. But if you create a method inside an interface using the private, static, or default keywords, then the method can also have a body. We will learn this in practice shortly. The interface can't do anything. It's just a pattern. You cannot instantiate an interface. It means that you cannot create the object of it. An interface does not contain any constructors. An interface is not extended by a class; it is implemented by one or more classes. Also, a Java class can implement multiple interfaces. If a class implements more than one interface, the interfaces must be separated by commas. If you remember, we said that in the inheritance video. A Java class cannot inherit two or more Java classes. To avoid this issue, we can implement more than one interface to a Java class. Got it? So, this is the main purpose of the interfaces. Also, an interface can inherit from another interface and a class can be extended and implement more than one interface at the same time. The interface in Java is a mechanism to achieve abstraction. It is used to achieve abstraction and multiple inheritances in java. In other words, you can say that interfaces can have abstract methods and variables.
The difference between interface and abstract is that abstract classes may contain non-final variables, whereas variables in the interface are final, public, and static. Java interface also represents the is-a relationship. Also, like a class, an interface can have methods and variables, but the methods declared in an interface are by default abstract. Basically, an interface simply says, "I can do these things." We give a promise to the compiler but an interface doesn't say how to do it. In our example, we have two interfaces: CanGo and CanStop. And the CanGo interface has a method named canGo and the CanStop interface has a method named canStop. And we have a Java class named Car and the class implements these interfaces with the implements keyword and the interfaces are separated by a comma. It means that the Car class can go and can stop. So, canGo() and canStop() methods must be implemented in Car and Motorcycle classes. This is an obligation. All right, let's try and understand interfaces with code examples. In the object-oriented programming project, right-click on the 'Source' folder, and this time, I will select new 'Interface', not the class.
I will specify the package name as interfaceclass and interface name as CanGo and click the 'Finish' button. Okay, an interface only declares method names without a body. We must implement all methods of the interface. Let's declare a void method without the body and the CanGo interface. void canGo(). The methods inside an interface are public and abstract as default. You can write the public abstract at the beginning of the method but this is really redundant. If you define methods in interface like this, that method is considered abstract and then the method cannot have a body. So, you cannot define a method body with curly braces, but there are some exceptions to this. For example, if we use the static keyword in this method, then it can be the body of the method. Or if we use the default keyword in this method, then the body of the method can still be. Or if we define this method as private, it can still be the body of this method. You can also use the static keyword if the modifier is private or you can also use the keyword strictfp with the private modifier. These syntaxes are valid. However, you cannot use the abstract keyword together with any of the other keywords. For example, let's define this method as abstract static. If you pay attention, the compiler warns us. If we position the mouse over the method name, we can see the following error message: "illegal combination of modifiers for the interface method canGo: only one of abstract, default, or static permitted."
All right, so I think you understand how to create methods in interfaces. As a general rule, we will accept methods in interfaces as abstract and therefore, we will not create a method body. Let's continue on. Also, a variable in the interface is public static final as default, i.e. all variables inside the interface are the constant by default. So, if you see a declaration like that in an interface, int speed; this is invalid because the variable is final by default and you must initialize the final variables. Got it? So, don't forget this. Now, let's continue. I will save the code. Now I will create another interface. I right-click on the 'Interface' class and select the new interface options. The name of this interface will be CanStop, and I will click the 'Finish' button. Okay, let's declare a void method without the body in the CanStop interface. void canStop. Okay, now our interfaces are ready. Let's create a new subclass. I right-click on the Interface class package and select 'new', 'Class'. I will specify the class name as Car and click the 'Finish' button.
Now, this Car class will implement the CanGo and CanStop interfaces. For this, after the implements keyword we wrote after the class name, I will write CanGo, CanStop. All right, so the Car class implements the CanGo and CanStop interfaces. That means the CanGo() and CanStop() methods in the interfaces must be implemented in Car class. As you can see, we get a compilation error. If you place the mouse pointer on the Car class, Eclipse shows a quick fix dialogue. It says The type Car must implement the inherited abstract methods. We must implement the abstract method CanGo and CanStop in the Car class. I will select the 'Add unimplemented methods' and Eclipse automatically added these methods with override annotation. Let's fill these methods now. In the canGo() method I will print the "Car can go" message by using the println() method. In the canStop() method, we print the "Car can stop" message by using the print() method. And I will save the code. Okay, let's create the main test class named InterfaceTest in the same package. In the main method, I will create a car object. Car myCar = new Car. Let's call the overridden and canGo() and canStop() methods from class Car. Okay, let's run the code. You see the results in the console. The car can go and car can stop. All right. The interface in Java is like that. So, let's take a short break here. See you in the next video my friends.
OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.