Main differences between Interfaces and Abstract classes

  1. Abstract class is an extensible program-code-template for creating objects, providing implementations of behavior (member functions or methods). On the other side interface is a contract between two interested parties.
  2. An interface contains only a signature of member (incomplete member) while an abstract class contains both abstract (incomplete) and complete member
  3. Methods and members of an abstract class can be defined with any visibility, whereas all methods of an interface must be defined as public (also they are defined public by default).
  4. Abstract classes can have constants, members, method stubs (methods without a body) and defined methods, whereas interfaces can only have constants and methods stubs.
  5. A child class can only extend a single class (abstract or concrete), whereas an interface can extend or a class can implement multiple other interfaces.
  6. A child class can define abstract methods with the same or less restrictive visibility, whereas a class implementing an interface must define the methods with the exact same visibility (public).

Abstract class establishes “is a” relation with concrete classes, while interface provides “has a” capability for classes.

At the end, when you should use interface or abstract class.

Consider using abstract classes if:

  • You want to share code among several closely related classes.
  • 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).
  • 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

Consider using interfaces if:

  • You expect that unrelated classes would implement your interface. For example, many unrelated objects can implement the Serializable interface.
  • You want to specify the behavior of a particular data type, but not concerned about who implements its behaviour.
  • You want to take advantage of multiple inheritances of type.

You may also like

June 10, 2024

Staying Motivated in the IT: Student's Guide

Securing a job in the IT field can feel like trying to crack the toughest code, especially without prior experience. That’s where internships and practical experience come into play, acting as the perfect stepping stones for your career path.  At Lilly021, we’ve noticed some young tech enthusiasts feeling a bit blue, likely due to market […]

June 17, 2024

Quests in Code: Is Game Development the Ultimate IT Career Move for You?

Game development has captured the imagination of IT students and professionals alike. The video game industry, now a multi-billion dollar behemoth, is booming like never before.  But why is it suddenly the talk of the tech town? Is it the allure of cutting-edge tech or the tantalizing promise of dream jobs? Get ready to find […]

June 13, 2024

Bugs and Scalpel Slips: Why Software Development Demands Surgical Precision

A surgeon and a programmer walk into a bar. But it’s not the start of a joke—it’s a scenario highlighting both professions’ weighty responsibilities. Surgeons, with their scalpels, work with life and death hanging in the balance. Armed with code, programmers might not hold lives in their hands, but their mistakes can still wreak havoc […]