Welcome to the CSC Q&A, on our server named in honor of Ada Lovelace. Write great code! Get help and give help!
It is our choices... that show what we truly are, far more than our abilities.

Categories

+14 votes
asked in CSC 305 Fall 2024 by (4.4k points)

4 Answers

+5 votes

from my understanding abstract interfaces are more a way to link things that do not have too many commonalities (classes that do not have a blatant inheritance relationship) they just provide general directions regarding what type of behavior should be implemented while abstract classes promote more code re-use and shared fields.

answered by (4.1k points)
+3 votes

I would use an abstract class when there is ...

  1. a shared state or implementation (common base of functionality)
  2. partial implementations (if some methods need a default implementation and needs to be abstract for subclasses to override)
  3. Hierarchical Relationships (is-a relationships)

Don't use an abstract class when...

  1. Multiple classes need to implement the same set of methods but are unrelated in their inheritance hierarchy.
  2. You need to define a pure contract without any shared state or implementation.

It really comes down to whether you need shared implementation (abstract class) or just behavioral specification (interface).

answered by (4.9k points)
0 votes

You'd use an abstract class when you need to provide shared implementation details or fields along with method declarations for subclasses. Abstract classes are ideal for creating a base class with common functionality. In contrast, interfaces are better when you only need to define a contract (method signatures) without shared implementation.

answered ago by (2.1k points)
0 votes

An abstract class is preferred over an interface when you need a class that allows the sharing of common code functionality, with an inheritance concept in between classes like the is-a relationship, whereas interfaces implement the common functionality between closely related classes. Abstract classes are useful when you want to update a common behavior in the base abstract class it updates the inheriting classes as well.

answered ago by (1.2k points)
...