01 logo

When to Use Interface and Abstract Class

People are getting confused when this question heads up in the interview.

By Rakshit ShahPublished 3 years ago 3 min read
Like
Image by Author | Interface and Abstract Class

Learn when and what will be helpful from the Interface or Abstract class.

With Java 8, interfaces can have default methods that provide implementations. Anyway, it depends on what you are trying to create. Abstract classes are preferred if the subclasses share some common functionalities with the superclass. Interfaces when you create unrelated classes to implement the interface. Interfaces allow you to implement multiple inheritances. Abstract classes can be extended only once.

Interface

An interface is just the declaration of methods of an object; it’s not the implementation. In an interface, we define what kind of operation an object can perform. These operations are defined by the classes that implement the interface. Interfaces form a contract between the class and the outside world, and this contract is enforced at build time by the compiler.

Gist by Author @rakshitshah94

Consider using interfaces if any of these statements apply to your situation:

  1. You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
  2. You want to take advantage of multiple inheritances.
  3. You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.

Abstract Class

An abstract class is a class that is declared abstract — it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed. An abstract class may have static fields and static methods. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, then the subclass must also be declared abstract.

An abstract method is a method that is declared without an implementation (without braces and followed by a semicolon), like this:

abstract void fetchData(String a, String b);

Consider using abstract classes if any of these statements apply to your situation:

  1. You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
  2. You expect that classes that extend your abstract class have many common methods or fields or require access modifiers other than public (such as protected and private).
  3. You want to share code among several closely related classes.

Remember most important things (Contributed by LinkedIn/Maddy Onyehara)

With Java 8, interfaces can have default methods that provide implementations. Anyway, it depends on what you are trying to create. Abstract classes are preferred if the subclasses share some common functionalities with the superclass. Interfaces when you create unrelated classes to implement the interface. Interfaces allow you to implement multiple inheritances. Abstract classes can be extended only once.

Now, when to use and what to use part is clear, Right?

Comment your thoughts in the below comment section, If something came up new in the market — please share with us!

Need Your Favor (Not compulsory):

If you find joy and value in what I do, please consider supporting my work by subscribing to me or You can become a Medium member to explore thousands of stories written by Creative writers on Medium. Join Medium Membership today, and unleash your creativity. $5/Month is not a big deal, you must try some astonishing experience.

You can also support my work with a donation — If you want to donate me however much you can afford, it means and helps more than you can imagine. You can give tips too using Buy me a coffee. The donation amount will help me to run the website, and server, So I can bring up innovative, knowledgeable things as always.

Disclaimer: Please note that, If you donate me money or using my Medium affiliate link to get your membership plan, I will also get a small commission when my referral link is used!

Originally published on When to use Interface and Abstract class?, also republished on Medium by Rakshit Shah (Me).

how to
Like

About the Creator

Rakshit Shah

I am Computer Engineer and love to make websites and software. I am really eager to know about anything. I am curious to read and write cool stuff.

Reader insights

Be the first to share your insights about this piece.

How does it work?

Add your insights

Comments

There are no comments for this story

Be the first to respond and start the conversation.

Sign in to comment

    Find us on social media

    Miscellaneous links

    • Explore
    • Contact
    • Privacy Policy
    • Terms of Use
    • Support

    © 2024 Creatd, Inc. All Rights Reserved.