Java review: Use a demo to illustrate the simple framework model of inheriting abstract classes and implementing interfaces

Source: Internet
Author: User


As we all know, in Java application development, "interface-oriented programming".
So what is an interface? What is the function of the interface? How is the interface used? Let's take a look back.

[Declaration] Welcome reprint, but please keep the article original source: http://blog.csdn.net/yelangjueqi/article/details/44701369

1, Interface review:

The concept of interface in 1.1,java
The interface in Java is a special kind of abstract class, compared with the general abstract class, all the methods inside the interface are abstract methods, all the properties inside the interface are constants. In other words, there is only a method definition in the interface and no method is implemented.

1.2, what is the interface for?
The interface is typically used to define the appearance of the implementation class, which is the behavior definition of the implementation class, which is used to constrain the behavior of the implementation class. Interface is equivalent to a contract, according to the external application needs of the function, agreed to implement the class should be implemented functions, but the specific implementation class in addition to implement the interface contract functions, but also to implement other functions as required, which is allowed, that is, the implementation of the function of the class is not limited to the interface constraints of the function.
By using interfaces, you can implement the same behavior for unrelated classes. Instead of having to consider the hierarchical relationships between these classes, an interface is an implementation of the external appearance of the class.

1.3, the idea of the interface
Based on the function and use of the interface, the idea of the interface is "encapsulation isolation".
The commonly mentioned encapsulation refers to the encapsulation of the data, but the encapsulation here refers to "encapsulation of the behavior of the quarantined body", or "encapsulation of the responsibility of the quarantined body", while isolation refers to the external call and internal implementation, the external call can only be called through the interface, external calls do not know the internal implementation of the concrete, This means that external calls and internal implementations are isolated by the interface.

1.4. Benefits of using interfaces
Because external calls and internal implementations are separated from the interface, so long as the interface is unchanged, internal implementation changes will not affect the external application, which makes the system more flexible, with better scalability and maintainability, this is the so-called "interface is the system pluggable guarantee" the meaning of the phrase.

1.5. Selection of interfaces and abstract classes
Since the interface is a special kind of abstract class, in development, when to choose the interface? When do you use abstract classes?
For their choice, in the development is a very important issue, in particular summed up two sentences to everyone;
A, preferential selection of interface
B, you should choose an abstract class when you need to define the behavior of subclasses and also provide common functionality for subclasses.


2, Simple frame review:
Here are a few of the most commonly used framework structures (the second is actually a variant of the first frame structure, in contrast, separately)
2.1, The first framework structure: a framework that inherits both abstract classes and interfaces (interfaces are a special kind of abstract class):
2.1.1, interface:
Public interface Iapi {
public void Add ();

public void Remove ();

public void update ();
}


2.1.2, abstract class
Public abstract class Aapi {
public abstract void Testadd ();

public abstract void Testremove ();

public void Testupdate () {
System.out.println ("Aapi/testupdate");
}
}

2.1.3, Implementing Class
public class Apiimpl extends Aapi implements IAPI {


First case: Declaring with an abstract class, creating an instance object with subclasses of the abstract class
public static void Main (string[] args) {
AAPI declaration with an abstract class, and a subclass Apiimpl creates an instance object Miapi can onlyCall abstract class Aapi inside the method (subclass Apiimpl New Method TESTIPML () cannot call, nor can call the method in the interface), or compile error: The method testipml () is undefined for the type Aapi

Aapi Miapi = new Apiimpl ();
Miapi.testupdate ();
MIAPI.TESTIPML ();//Compile Error
}

Second case: Using an interface to declare, create an instance object with the implementation class of the interface
public static void Main (string[] args) {
An instance object is created with an interface IAPI declaration and an implementation class Apiimpl, which Miapi can onlyCall the method inside the interface Iapi (implement class Apiimpl new method TESTIPML () cannot call, also cannot call abstract class Aapi inside method), otherwise compile error: The method testipml () is undefined for the type Aapi, the phenomenon is the same as the first case, because the interface is a special kind of abstract class.

Iapi Miapi = new Apiimpl ();
Miapi.add ();
MIAPI.TESTIPML ();//Compile Error
}

The third case is declared with the subclass of the abstract class (or the implementation class of the interface), creating the instance object with the subclass of the abstract class (or the implementation class of the interface)
public static void Main (string[] args) {
A subclass (or implementation Class) Apiimpl to declare that a subclass (or implementation Class) Apiimpl create an instance object Miapi can invoke methods inside an abstract class and invoke methods inside the interface, or call its own new method TESTIPML ().
Apiimpl Miapi = new Apiimpl ();
Miapi.add ();
Miapi.testupdate ();
MIAPI.TESTIPML ();
}


public void testipml () {
System.out.println ("APIIMPL/TESTIPML");
}

@Override
public void Testadd () {
System.out.println ("Apiimpl/testadd");
}

@Override
public void Testremove () {
TODO auto-generated Method Stub
System.out.println ("Apiimpl/testremove");
}

@Override
public void Add () {
TODO auto-generated Method Stub
System.out.println ("Apiimpl/add");
}

@Override
public void Remove () {
TODO auto-generated Method Stub
System.out.println ("Apiimpl/remove");
}

@Override
public void Update () {
TODO auto-generated Method Stub
System.out.println ("Apiimpl/update");
}
}

according to the above demo summary:
(1) An instance object declared with an abstract class or interface, which can onlyCall the abstract class or the interface defined inside the method, not in the abstract class or interface definition of methods, can not be called, or compile error.
(2), a subclass or an instance object declared by the implementation class can call the method defined inside the abstract class and invoke the method defined inside the interface, or call its own new method.
(3), abstract class or interface can not be directly instantiated with itself, such as the following, will compile error:
Aapi Miapi = new Aapi ();//Compile Error: Cannot instantiate the type Aapi
Iapi Miapi = new Iapi ();//Compile Error: Cannot instantiate the type Iapi
Thus, an abstract class (or interface) can be instantiated only through subclasses (or implementation classes).

2 . 2, the second framework structure: first the abstract class implements the interface, then inherits the abstract class(It is actually a variant of the first frame structure, not recommended in this way)
To change the above demo, the following:
2.2.1, Interface:
Public interface Iapi {
public void Add ();

public void Remove ();

public void update ();
}

2.2.2, abstract class (implements interface): Implements the partial interface method, other interface method subclasses must give the implementation
Public abstract class Aapi implements IAPI {

public abstract void Testadd ();

public abstract void Testremove ();

public void Testupdate () {
System.out.println ("Aapi/testupdate");
}


@Override
public void Add () {//interface method
TODO auto-generated Method Stub

}
}


2.2.3, subclass (inheriting abstract class)
public class Apiimpl extends Aapi {

public static void Main (string[] args) {

}

public void testipml () {
System.out.println ("APIIMPL/TESTIPML");
}

@Override
public void Testadd () {
System.out.println ("Apiimpl/testadd");
}

@Override
public void Testremove () {
TODO auto-generated Method Stub
System.out.println ("Apiimpl/testremove");
}

@Override
public void Remove () {//interface method
TODO auto-generated Method Stub

}

@Override
public void Update () {//interface method
TODO auto-generated Method Stub

}
}

2.3, the third frame structure: inherit only abstract class (effect is equivalent to the second frame structure, which is generally used in this way)
2.3.1, abstract class:
Public abstract class Aapi {

public abstract void Testadd ();

public abstract void Testremove ();

public abstract void Add ();

public abstract void Remove ();

public abstract void Update ();

public void Testupdate () {
System.out.println ("Aapi/testupdate");
}
}

2.3.2, sub-category:
public class Apiimpl extends Aapi {

public static void Main (string[] args) {

}

public void testipml () {
System.out.println ("APIIMPL/TESTIPML");
}

@Override
public void Testadd () {
System.out.println ("Apiimpl/testadd");
}

@Override
public void Testremove () {
TODO auto-generated Method Stub
System.out.println ("Apiimpl/testremove");
}

@Override
public void Add () {
TODO auto-generated Method Stub
}

@Override
public void Remove () {
TODO auto-generated Method Stub
}

@Override
public void Update () {
TODO auto-generated Method Stub
}
}

2.4, fourth frame structure: Implement Interface only
2.4.1, Interface:
Public interface Iapi {
public void Add ();

public void Remove ();

public void update ();
}

2.4.2, implementation class:
public class Apiimpl implements IAPI {

public static void Main (string[] args) {

}

public void testipml () {
System.out.println ("APIIMPL/TESTIPML");
}

@Override
public void Add () {
TODO auto-generated Method Stub

}

@Override
public void Remove () {
TODO auto-generated Method Stub

}

@Override
public void Update () {
TODO auto-generated Method Stub

}
}


Summarize:
the above 4 kinds of frame structure each has the characteristic, the analysis is as follows (can choose according to the actual situation and the actual need):
The first framework: the method implementation of the interface is all placed in the implementation class.
The second framework: the method implementation of the interface can be part of the abstract class, some can be placed in the implementation of the class, not in the abstract class to implement the method must be in the subclass to the interface method implementation; The interface method implemented in the abstract class provides common functionality for all child class instances. It is the same as redefining a method within an abstract class to provide the same functionality. Because the second frame structure is similar to the third frame structure from the flexibility and extensibility, and the second frame structure is redundant to implement an interface with respect to the third frame structure, it is somewhat superfluous to imply that the method that requires the subclass implementation is declared abstract on the abstraction class. There is no need to declare it in the interface), so it is generally used third Framework Structure, the second frame structure is generally not used.

So:
1. If you need to provide common functionality for subclasses, you should choose an abstract class instead of an interface.
2, the interface implements the same behavior of unrelated classes. If the system is very complex, there may be a lot of similar characteristics of the class, while there are many classes that do not have similar characteristics, and those with similar characteristics of the class and does not have similar characteristics of the class and there are some common behavior. At this point, for those classes with similar characteristics can abstract an abstract class, and the same behavior on them can be abstracted out an interface, then the class with similar characteristics of the final implementation of the way is The first kind of frame structureFor classes that do not have similar characteristics, the last way to achieve this is fourth Framework Structure









Java review: Use a demo to illustrate the simple framework model of inheriting abstract classes and implementing 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.