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

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

问题描述

在我的一次访谈中,我被要求解释界面抽象类之间的区别。

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抽象
类可以具有通常的类成员类型,例如private,
protected等。

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接口工具;应使用关键字extends扩展
Java抽象类。

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个数据库。然后该数据库的每个实现都需要定义上述两种方法:

Now 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.

我尽我所能并希望这会消除你的疑虑。

I tried my best and Hope this will clear your doubts.

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

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