Home > Java > javaTutorial > body text

Abstract features of Java - in-depth understanding of abstract classes and interfaces

黄舟
Release: 2017-03-14 11:49:19
Original
1142 people have browsed it


Key points:

##1. Abstract

 For

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 class

1). 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 understand

Abstract 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();
Copy after login

Abstract methods must be modified with the abstract keyword. If a class contains an abstract method, it is called an abstract class. The abstract class must be modified with the abstract keyword

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();
}
Copy after login

It can be seen from here that

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

1).Conceptual basis

  接口(interface),在软件工程中,接口泛指供别人调用的方法或者函数。从这里,我们可以体会到Java语言设计者的初衷,它是对行为的抽象接口只能被public和默认修饰符修饰。 在Java中,定义一个接口的形式如下:

public interface InterfaceName {}
Copy after login

  接口中可以含有 变量和方法。但是要注意,接口中的 变量 会被隐式地指定为 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,[....]{

}
Copy after login

  可以看出,允许一个类遵循多个特定的接口。如果一个非抽象类遵循了某个接口,就必须实现该接口中的所有方法。对于遵循某个接口的抽象类,可以不实现该接口中的抽象方法。

2).本质

  • 接口是一种行为契约,是对行为的抽象;


四. 接口与抽象类的区别

1.语法层面上的区别

  1)抽象类可以提供成员方法的实现细节,而接口中只能存在 public abstract (可省) 方法;

  2)抽象类中的成员变量可以是各种类型的,而接口中的成员变量只能是 public static final (可省) 类型的;

  3)接口中不能含有静态代码块以及静态方法,而抽象类可以有静态代码块和静态方法;

  4)一个类只能继承一个抽象类,而一个类却可以实现多个接口。

         Abstract features of Java - in-depth understanding of abstract classes and interfaces


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();
}
Copy after login

或者

interface Door {    public abstract void open();    public abstract void close();
}
Copy after login

但是现在如果我们需要门具有报警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() {      //....
    }
}
Copy after login


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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!