Object-Oriented Programming in Java —Interfaces

An Important Topic of Java Abstraction

Nickson Joram
May 14 · 3 min read

What is an Interface?

Photo by Tracy Adams on Unsplash

Declaration

interface interfaceName {    // Code goes here}
  1. default method(s)
  2. static method(s)
  3. private method(s)
  4. private static method(s)
  5. public static final variable(s)

Rules to consider

  1. By default, all methods declared or implemented in an interface are public, and all variables are public static final.
  2. An interface, like an abstract class, cannot be instantiated.
  3. A class must implement all of the abstract method(s) declared in an interface in order to use it.
  4. An interface cannot contain constructor(s).
  5. A class cannot extend from more than one other class, but it may implement an unlimited number of interfaces.
  6. An interface can be extended by another interface.
  7. A private or protected interface cannot be declared.
Image by Author

Implementation

Let’s see interfaces in action using the below example.

  1. A derived class named Parrot
  2. A derived class named Penguin
  3. An interface named CanFly
Image by Author
Image by Author
Image by Author

Implementation

Parrot is eating! 
Parrot is Flying!
Penguin is eating!
Penguin is Walking!

Advantages

  1. Interfaces enable us to achieve complete abstraction.
  2. Interfaces can be used in an application to achieve loose coupling. This means that a change in one class has no effect on how the other class is implemented.
  3. Interfaces can be used to break up complex designs and clear dependencies between objects.
  4. Multiple inheritances can be accomplished through the use of interfaces.
  1. Object-Oriented Programming in Java — Method Overriding
  2. Object-Oriented Programming in Java — Abstraction

Geek Culture

Proud to geek out. Follow to join our +500K monthly readers.

Nickson Joram

Written by

Computer Science Undergrad | Java Lover | Passionate about Software Engineering, Parallel Computing, AI, and Robotics | Learner | Tech Enthusiast | Being human

Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Nickson Joram

Written by

Computer Science Undergrad | Java Lover | Passionate about Software Engineering, Parallel Computing, AI, and Robotics | Learner | Tech Enthusiast | Being human

Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store