Java抽象类和方法

简介: 用了Java很多年,很少涉及到抽象类的使用。现在快毕业了,找工作的时候,首当其冲的面试题就是“Java 抽象类和接口的区别”。 好吧,为了以后自己方便,也为了看到这篇文章的读者方便,引用一下官网的解释。

用了Java很多年,很少涉及到抽象类的使用。现在快毕业了,找工作的时候,首当其冲的面试题就是“Java 抽象类和接口的区别”。

好吧,为了以后自己方便,也为了看到这篇文章的读者方便,引用一下官网的解释。


An abstract class is a class that is declared abstract —it may or may not include abstract methods.

抽象类可以含有非抽象方法,也可以不含抽象方法。

Abstract classes cannot be instantiated, but they can be subclassed.

抽象类可以被继承,但不能new。

An abstract method is a method that is declared without an implementation (without braces, and followed by a semicolon), like this:

抽象方法没有方法体,以分号结尾,如下:

abstract void moveTo(double deltaX, double deltaY);

If a class includes abstract methods, the class itself must be declared abstract , as in:

public abstract class GraphicObject {
   // declare fields
   // declare non-abstract methods
   abstract void draw();
}

When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class. However, if it does not, the subclass must also be declared abstract .含有抽象方法的类必须是抽象类。他的子类必须实现他的所有抽象方法,否则子类必须声明为抽象的。



以上是抽象类和抽象方法的基本概念。接下来看一下他和接口的关系:说不定会让你吃一惊。


 All of the methods in an interface (see the Interfaces section) are implicitly abstract, so the abstract modifier is not used with interface methods (it could be—it's just not necessary).接口中的方法都是抽象方法,虽然没有声明。当日声明了也是可以的。

Unlike interfaces, abstract classes can contain fields that are not static and final , and they can contain implemented methods. Such abstract classes are similar to interfaces, except that they provide a partial implementation, leaving it to subclasses to complete the implementation. If an abstract class contains only abstract method declarations, it should be declared as an interface instead.

如果一个抽象类只含有抽象方法,那就把它声明为接口吧。这就是共同点。否则的话,抽象类中的非抽象方法可以有部分功能代码,子类可以完成这些方法。抽象类中的变量也无需是静态和常量。而接口中的变量都是final的。

Multiple interfaces can be implemented by classes anywhere in the class hierarchy, whether or not they are related to one another in any way. Think of Comparable or Cloneable , for example.

By comparison, abstract classes are most commonly subclassed to share pieces of implementation. A single abstract class is subclassed by similar classes that have a lot in common (the implemented parts of the abstract class), but also have some differences (the abstract methods).

抽象类虽然像接口,但他毕竟是一个类,就有类的特征。而接口是给实现他的泪增加属性的。可能是借口和具体类直接的分立太明显了,所以SUN提出了抽象类吧。



曾经听一哥们说:

抽象类被继承后,你可以往抽象类中添加非抽象方法,这样他的子类都具有了这些方法。而接口被实现后,往里面增加方法后,子类必须重写,否则编译出错了。这就是他们看起来的大区别吧。





目录
相关文章
|
3天前
|
Java
Java 抽象类
5月更文挑战第4天
|
12天前
|
Java
判断不为空和不为空串的方法java
判断不为空和不为空串的方法java
|
2天前
|
XML JavaScript Java
详解Java解析XML的四种方法
详解Java解析XML的四种方法
|
2天前
|
存储 Java API
掌握8条方法设计规则,设计优雅健壮的Java方法
掌握8条方法设计规则,设计优雅健壮的Java方法
|
3天前
|
Java
Java一分钟之-抽象类与接口的应用场景
【5月更文挑战第9天】Java中,抽象类和接口用于实现多态和抽象。抽象类不能实例化,提供部分实现和定义模板;接口包含无实现的抽象方法,用于定义行为规范和解耦合。选择时,关注行为用接口,部分实现用抽象类。注意抽象类的`final`和`static`方法、接口冲突等问题,明确设计目标,适度抽象,遵循接口设计原则,以提高代码质量。
12 1
|
3天前
|
Java C语言
详解java方法与递归
详解java方法与递归
9 3
|
3天前
|
SQL Java 数据库连接
JDBC Java标准库提供的一些api(类+方法) 统一各种数据库提供的api
JDBC Java标准库提供的一些api(类+方法) 统一各种数据库提供的api
9 0
|
4天前
|
Java
Java一分钟之-方法定义与调用基础
【5月更文挑战第8天】本文介绍了Java编程中的方法定义和调用,包括基本结构、常见问题和避免策略。方法定义涉及返回类型、参数列表和方法体,易错点有返回类型不匹配、参数错误和忘记返回值。在方法调用时,要注意参数传递、静态与非静态方法的区分,以及重载方法的调用。避免错误的策略包括明确返回类型、参数校验、理解值传递、区分静态和非静态方法以及合理利用重载。通过学习和实践,可以提升编写清晰、可维护代码的能力。
13 0
|
6天前
|
搜索推荐 Java Shell
8大Java排序方法(由简入繁),有代码详解和原理指导
8大Java排序方法(由简入繁),有代码详解和原理指导
29 0
|
12天前
|
Java API
【亮剑】三种有效的方法来删除List中的重复元素Java的List
【4月更文挑战第30天】本文介绍了三种Java中删除List重复元素的方法:1) 使用HashSet,借助其不允许重复值的特性;2) 利用Java 8 Stream API的distinct()方法;3) 对自定义对象重写equals()和hashCode()。每种方法都附带了代码示例,帮助理解和应用。