Interfaces and abstract classes, silly you could not tell?

 


01、

Look on the network interface for some explanation:

Interface (English: Interface), is an abstract type in the Java programming language, is a collection of abstract methods. Interfaces inherit a class manner, thereby inherit the abstract interface methods.

Brothers, how you look, this interpretation was around me dizzy, like drink a pound Erguotou. In the end it is an abstract class or interface to explain it? stupidly can not tell.

Confused about abstract classes or interfaces, take a look at the difference between the two. To abstract classes and interfaces, you both come Bibi height.

A method abstract class can have a method thereof, to achieve specific functions to be implemented method, but the method is not an interface, no method thereof. 2. abstract class member variables may be of various types, but the interface member variables are only  public static final type, and is implicit default. 3. The interface block can not contain a static and static method (using  static a modified method), but there may be an abstract class is a static code blocks and static methods. 4. A class can inherit an abstract class, and a class they can implement multiple interfaces.

02、

They seem to know the difference between the two, but the impression is still somewhat obscure. It does not matter, we have further deepened.

Abstract class

Abstract class embodies the abstract idea of ​​data (or else it), is a mechanism to implement polymorphism. Abstract class defines a set of abstract methods, the specific form of the set of abstract methods inherited by subclasses to achieve.

An abstract class is used to inheritance, it does not make any sense otherwise exist. For example, we define an abstract class author.

abstract class Author {    abstract void write ();    public void sleep () {        System.out.println("吃饭睡觉打豆豆");    }}

As a writer, their job is to engage in writing, the other time to eat and sleep play Peas; but as to what kind of work to write, depends on which one of the authors. For example, silence the king's work must be able to write humorous.

public class Wanger extends Author {    @Override    void write() {        System.out.println("沉默王二的作品《Web 全栈开发进阶之路》,读起来轻松惬意");    }}

Not noticed? An abstract class can have its own way, but its subclasses can inherit ignored.

interface

Interface is a more abstract than the abstract class "class", after all, is a key  interface statement, not with  class.

The interface is just a form, just like a paper contract itself can not do anything. But as long as a class implements this interface, you have to act in accordance with this paper contract: an interface method must be mentioned in the full realization, one less will not work (subclass of the abstract class non-abstract method can be ignored). For example, let's define an interface Beihang publishing contract.

interface ContractBeihang {    void scriptBeihang();}

Once the author signed a contract, then it must periodically complete a certain amount of manuscript.

public class Wanger extends Author implements ContractBeihang {    @Override    void write() {        System.out.println("作品《Web 全栈开发进阶之路》,读起来轻松惬意的技术书");    }    @Override    public void scriptBeihang() {        System.out.println("一年内完成书稿啊,不然要交违约金的哦。");    }}

Interface is a supplement abstract class, Java in order to ensure the security of data does not allow multiple inheritance, that is a class while allowing only inherit a parent class (Why? Search for the keyword "diamond problem").

But different interfaces, a class can implement multiple interfaces at the same time, you can not have much relationship between these interfaces (abstract classes can not make up for the deficiencies of multiple inheritance). For example, not only silence the king signed a contract Beihang Press, and 51CTO also signed a contract fee-paying courses.

public class Wanger extends Author implements ContractBeihang, Contract51 {    @Override    void write() {        System.out.println("作品《Web 全栈开发进阶之路》,读起来轻松惬意的技术书");    }    @Override    public void scriptBeihang() {        System.out.println("一年内完成书稿啊,不然要交违约金的哦。");    }    @Override    public void script51() {        System.out.println("王老师,先把 Java 云盘的大纲整理出来。");    }}

03、

By the example cited above, is not a relatively clear understanding of the interfaces and abstract classes? If not, Come, let us compare the differences between interfaces and abstract classes.

Exactly when to use interfaces, abstract classes when to use it?

1, showing a relationship of abstract class "is-a", and the interface indicates that the "like-a" relationship. That is, if B is a class A (silent king is an author), then A should use an abstract class. Class A and B only if there is a relationship, then A should use the interface.

2, if you want to have your own member variables and non-abstract methods, abstract classes are used. Static interface can only exist immutable member variable (but generally not the definition of member variables in the interface).

3, add any interface method (abstract), all its implements this interface, new methods must be implemented, otherwise the compilation error. For an abstract class, if a non-abstract method to add, but you can sit back and enjoy its subclasses, do not have to worry about the compiler will be problems.

4, abstract classes and interfaces are very similar, careful judgment. Java 1.8 version from the start, tried to introduce a default methods and static methods to interfaces in order to reduce the difference between abstract classes and interfaces. In other words, more and more difficult to distinguish between the two.

04、

In the actual development and application of them, I used much abstract class (which is really big truth); the interface I'd have to use find many, like this child:

public interface CityMapper {    @Select("select * from city")    List<City> getCitys();}

@Insert, @Update, @Delete, @Select Is known as  Mybatis syringe comment.

I am not suddenly feel a little ignorant? Before still talking about interfaces and abstract classes, how all of a sudden jump to  Mybatis the top of it? What mapping annotations?

Ah, that's right. All theoretical knowledge must be applied in practice, otherwise there will be no existence. In my practice among applications, most of the interface is to use  Mybatis the  Mapper interface.

MyBatis It is an excellent persistence framework that supports custom SQL, stored procedures and advanced mappings. MyBatis It avoids almost all JDBC code and manual setting parameters and obtaining the result set. MyBatis You can use simple XML or annotation (what you saw in the previous four CRUD notes) to configure and mapping primitive types, interfaces, and  Java the POJO (Plain Old Java Objects, plain old Java object) is recorded in the database.

When we configured the  MyBatis after environment, you can directly call the injector interface via the following statement.

@Servicepublic class CityService {    @Autowired    private CityMapper cityMapper;    public void init() {            List<City> citys = cityMapper.getCitys();        }    }}

In the injector interface, but also there will be only those related abstract method to query the database, as you can see  List<City> getCitys();. A + syringe injector interface can annotate CRUD database, you are not feeling very magical?

05、

The purpose of this article is to help readers understand and grasp more abstract class, interface features, as well as various usage scenarios, through the efforts of my entire article, I'm sure you gained if - and this is my writing the most powerful engine. Finally, thank you for reading oh.

 

Java technology geeks public number, is founded by a group of people who love Java technology development, focused on sharing of original, high-quality Java articles. If you feel that our article is not bad, please help appreciated, watching, forwarding support, encourage us to share a better article.

No public concern, we can reply, "blog Park" No background in public, freely available knowledge of Java / information interview must-see.

 

Guess you like

Origin www.cnblogs.com/justdojava/p/11211589.html