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提出了抽象类吧。



曾经听一哥们说:

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





目录
相关文章
|
15天前
|
Java
Java中的抽象类:深入了解抽象类的概念和用法
Java中的抽象类是一种不能实例化的特殊类,常作为其他类的父类模板,定义子类行为和属性。抽象类包含抽象方法(无实现)和非抽象方法。定义抽象类用`abstract`关键字,子类继承并实现抽象方法。抽象类适用于定义通用模板、复用代码和强制子类实现特定方法。优点是提供抽象模板和代码复用,缺点是限制继承灵活性和增加类复杂性。与接口相比,抽象类可包含成员变量和单继承。使用时注意设计合理的抽象类结构,谨慎使用抽象方法,并遵循命名规范。抽象类是提高代码质量的重要工具。
26 1
|
13天前
|
Java
Java中ReentrantLock中tryLock()方法加锁分析
Java中ReentrantLock中tryLock()方法加锁分析
12 0
|
2天前
|
Java 关系型数据库 MySQL
Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
【4月更文挑战第12天】Elasticsearch【问题记录 01】启动服务&停止服务的2类方法【及 java.nio.file.AccessDeniedException: xx/pid 问题解决】(含shell脚本文件)
23 3
|
4天前
|
存储 Java
Java动态转发代理IP的实现方法
Java动态转发代理IP的实现方法
20 11
|
5天前
|
Java
Java接口中可以定义哪些方法?
【4月更文挑战第13天】
7 0
Java接口中可以定义哪些方法?
|
7天前
|
设计模式 Java
Java接口与抽象类
Java接口与抽象类
17 0
|
11天前
|
Java Shell
Java 21颠覆传统:未命名类与实例Main方法的编码变革
Java 21颠覆传统:未命名类与实例Main方法的编码变革
12 0
|
13天前
|
Java
Java中关于ConditionObject的signal()方法的分析
Java中关于ConditionObject的signal()方法的分析
21 4
|
13天前
|
安全 Java
append在Java中是哪个类下的方法
append在Java中是哪个类下的方法
21 9
|
15天前
|
Java 关系型数据库 MySQL
大厂面试题详解:Java抽象类与接口的概念及区别
字节跳动大厂面试题详解:Java抽象类与接口的概念及区别
39 0

热门文章

最新文章