image
Interfaces
Start course
Difficulty
Beginner
Duration
2h 28m
Students
136
Ratings
5/5
starstarstarstarstar
Description

In this course, we'll learn about object-oriented programming and its implementations.

Learning Objectives

  • Inheritance
  • Method Overriding
  • Super and This Keywords
  • Final Keyword
  • Abstract Classes
  • Interfaces
  • Polymorphism 
  • Encapsulation

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

Hi there. In this video, we'll 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. 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's 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. 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's used to achieve abstraction and multiple inheritances in Java. In other words, you can say that interfaces 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. It cannot have a method body. 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. 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 CanGo and CanStop. So, CanGo and CanStop methods must be implemented in car and motorcycle classes. This is an obligation. Now, let's make some examples with the interface. In the ObjectOriented programming project, right click on the "src" folder, and this time I'll select "New," "Interface," not the "Class." 

I'll specify the package name as "interfaceclass," and interface name as "CanGo" and click the 'Finish' button. 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 in 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 redundant. 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. 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. Let's declare a void method without the body in the CanStop interface, void canStop(). Now, our interfaces are ready. Let's create a new sub-class. 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. 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, a clip 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'll 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. 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 canGo and canStop methods from class Car. Let's run the code. You see the results in the console. The car can go and car can stop. Yes, the interface in Java is like that. Let's take a short break here. See you in the next video.

 

About the Author
Students
2748
Courses
64
Learning Paths
4

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.

Covered Topics