Why Java uses abstract classes and interfaces

Source: Internet
Author: User
Tags naming convention

Java interfaces and Java abstract classes represent abstract types, which are the concrete manifestations of the abstraction layer we need to propose. OOP object-oriented programming, if you want to increase the reuse rate of programs, increase the maintainability of the program, extensibility, it must be interface-oriented programming, oriented to abstract programming, the correct use of interfaces, abstract classes, these too useful abstract types as the top layer of your hierarchy. 1. One of the biggest differences between Java interfaces and Java abstract classes is thatJava Abstract classes can provide partial implementations of some methods, while Java interfaces can not, this is probably the only advantage of the Java abstract class, but this advantage is very useful. If you add a new concrete method to an abstract class, all of its subclasses get the new method all of a sudden, and the Java interface does not do this, and if a new method is added to a Java interface, all classes that implement the interface will fail to compile successfully. Because you have to let every class implement this method again. 2, an abstract class implementation can only be given by the subclass of this abstract class, that is, the implementation in the abstract class is defined by the hierarchy of inheritance, and because of the Java language single inheritance, so the abstract class as a type definition tool performance greatly discounted. At this point, the advantages of the Java interface come out,any class that implements a method defined by a Java interface can have a type of this interface, and a class can implement any number of Java interfaces, so that the class has multiple types. 3, from the 2nd is not difficult to see, Java interface is the ideal tool to define a mixed type, mixed class indicates that a class does not only have the behavior of a primary type, but also has other secondary behavior. 4, the combination of 1, 2 points in the abstract class and Java interface of their respective advantages, with refined design mode is out:the work of declaring type is still assumed by the Java interface, but it is given a Java abstract class, and this interface is implemented, and the other specific classes that belong to this abstract type can choose to implement this Java interface, or you can choose to inherit this abstract class, that is, in the hierarchy, Java interface at the top, followed by the abstract class, ha, the next two of the biggest advantages can be played to the extreme. This mode is "default adaptation mode". This pattern is used in the Java language API, and all follow a certain naming convention: Abstract + interface name. Java interfaces and Java abstract classes exist to be used for the implementation and inheritance of specific classes, and if you are going to write a specific class to inherit another specific class, then your design has a big problem.The Java abstract class exists for inheritance, and its abstract approach is to force subclasses to be implemented. Use Java interfaces and abstract Java classes for variable type declarations, arguments for type declarations, method return type descriptions, and conversion of data types. Instead of using specific Java classes to declare variable types, arguments are type declarations, method return type descriptions, and data type conversions. I think if you make up the code with an interface and the abstract class is not, perhaps I can say you do not use any design mode, any design pattern is inseparable from the abstract, and abstract and Java interface and abstract Java class is inseparable.The role of the interface, Word, is the category of the flag class。 By attributing different types of classes to different interfaces, you can better manage them.classify a group of unrelated classes as an interface to invoke. You can use an interface type variable to refer to an object, which is what I think is the biggest function of the interface.My own feelings in normal Java programming, using JDBC to connect to the database is very common. And this involves drivermanager,connection,statement, the first of which is the class, and the latter is the interface. The connection is used to obtain a connection that specifies a database. This database is specified at the beginning of the program or in a configuration file. Then the specific database connection object based on the specified database can be obtained by drivermanager.getconnection. Well, The key to the problem is here, in a future program, the connection that we use is an object referenced by this interface. It can be either an Oracle database connection object or a SQL Server connection object. But looking at internal procedures, We don't know what type it is. Because of the interface. It shows us the connection type. No matter what database we change, the program always connection conn= ... But if we don't have to connection the interface, and switch to a specific class, then if we just use a database like SQL Server, Then we'll use this sqlserverconnection class to instantiate an object and invoke it in the program. But suppose one day we're going to switch to MySQL database? So, The program uses all the sqlserverconnection to be replaced by mysqlconnection, and the method may fail. This is the advantage of the interface, if we use the interface, we do not have to control the program is specifically in the invocation of which class, All I need to know is to invoke a class that has some kind of common property. The designation of this class is given to the factory class. Within the program, all we can see is a call to the interface. This interface represents a specific implementation class. Now learn the MVC pattern. Enables web development in a multi-tiered manner. These layers are closely related to the model layer, the persistence layer, and then the underlying database. Bo,dto,vo is required in the model layer. And the persistence layer is the DAO class. But according to the large project structure. Each layer should pass through the interface. This is more important. The purpose of the interface is to reduce the degree of coupling between the layers. In this way, the lower layer exposes the interface only to the upper layer. and closed the internal implementation. This is the benefit of 1. The second is when the implementation of the interface changes. The calling code of the upper layer is not changed. Last point. Is the advantage of the interface itself, that is an interface, a variety of implementations. The specific use of that implementation is specified by the factory. So in case the implementation changes, but also only to change the factory, do not change the program.

Why Java uses abstract classes and interfaces

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.