Trending September 2023 # Interface In Java With Example # Suggested October 2023 # Top 15 Popular | Saigonspaclinic.com

Trending September 2023 # Interface In Java With Example # Suggested October 2023 # Top 15 Popular

You are reading the article Interface In Java With Example updated in September 2023 on the website Saigonspaclinic.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested October 2023 Interface In Java With Example

What is Interface in Java?

An Interface in Java programming language is defined as an abstract type used to specify the behavior of a class. A Java interface contains static constants and abstract methods. A class can implement multiple interfaces. In Java, interfaces are declared using the interface keyword. All methods in the interface are implicitly public and abstract.

Syntax for Declaring Interface

To use an interface in your class, append the keyword “implements” after your class name followed by the interface name.

interface { }

Example for Implementing Interface

Now, let’s understand interface in Java with example:

class Dog implements Pet interface RidableAnimal extends Animal, Vehicle

Why is an Interface required?

To understand the use of interface in Java better, let see an Java interface example. The class “Media Player” has two subclasses: CD player and DVD player. Each having its unique interface implementation in Java method to play music.

Another class “Combo drive” is inheriting both CD and DVD (see image below). Which play method should it inherit? This may cause serious design issues. And hence, Java does not allow multiple inheritance.

Now let’s take another example of Dog.

Suppose you have a requirement where class “dog” inheriting class “animal” and “Pet” (see image below). But you cannot extend two classes in Java. So what would you do? The solution is Interface.

The rulebook for interface says,

A Java implement interface is 100% abstract class and has only abstract methods.

Class can implement any number of interfaces.

Class Dog can extend to class “Animal” and implement interface as “Pet”.

Java Interface Example:

Let’s understand the below interface program in Java:

Step 1) Copy following code into an editor.

interface Pet{ public void test(); } class Dog implements Pet{ public void test(){ System.out.println("Interface Method Implemented"); } public static void main(String args[]){ Pet p = new Dog(); p.test(); } }

Step 2) Save , Compile & Run the code. Observe the Output of the interface in Java program.

Difference between Class and Interface

Class Interface

In class, you can instantiate variable and create an object. In an interface, you can’t instantiate variable and create an object.

Class can contain concrete(with implementation) methods The interface cannot contain concrete(with implementation) methods

The access specifiers used with classes are private, protected and public. In Interface only one specifier is used- Public.

When to use Interface and Abstract Class?

Use an abstract class when a template needs to be defined for a group of subclasses

Use an interface when a role needs to be defined for other classes, regardless of the inheritance tree of these classes

Must know facts about Interface

A Java class can implement multiple Java Interfaces. It is necessary that the class must implement all the methods declared in the interfaces.

Class should override all the abstract methods declared in the interface

The interface allows sending a message to an object without concerning which classes it belongs.

Class needs to provide functionality for the methods declared in the interface.

All methods in an interface are implicitly public and abstract

An interface cannot be instantiated

An interface reference can point to objects of its implementing classes

An interface can extend from one or many interfaces. Class can extend only one class but implement any number of interfaces

An interface cannot implement another Interface. It has to extend another interface if needed.

An interface which is declared inside another interface is referred as nested interface

At the time of declaration, interface variable must be initialized. Otherwise, the compiler will throw an error.

The class cannot implement two interfaces in java that have methods with same name but different return type.

Summary:

The class which implements the interface needs to provide functionality for the methods declared in the interface

All methods in an interface are implicitly public and abstract

An interface cannot be instantiated

An interface reference can point to objects of its implementing classes

An interface can extend from one or many interfaces. A class can extend only one class but implement any number of interfaces

You're reading Interface In Java With Example

Update the detailed information about Interface In Java With Example on the Saigonspaclinic.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!