


Abstract features of Java - in-depth understanding of abstract classes and interfaces
Key points:
The difference between abstract class and interface
Object-orientedProgrammingIn terms of Abstraction is one of its four major characteristics. In Java, OOP abstraction can be reflected in two forms: interface and abstract class. Interfaces and abstract classes provide us with a more structured way to separate interfaces from implementations.
2. Abstract class1). Conceptual basis We all know that in the object-oriented field, everything is an object. At the same time,
all objects are described by classes, but not all classes can describe objects (object = state + behavior) . If a class does not have enough information to describe a specific object, then we can set such a class as an abstract class. Abstract classes can only be modified by public and default modifiers.
Before understanding abstract classes, let’s first understandAbstract methods. Abstract method is a special method: it only has declaration, but no specific implementation. The declaration format of abstract methods is:
abstract void func();
before the class (it is important to note that an abstract class does not need to contain an abstract method, that is, where All methods are specific methods). Because the abstract class contains methods that are not implemented concretely, you cannot use the abstract class to create objects.
public abstract class ClassName { abstract void fun(); }
abstract classes exist for inheritance. If you define an abstract class, but not If you inherit it, you will create this abstract class in vain, because you can't use it to do anything. For a parent class, if one of its methods does not make any sense when implemented in the parent class, and must be implemented differently according to the actual needs of the subclass, then this method can be declared as an abstract method. , at this time this class will become an abstract class.
A class that contains abstract methods is called an abstract class, but it does not mean that there can only be abstract methods in an abstract class.It can also have member variables like ordinary classes. and ordinary member methods. Note that there are three main differences between abstract classes and ordinary classes:
Abstract methods cannot be private, otherwise, they cannot be subclassed Inherited, subclasses cannot implement this method, so it makes no sense;
Abstract classes cannot be used to create objects;
If a class inherits from an abstract class, the subclass must implement the abstract method of the parent class. If the subclass does not implement the abstract method of the parent class, the subclass must also be defined as an abstract class.
In other aspects, there is no difference between abstract classes and ordinary classes.
2).Essence
The only difference between abstract class and concrete class Difference: A class containing abstract methods must be an abstract class;
The fundamental function of an abstract class is to inherit, so the abstraction of an abstract class Methods cannot be modified by private;
Abstract classes have only one more abstract factor than concrete classes: behavioral level abstraction (can include abstract methods) ;
3. Interface1).Conceptual basis
接口(interface),在软件工程中,接口泛指供别人调用的方法或者函数。从这里,我们可以体会到Java语言设计者的初衷,它是对行为的抽象。接口只能被public和默认修饰符修饰。 在Java中,定义一个接口的形式如下:
public interface InterfaceName {}
接口中可以含有 变量和方法。但是要注意,接口中的 变量 会被隐式地指定为 public static final变量(并且只能是public static final变量,而且 public static final 可省,即默认就是public static final 的) ,而 方法 会被隐式地指定为 public abstract方法且只能是 public abstract 方法(public abstract 可省,即默认就是 public abstract 的),并且接口中所有的方法不能有具体的实现,也就是说,接口中的方法必须都是抽象方法。从这里可以隐约看出接口和抽象类的区别,接口是一种极度抽象的类型,它比抽象类更加“抽象”。
要让一个类遵循某组特地的接口需要使用 implements 关键字,具体格式如下:
class ClassName implements Interface1,Interface2,[....]{ }
可以看出,允许一个类遵循多个特定的接口。如果一个非抽象类遵循了某个接口,就必须实现该接口中的所有方法。对于遵循某个接口的抽象类,可以不实现该接口中的抽象方法。
2).本质
接口是一种行为契约,是对行为的抽象;
四. 接口与抽象类的区别
1.语法层面上的区别
1)抽象类可以提供成员方法的实现细节,而接口中只能存在 public abstract (可省) 方法;
2)抽象类中的成员变量可以是各种类型的,而接口中的成员变量只能是 public static final (可省) 类型的;
3)接口中不能含有静态代码块以及静态方法,而抽象类可以有静态代码块和静态方法;
4)一个类只能继承一个抽象类,而一个类却可以实现多个接口。
2.设计层面上的区别
1)抽象类是对一种事物的抽象,即对类抽象,而接口是对行为的抽象。抽象类是对整个类整体进行抽象,包括属性、行为,但是接口却是对类局部(行为)进行抽象。举个简单的例子,飞机和鸟是不同类的事物,但是它们都有一个共性,就是都会飞。那么在设计的时候,可以将飞机设计为一个类 Airplane,将鸟设计为一个类 Bird,但是不能将飞行 这个特性也设计为类,因此它只是一个行为特性,并不是对一类事物的抽象描述。此时可以将 飞行 设计为一个接口Fly,包含方法fly(),然后Airplane和Bird分别根据自己的需要实现Fly这个接口。然后至于有不同种类的飞机,比如战斗机、民用飞机等直接继承Airplane即可,对于鸟也是类似的,不同种类的鸟直接继承Bird类即可。从这里可以看出,继承是一个 “是不是”的关系,而 接口 实现则是 “有没有”的关系。如果一个类继承了某个抽象类,则子类必定是抽象类的种类,而接口实现则是有没有、具备不具备的关系,比如鸟是否能飞(或者是否具备飞行这个特点),能飞行则可以实现这个接口,不能飞行就不实现这个接口。
2)设计层面不同,抽象类作为很多子类的父类,它是一种模板式设计。而接口是一种行为规范(契约),它可以跨越不同的类,是一种辐射式设计。什么是模板式设计?最简单例子,大家都用过ppt里面的模板,如果用模板A设计了ppt B和ppt C,ppt B和ppt C公共的部分就是模板A了,如果它们的公共部分需要改动,则只需要改动模板A就可以了,不需要重新对ppt B和ppt C进行改动。而辐射式设计,比如某个电梯都装了某种报警器,一旦要更新报警器,就必须全部更新。也就是说 对于抽象类,如果需要添加新的方法,可以直接在抽象类中添加具体的实现,子类可以不进行变更;而对于接口则不行,如果接口进行了变更,则所有实现这个接口的类都必须进行相应的改动。
下面看一个网上流传最广泛的例子:门和警报的例子:门都有open( )和close( )两个动作,此时我们可以定义通过抽象类和接口来定义这个抽象概念:
abstract class Door { public abstract void open(); public abstract void close(); }
或者
interface Door { public abstract void open(); public abstract void close(); }
但是现在如果我们需要门具有报警alarm( )的功能,那么该如何实现?下面提供两种思路:
1)将这三个功能都放在抽象类里面,但是这样一来所有继承于这个抽象类的子类都具备了报警功能,但是有的门并不一定具备报警功能;
2)将这三个功能都放在接口里面,需要用到报警功能的类就需要实现这个接口中的open( )和close( ),也许这个类根本就不具备open( )和close( )这两个功能,比如火灾报警器。
从这里可以看出, Door的open() 、close()和alarm()根本就属于两个不同范畴内的行为,open()和close()属于门本身固有的行为特性,而alarm()属于延伸的附加行为。因此最好的解决办法是单独将报警设计为一个接口,包含alarm()行为,Door设计为单独的一个抽象类,包含open和close两种行为。再设计一个报警门继承Door类和实现Alarm接口。
interface Alram { void alarm(); }abstract class Door { void open(); void close(); } class AlarmDoor extends Door implements Alarm { void oepn() { //.... } void close() { //.... } void alarm() { //.... } }
3、小结
抽象类是对一种事物的抽象,接口是对行为的抽象;
抽象类是一种模板,接口是一种契约;
抽象类的抽象程度介于普通类和接口之间。
The above is the detailed content of Abstract features of Java - in-depth understanding of abstract classes and interfaces. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

The main difference between an abstract class and an interface is that an abstract class can contain the implementation of a method, while an interface can only define the signature of a method. 1. Abstract class is defined using abstract keyword, which can contain abstract and concrete methods, suitable for providing default implementations and shared code. 2. The interface is defined using the interface keyword, which only contains method signatures, which is suitable for defining behavioral norms and multiple inheritance.

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.
