我应该如何解释接口和抽象类之间的区别? [英] How should I have explained the difference between an Interface and an Abstract class?

查看:15
本文介绍了我应该如何解释接口和抽象类之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一次采访中,我被要求解释接口抽象类之间的区别.

In one of my interviews, I have been asked to explain the difference between an Interface and an Abstract class.

这是我的回答:

Java 接口的方法是隐式抽象的并且不能有实现.Java抽象类可以有实现默认行为的实例方法.

Methods of a Java interface are implicitly abstract and cannot have implementations. A Java abstract class can have instance methods that implements a default behaviour.

在 Java 接口中声明的变量默认是 final 的.一个抽象类可能包含非最终变量.

Variables declared in a Java interface are by default final. An abstract class may contain non-final variables.

Java 接口的成员默认是公开的.Java 摘要班级可以具有班级成员的通常风格,例如私人,保护等

Members of a Java interface are public by default. A Java abstract class can have the usual flavours of class members like private, protected, etc.

Java 接口应该使用关键字implements"来实现;一种Java抽象类应该使用关键字extends"进行扩展.

A Java interface should be implemented using keyword "implements"; A Java abstract class should be extended using keyword "extends".

一个接口只能扩展另一个Java接口,一个抽象类可以扩展另一个Java类并实现多个Java接口.

An interface can extend another Java interface only, an abstract class can extend another Java class and implement multiple Java interfaces.

Java 类可以实现多个接口,但只能扩展一个抽象类.

A Java class can implement multiple interfaces but it can extend only one abstract class.

然而,面试官并不满意,告诉我这个描述代表了书本知识".

However, the interviewer was not satisfied, and told me that this description represented "bookish knowledge".

他要求我给出更实际的答复,并使用实际示例解释什么时候我会选择抽象类而不是接口.

He asked me for a more practical response, explaining when I would choose an abstract class over an interface, using practical examples.

我哪里做错了?

推荐答案

我先举个例子:

public interface LoginAuth{
   public String encryptPassword(String pass);
   public void checkDBforUser();
}

假设您的应用程序中有 3 个数据库.那么该数据库的每一个实现都需要定义上述两种方法:

Suppose you have 3 databases in your application. Then each and every implementation for that database needs to define the above 2 methods:

public class DBMySQL implements LoginAuth{
          // Needs to implement both methods
}
public class DBOracle implements LoginAuth{
          // Needs to implement both methods
}
public class DBAbc implements LoginAuth{
          // Needs to implement both methods
}

但是如果 encryptPassword() 不依赖于数据库,并且每个类都相同呢?那么上面的方法就不是一个好方法.

But what if encryptPassword() is not database dependent, and it's the same for each class? Then the above would not be a good approach.

相反,请考虑这种方法:

Instead, consider this approach:

public abstract class LoginAuth{
   public String encryptPassword(String pass){
            // Implement the same default behavior here 
            // that is shared by all subclasses.
   }

   // Each subclass needs to provide their own implementation of this only:
   public abstract void checkDBforUser();
}

现在在每个子类中,我们只需要实现一个方法——依赖于数据库的方法.

Now in each child class, we only need to implement one method - the method that is database dependent.

这篇关于我应该如何解释接口和抽象类之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆