简体   繁体   中英

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; }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM