Home > Java > javaTutorial > What is the interface? What is an abstract class?

What is the interface? What is an abstract class?

零下一度
Release: 2017-06-30 11:20:01
Original
1585 people have browsed it

What is an interface and what is an abstract class? With abstract classes, why do we need interfaces?

Abstract class

The concept of abstract class:

 Abstract class is relative to ordinary class. Ordinary class is a complete functional class that can directly generate instantiated objects, and Ordinary classes can contain constructors, ordinary methods, static methods, constants, variables, etc. Abstract classes refer to adding abstract method components to the structure of ordinary classes, or directly declaring the class as an abstract type;

Similarly, abstract methods are relative to ordinary methods. , there will be a "{}" above all ordinary methods, which represents the method body. Methods with method bodies must be directly used by objects. Abstract methods refer to methods without a method body, and abstract methods must also be modified with the keyword abstract. A class with abstract methods is an abstract class, and abstract classes must be declared using the abstract keyword.

The following is a simple abstract class:

package abstractdemo;  
  
//抽象类  public abstract class AbstractDemo {  //抽象方法,没有方法体  public abstract void test();  
      public void test1(){  
        System.out.println("普通方法");  
    }  
  public static void main(String[] args) {  
        Class<AbstractDemo> c = AbstractDemo.class;  
        System.out.println(c.getName());  
    }  
}
Copy after login

Instance of abstract class:

package abstractdemo;  
  
public abstract class AbstractTest {  
  public static void main(String[] args) {  
        AbstractDemo ad = new AbstractDemo() {  
              
            @Override  public void test() {  
                System.out.println("实例抽象类要重写抽象类中所有的抽象方法!");  
            }  
        };  
        ad.test();  
        System.out.println(ad.getClass());  
    }  
  
}
Copy after login

It can be seen from the above that abstract classes cannot be instantiated directly. Why can't it be instantiated directly? When a class is instantiated, it means that the object can call attributes or methods in the class, but there are abstract methods in abstract classes, and abstract methods have no method body, and they cannot be called without a method body. Since method calls cannot be made, how to generate instantiated objects? Therefore, when instantiating an abstract class, you will be asked to override the abstract method in the abstract class first; if there is no abstract method in an abstract class, then the class cannot be instantiated, and a compilation error will be reported during instantiation;

The principles for using abstract classes are as follows:
(1) Abstract methods must be public or protected (because if they are private, they cannot be inherited by subclasses, and subclasses cannot implement them. This method), which is public by default;
(2) Abstract classes cannot be instantiated directly and need to be handled by subclasses through upward transformation;
(3) Abstract classes must have subclasses. Use extends inheritance. A subclass can only inherit one abstract class;
(4) Subclasses (if not abstract classes) must override abstraction. All abstract methods in the class (if the subclass does not implement the abstract method of the parent class, then the subclass is also an abstract class)

Look at a piece of code below:

package abstractdemo;  
  
//抽象类  public abstract class AbstractDemo {  //抽象方法,没有方法体  public abstract void test();  
      public void test1(){  
        System.out.println("普通方法");  
    }  
  public static void main(String[] args) {  
        Class<AbstractDemo> c = AbstractDemo.class;  
        System.out.println(c.getName());  
    }  
  
}  

package abstractdemo;  
  
public class Test extends AbstractDemo{  
  
    @Override  public void test() {  
        System.out.println("继承抽象类,一定要重写抽象类中的抽象方法,如果不重写,那么该类也是抽象类!");  
    }  
  
}  

package abstractdemo;  
  
public class Demo {  
  public static void main(String[] args) {  
        AbstractDemo ad = new Test();  
        ad.test();  
    }  
  
}
Copy after login

现在就可以清楚的发现: 
(1)抽象类继承子类里面有明确的方法覆写要求,而普通类可以有选择性的来决定是否需要覆写; 
(2)抽象类实际上就比普通类多了一些抽象方法而已,其他组成部分和普通类完全一样; 
(3)普通类对象可以直接实例化,但抽象类的对象必须经过向上转型之后才可以得到。

虽然一个类的子类可以去继承任意的一个普通类,可是从开发的实际要求来讲,普通类尽量不要去继承另外一个普通类,而是去继承抽象类。

抽象类的使用限制:

1.由于抽象类里会存在一些属性,那么抽象类中一定存在构造方法,其存在目的是为了属性的初始化。抽象类中是有构造函数的,所以子类继承抽象类,构造方法的调用顺序仍然是先父类构造函数,然后子类构造函数

2.抽象类不可以修饰为final,因为抽象类有子类,而final修饰的类不能被继承;

3.外部抽象类不能被修饰为static,外部抽象类不允许使用static声明,而内部的抽象类运行使用static声明。使用static声明的内部抽象类相当于一个外部抽象类的成员,继承的时候使用“外部类.内部类”的形式表示类名称。

package com.wz.abstractdemo;static abstract class A{//定义一个抽象类public abstract void print();

}class B extends A{public void print(){
        System.out.println("**********");
    }
}public class TestDemo {public static void main(String[] args) {
        A a = new B();//向上转型        a.print();
    }

}
Copy after login

执行结果

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
    Illegal modifier for the class A; only public, abstract & final are permitted
    at com.wz.abstractdemo.A.<init>(TestDemo.java:3)
    at com.wz.abstractdemo.B.<init>(TestDemo.java:9)
    at com.wz.abstractdemo.TestDemo.main(TestDemo.java:18)
Copy after login

再看一个关于内部抽象类:

package com.wz.abstractdemo;abstract class A{//定义一个抽象类static abstract class B{//static定义的内部类属于外部类public abstract void print();
    }

}class C extends A.B{public void print(){
        System.out.println("**********");
    }
}public class TestDemo {public static void main(String[] args) {
        A.B ab = new C();//向上转型        ab.print();
    }

}
Copy after login

执行结果:

**********
Copy after login

4.任何时候,如果要执行类中的static方法的时候,都可以在没有对象的情况下直接调用,对于抽象类也一样。但是修饰为abstract的抽象方法,不能修饰为static,没有意义;

5.有时候由于抽象类中只需要一个特定的系统子类操作,所以可以忽略掉外部子类。这样的设计在系统类库中会比较常见,目的是对用户隐藏不需要知道的子类。

范例如下:

package com.wz.abstractdemo;abstract class A{//定义一个抽象类public abstract void print();private static class B extends A{//内部抽象类子类public void print(){//覆写抽象类的方法System.out.println("Hello World !");
        }
    }//这个方法不受实例化对象的控制public static A getInstance(){return new B();
    }

}public class TestDemo {public static void main(String[] args) {//此时取得抽象类对象的时候完全不需要知道B类这个子类的存在A a = A.getInstance();
        a.print();
    }
}
Copy after login

运行结果:

Hello World !
Copy after login

抽象类的应用——模板设计模式

例如,现在有三类事物:
(1)机器人:充电,工作;
(2)人:吃饭,工作,睡觉;
(3)猪:进食,睡觉。
现要求实现一个程序,可以实现三种不同事物的行为。

先定义一个抽象行为类:

package com.wz.abstractdemo;public abstract class Action{public static final int EAT = 1 ;public static final int SLEEP = 3 ;public static final int WORK = 5 ;public abstract void eat();public abstract void sleep();public abstract void work();public void commond(int flags){      switch(flags){case EAT:this.eat();break;case SLEEP:this.sleep();break;case WORK:this.work();break;case EAT + SLEEP:this.eat();this.sleep();break;case SLEEP + WORK:this.sleep();this.work();break;default:break;
        }
    }
}
Copy after login

定义一个机器人的类:

package com.wz.abstractdemo;public class Robot extends Action{

    @Overridepublic void eat() {
        System.out.println("机器人充电");

    }

    @Overridepublic void sleep() {

    }

    @Overridepublic void work() {
        System.out.println("机器人工作");

    }

}
Copy after login

定义一个人的类:

package com.wz.abstractdemo;public class Human extends Action{

    @Overridepublic void eat() {
        System.out.println("人吃饭");

    }

    @Overridepublic void sleep() {
        System.out.println("人睡觉");

    }

    @Overridepublic void work() {
        System.out.println("人工作");

    }

}
Copy after login

定义一个猪的类:

package com.wz.abstractdemo;public class Pig extends Action{

    @Overridepublic void eat() {
        System.out.println("猪进食");

    }

    @Overridepublic void sleep() {
        System.out.println("猪睡觉");

    }

    @Overridepublic void work() {


    }

}
Copy after login

测试主类:

package com.wz.abstractdemo;public class AbstractDemo {public static void main(String[] args) {

        fun(new Robot());

        fun(new Human());

        fun(new Pig());

    }public static void fun(Action act){
        act.commond(Action.EAT);
        act.commond(Action.SLEEP);
        act.commond(Action.WORK);
    }

}
Copy after login

运行结果:

机器人充电
机器人工作
人吃饭
人睡觉
人工作
猪进食
猪睡觉
Copy after login

If all subclasses want to complete the operation normally, they must override according to the specified method. At this time, the function of the abstract class is the function of a class definition template.

---------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- -------------------------------------------------- --------------------------------------------------

Interface

An interface is a "class" that is more abstract than an abstract class. I put "class" in quotes here because I can't find a better word to express it, but we need to be clear that the interface itself is not a class, which can be seen from the fact that we cannot instantiate an interface. For example, new Runnable(); is definitely wrong, we can only new its implementation class.

The interface is used to establish a protocol between classes. What it provides is only a form without a specific implementation. At the same time, the implementation class that implements the interface must implement all methods of the interface. By using the implements keyword, it indicates that the class is following a specific interface or group of specific interfaces. It also indicates that "interface is just its appearance, but now Need to state how it works”.

Interface is an extension of abstract class. In order to ensure data security, Java cannot have multiple inheritance. That is to say, inheritance can only exist in one parent class, but the interface is different, and one class can be implemented at the same time. Multiple interfaces, regardless of whether there is a relationship between these interfaces, so interfaces make up for the defect that abstract classes cannot have multiple inheritance, but it is recommended to use inheritance and interfaces together, because this can not only ensure data security but also achieve multiple inheritance.

When using the interface, you need to pay attention to the following issues:

1. All method access rights of an Interface are automatically declared as public. To be precise, it can only be public. Of course, you can explicitly declare it as protected or private, but compilation will cause errors!

2. You can define "member variables" in the interface, or immutable constants, because the "member variables" in the interface will automatically become public static final. It can be accessed directly through the class name: ImplementClass.name.

3. There is no implemented method in the interface.

4. A non-abstract class that implements an interface must implement all methods of the interface. Abstract classes do not need to be implemented.

5. You cannot use the new operator to instantiate an interface, but you can declare an interface variable, which must refer to an object of a class that implements the interface. You can use instanceof to check whether an object implements a specific interface. For example: if(anObject instanceof Comparable){}.

6. When implementing multiple interfaces, you must avoid duplication of method names.

The difference between abstract class and interface

## In the Java language, abstract class and interface are supported Two mechanisms for abstract class definition. It is precisely because of the existence of these two mechanisms that Java is given powerful object-oriented capabilities. There are great similarities between abstract class and interface in terms of support for abstract class definition, and they can even be replaced with each other. Therefore, many developers seem to be more casual about the choice of abstract class and interface when defining abstract classes. In fact, there is a big difference between the two, and their choice even reflects whether the understanding of the nature of the problem field and the understanding of the design intention are correct and reasonable.

##InstantiationCan’tCan’t

Abstract class and interface are both used to create abstract classes in the Java language (the abstract class in this article is not translated from abstract class, it represents an abstract body, and abstract class is A method used to define abstract classes in the Java language), so what is an abstract class, and what benefits can using abstract classes bring us?

A class that declares the existence of a method without implementing it is called an abstract class, which is used to create a class that embodies some basic behavior, and declare a method for the class, but the class cannot be implemented in the class. Instances of abstract classes cannot be created. However, you can create a variable whose type is an abstract class and have it point to an instance of a concrete subclass. There cannot be abstract constructors or abstract static methods. Subclasses of the Abstract class provide implementations for all abstract methods in their parent class, otherwise they are also abstract classes. Instead, implement the method in a subclass. Other classes that are aware of its behavior can implement these methods in their class.

Interface is a variant of abstract class. In an interface, all methods are abstract. Multiple inheritance can be obtained by implementing such an interface. All methods in the interface are abstract, and none of them have a program body. Interfaces can only define static final member variables. Implementation of an interface is similar to subclassing, except that the implementing class cannot inherit behavior from the interface definition. When a class implements a particular interface, it defines (i.e. gives the program body to) all of the methods of this interface. It can then call the interface's methods on any object of the class that implements the interface. Since there are abstract classes, it allows using the interface name as the type of the reference variable. Normal dynamic linking will take effect. References can be converted to and from interface types, and the instanceof operator can be used to determine whether an object's class implements the interface.

Interfaces can inherit interfaces. Abstract classes can implement interfaces, and abstract classes can inherit entity classes, but the premise is that the entity class must have a clear constructor. Interfaces focus more on "what functions can be achieved", regardless of "how they are implemented".

1. Similarities
#A. Both are abstract classes and neither can be instantiated.
B. Both interface implementation classes and subclasses of abstract class must implement the declared abstract methods.

2. Differences
A. Interface needs to be implemented, using implements, while abstract class needs to be inherited, using extends.
B. A class can implement multiple interfaces, but a class can only inherit one abstract class.
C. Interface emphasizes the implementation of specific functions, while abstract class emphasizes the ownership relationship.
D. Although both the interface implementation class and the subclasses of abstract class must implement the corresponding abstract method, the implementation forms are different. Every method in the interface is an abstract method and is only declared. (declaration, no method body), the implementation class must implement it. Subclasses of abstract class can be implemented selectively.
This choice has two meanings:
First, not all methods in the Abstract class are abstract, only those methods with the title abstract are It is abstract and must be implemented by subclasses. For those methods without abstract, the method body must be defined in the Abstract class.
Second, when a subclass of abstract class inherits it, it can directly inherit or override non-abstract methods; for abstract methods, you can choose to implement them, or you can declare its method as abstract again without having to Implementation is left to its subclasses, but this class must also be declared abstract. Since it is an abstract class, of course it cannot be instantiated.

E. Abstract class is the intermediary between interface and Class.
Interface is completely abstract. It can only declare methods, and only public methods. Private and protected methods cannot be declared, method bodies cannot be defined, and instance variables cannot be declared. However, interfaces can declare constant variables, and it is not difficult to find such examples in the JDK. However, placing constant variables in an interface violates its purpose of existing as an interface, and also confuses the different values ​​​​of interfaces and classes. If you really need it, you can put it in the corresponding abstract class or Class.
abstract class plays a connecting role in interface and Class. On the one hand, abstract class is abstract and can declare abstract methods to standardize the functions that subclasses must implement; on the other hand, it can define default method bodies for direct use or override by subclasses. In addition, it can define its own instance variables for use by subclasses through inheritance.

Application occasions of interface
A. Classes need specific interfaces to coordinate, regardless of how they are implemented.
B. It exists as an identifier that can implement a specific function, or it can be a pure identifier without any interface methods.
C. A group of classes needs to be treated as a single class, and the caller only contacts this group of classes through the interface.
D. Specific multiple functions need to be implemented, and these functions may have no connection at all.

Application occasions of abstract class
In a word, when both a unified interface and instance variables or defaults are needed method, you can use it. The most common ones are:
A. Define a set of interfaces, but do not want to force each implementation class to implement all interfaces. You can use abstract class to define a set of method bodies, or even empty method bodies, and then let subclasses choose the methods they are interested in to cover.
B. In some cases, pure interfaces alone cannot satisfy the coordination between classes. Variables representing states in the class must also be used to distinguish different relationships. The intermediary role of abstract can satisfy this very well.
C. Standardizes a set of coordinated methods, some of which are common, state-independent, and can be shared without the need for subclasses to implement them separately; while other methods require each subclass to implement them separately. Classes implement specific functions based on their specific state.

Benefits of interfaces and abstract classes:

好像定义接口是提前做了个多余的工作。下面我给大家总结了4点关于JAVA中接口存在的意义:

  1、重要性:在Java语言中, abstract class 和interface 是支持抽象类定义的两种机制。正是由于这两种机制的存在,才赋予了Java强大的 面向对象能力。

  2、简单、规范性:如果一个项目比较庞大,那么就需要一个能理清所有业务的架构师来定义一些主要的接口,这些接口不仅告诉开发人员你需要实现那些业务,而且也将命名规范限制住了(防止一些开发人员随便命名导致别的程序员无法看明白)。

  3、维护、拓展性:比如你要做一个画板程序,其中里面有一个面板类,主要负责绘画功能,然后你就这样定义了这个类。

可是在不久将来,你突然发现这个类满足不了你了,然后你又要重新设计这个类,更糟糕是你可能要放弃这个类,那么其他地方可能有引用他,这样修改起来很麻烦。

如果你一开始定义一个接口,把绘制功能放在接口里,然后定义类时实现这个接口,然后你只要用这个接口去引用实现它的类就行了,以后要换的话只不过是引用另一个类而已,这样就达到维护、拓展的方便性。

  4、安全、严密性:接口是实现软件松耦合的重要手段,它描叙了系统对外的所有服务,而不涉及任何具体的实现细节。这样就比较安全、严密一些(一般软件服务商考虑的比较多)。

尽管抽象类和接口之间存在较大的相同点,甚至有时候还可以互换,但这样并不能弥补他们之间的差异之处。下面将从语法层次和设计层次两个方面对抽象类和接口进行阐述。

语法层次

在语法层次,java语言对于抽象类和接口分别给出了不同的定义。下面已Demo类来说明他们之间的不同之处。

使用抽象类来实现:

public abstract class Demo {  abstract void method1();  
      
      void method2(){  //实现      }  
}
Copy after login

使用接口来实现

interface Demo {  void method1();  void method2();  
}
Copy after login

抽象类方式中,抽象类可以拥有任意范围的成员数据,同时也可以拥有自己的非抽象方法,但是接口方式中,它仅能够有静态、不能修改的成员数据(但是我们一般是不会在接口中使用成员数据),同时它所有的方法都必须是抽象的。在某种程度上来说,接口是抽象类的特殊化。

对子类而言,它只能继承一个抽象类(这是java为了数据安全而考虑的),但是却可以实现多个接口。

设计层次

上面只是从语法层次和编程角度来区分它们之间的关系,这些都是低层次的,要真正使用好抽象类和接口,我们就必须要从较高层次来区分了。只有从设计理念的角度才能看出它们的本质所在。一般来说他们存在如下三个不同点:

1、 抽象层次不同。抽象类是对类抽象,而接口是对行为的抽象。抽象类是对整个类整体进行抽象,包括属性、行为,但是接口却是对类局部(行为)进行抽象。

2、 跨域不同。抽象类所跨域的是具有相似特点的类,而接口却可以跨域不同的类。我们知道抽象类是从子类中发现公共部分,然后泛化成抽象类,子类继承该父类即可,但是接口不同。实现它的子类可以不存在任何关系,共同之处。例如猫、狗可以抽象成一个动物类抽象类,具备叫的方法。鸟、飞机可以实现飞Fly接口,具备飞的行为,这里我们总不能将鸟、飞机共用一个父类吧!所以说抽象类所体现的是一种继承关系,要想使得继承关系合理,父类和派生类之间必须存在"is-a" 关系,即父类和派生类在概念本质上应该是相同的。对于接口则不然,并不要求接口的实现者和接口定义在概念本质上是一致的, 仅仅是实现了接口定义的契约而已。

3、 设计层次不同。对于抽象类而言,它是自下而上来设计的,我们要先知道子类才能抽象出父类,而接口则不同,它根本就不需要知道子类的存在,只需要定义一个规则即可,至于什么子类、什么时候怎么实现它一概不知。比如我们只有一个猫类在这里,如果你这是就抽象成一个动物类,是不是设计有点儿过度?我们起码要有两个动物类,猫、狗在这里,我们在抽象他们的共同点形成动物抽象类吧!所以说抽象类往往都是通过重构而来的!但是接口就不同,比如说飞,我们根本就不知道会有什么东西来实现这个飞接口,怎么实现也不得而知,我们要做的就是事前定义好飞的行为接口。所以说抽象类是自底向上抽象而来的,接口是自顶向下设计出来的。

我们有一个Door的抽象概念,它具备两个行为open()和close(),此时我们可以定义通过抽象类和接口来定义这个抽象概念:

抽象类:

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

接口

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

至于其他的具体类可以通过使用extends使用抽象类方式定义Door或者Implements使用接口方式定义Door,这里发现两者并没有什么很大的差异。

但是现在如果我们需要门具有报警的功能,那么该如何实现呢?

解决方案一:给Door增加一个报警方法:clarm();

abstract class Door{  abstract void open();  abstract void close();  abstract void alarm();  
}
Copy after login

或者

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

这种方法违反了面向对象设计中的一个核心原则 ISP (Interface Segregation Principle)(参见:),在Door的定义中把Door概念本身固有的行为方法和另外一个概念"报警器"的行为方 法混在了一起。这样引起的一个问题是那些仅仅依赖于Door这个概念的模块会因为"报警器"这个概念的改变而改变,反之依然。

解决方案二

既然open()、close()和alarm()属于两个不同的概念,那么我们依据ISP原则将它们分开定义在两个代表两个不同概念的抽象类里面,定义的方式有三种:

1、两个都使用抽象类来定义。

2、两个都使用接口来定义。

3、一个使用抽象类定义,一个是用接口定义。

由于java不支持多继承所以第一种是不可行的。后面两种都是可行的,但是选择何种就反映了你对问题域本质的理解。

如果选择第二种都是接口来定义,那么就反映了两个问题:1、我们可能没有理解清楚问题域,AlarmDoor在概念本质上到底是门还报警器。2、如果我们对问题域的理解没有问题,比如我们在分析时确定了AlarmDoor在本质上概念是一致的,那么我们在设计时就没有正确的反映出我们的设计意图。因为你使用了两个接口来进行定义,他们概念的定义并不能够反映上述含义。

第三种,如果我们对问题域的理解是这样的:AlarmDoor本质上Door,但同时它也拥有报警的行为功能,这个时候我们使用第三种方案恰好可以阐述我们的设计意图。AlarmDoor本质是们,所以对于这个概念我们使用抽象类来定义,同时AlarmDoor具备报警功能,说明它能够完成报警概念中定义的行为功能,所以alarm可以使用接口来进行定义。如下:

abstract class Door{  abstract void open();  abstract void close();  
}  
  
interface Alarm{  void alarm();  
}  
  
class AlarmDoor extends Door implements Alarm{  void open(){}  void close(){}  void alarm(){}  
}
Copy after login

 

       这种实现方式基本上能够明确的反映出我们对于问题领域的理解,正确的揭示我们的设计意图。其实抽象类表示的是"is-a"关系,接口表示的是"like-a"关系,大家在选择时可以作为一个依据,当然这是建立在对问题领域的理解上的,比如:如果我们认为AlarmDoor在概念本质上是报警器,同时又具有Door的功能,那么上述的定义方式就要反过来了。

##Abstract class

Interface

Class

An inheritance relationship, a class can only use the inheritance relationship once . Multiple inheritance can be achieved by inheriting multiple interfaces

A class can implement multiple interfaces

Data members

can have their own

Static cannot be modified, that is, it must be static final. Generally, it is not defined here

method

Can be private, non-abstract method, abstract method must be implemented

##It cannot be private, the default is public, abstract type

Variables

can be private, default is friendly Type, its value can be redefined in a subclass or reassigned

cannot be private, the default is public static final type, and must be given to it The initial value cannot be redefined in the implementation class and its value cannot be changed.

Design Concept

means "is- a" relationship

represents the "like-a" relationship

##Implementation

needs inheritance and uses extends

requires implementations

      批注:

   ISP(Interface Segregation Principle):面向对象的一个核心原则。它表明使用多个专门的接口比使用单一的总接口要好。

   一个类对另外一个类的依赖性应当是建立在最小的接口上的。

   一个接口代表一个角色,不应当将不同的角色都交给一个接口。没有关系的接口合并在一起,形成一个臃肿的大接口,这是对角色和接口的污染。

Summary

              1. Abstract classes represent an inheritance relationship in the Java language. A subclass can only have one parent class, but can have multiple interfaces.

2. An abstract class can have its own member variables and non-abstract class methods, but only static and immutable member data can exist in the interface (but generally not in the interface) defines member data), and all its methods are abstract.

3. The design concepts reflected by abstract classes and interfaces are different. Abstract classes represent the "is-a" relationship, while interfaces represent the "like- a" relationship.

Abstract classes and interfaces are two different abstract concepts in the Java language. Their existence provides very good support for polymorphism, although there are great similarities between them. . But their choice often reflects your understanding of the problem domain. Only by having a good understanding of the nature of the problem domain can we make a correct and reasonable design.

The above is the detailed content of What is the interface? What is an abstract class?. 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