AWS Global Infrastructure

Programming & Frameworks

Topics Covered
  • C Programming and Data Structures (17 Blogs)
  • Comprehensive Java Course (4 Blogs)
  • Java/J2EE and SOA (343 Blogs)
  • Spring Framework (8 Blogs)
SEE MORE

All you Need to Know About Implements In Java

Last updated on Jul 13,2020 2.4K Views


The Interface is an Important aspect of OOPs in Java. In this article, we will understand what is implements in Java and how it is used in the following manner:

 

What are Implements in Java?

“Implements” is a keyword which is used to implement an interface in the java language. So, What is an interface in Java?

An Interface is like a Java Class that only has static constants and abstract methods. A java class can implement these interfaces by using the keyword implements. Multiple interfaces can be implemented at a time. All methods are abstract and public.

Java Logo

Two main rules when it comes to interfaces

  • It must be abstract with abstract methods.

  • Any number of interfaces can be implemented by the class.

Multiple inheritances are not supported by java but this can be achieved by using interfaces. More than one Interface can be implemented.

Syntax:

class classname implements interfacename

Example:

class Dog implements Pet

 

Example of Implements in Java

interface Animal {
    public void eat();
    public void travel();
}
public class PetInt implements Animal {
            public void eat() {
                  System.out.println("Pet eats");
               }
               public void travel() {
                  System.out.println("Pet travels");
               }
               public int noOfLegs() {
                  return 0;
               }
               public static void main(String args[]) {
                  PetInt m = new PetInt();
                  m.eat();
                  m.travel();
               }
}

OUTPUT:

Implements In Java

Explanation of the above Code:

In the above code, an interface is created and is implemented using the implements keyword.

interface Animal {
   public void eat();
   public void travel();
}

An interface is created with two methods declared. The class PetInt implements this interface.

public void eat() {
                  System.out.println("Pet eats");
               }
               public void travel() {
                  System.out.println("Pet travels");
               }

Two methods are created called eat and travel respectively. Both methods have simple print statements.

In the main function, we create a pet variable m and call both the methods for it. This is how the interface is implemented.

 

Difference Between Implements and Extends

  1. Implements keyword is used when we want to inherit an interface, whereas the extends keyword is used for inheriting classes.

  2. Multiple interfaces can be inherited at a time whereas only one class is inherited at a time.

  3. A subclass can override some methods of the superclass whereas all methods are implemented from interfaces by class.

For a more In-depth difference, you can refer to our other Blog.

With this, we come to an end of this Implements in Java article. I hope you got an understanding of what exactly Implements are and how are they used.

Check out the Java Training by Edureka, a trusted online learning company with a network of more than 250,000 satisfied learners spread across the globe. We are here to help you with every step on your journey, for becoming a besides this java interview questions, we come up with a curriculum which is designed for students and professionals who want to be a Java Developer.

Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

All you Need to Know About Implements In Java

edureka.co