Roo
Roo

Reputation: 427

Why have an abstract class and abstract variables/methods in an Interface?

I was given an Interface from another team to include in my code for database logging. The Interface is filled with only abstract classes and variables/methods. What is the reason to do this?

It does not have any logic. I am simply overriding trace and isTraceEnabled when I extend Logger.

I can`t get my head around it. What is the point of having methods implemented that do nothing but return a specific value they expect?

public abstract class Logger {
    public abstract void trace(String message);
    public abstract bool isTraceEnabled { get; }
}

Upvotes: 1

Views: 184

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726809

When they give you an interface like that, it means one of two things:

  • If they are requesting logging from your library, while your library is responsible for performing database logging, then you need to define a class that extends Logger, and provide implementation of each of its abstract methods.
  • If you are using Logger to perform logging which their library provides to you, then they should provide a way for you to get an instance of a class extending the abstract Logger class. In this case you should not be extending the class at all: you should get an instance of it upfront, store it in a variable of type Logger, and program to its interface.

Upvotes: 4

Related Questions