Abstract method Specific method hook method

Source: Internet
Author: User


The basic methods can be divided into three kinds: abstract methods (abstraction method), specific methods (concrete method) and hook method (hook methods).

This is what I see in the "Java and Schema" book, it is very new, because in my impression is not in this way to the other side of the classification of the law, it may be called in the design pattern.

These three methods are also mentioned in the book's Templatemethod mode, so it is essential to take the template method again.

First, the basic definition of the three methods:
• Abstract methods: declared by an abstract class, implemented by a specific subclass. In the Java language, an abstract method is marked with the abstract keyword.
• Concrete methods: declared and implemented by abstract classes, and subclasses are not implemented or overwritten. In fact, it is the general method, but do not need subclasses to implement.
• Hook method: declared and implemented by abstract classes, and subclasses are extended. Usually an abstract class gives an empty hook method, which is a method that is not implemented. In fact, it and the specific method in the code is no different, but a difference in consciousness.

See its application in the template method:


Look at the code:
AbstractClass1 Code:

/**
* Abstract Parent class, define algorithm skeleton and basic method
* @author The5fire
*
*/
Abstract class AbstractClass1 {

/**
* Specific Methods
*/
public void Concretemethod () {
System.out.println ("Web site opens a WordPress site.") ");
System.out.println ("First master Station file". ");
This.hookmethod ();
}

/**
* Abstract method
*/
protected abstract void Abstractmethod ();

/**
* Hook method
*/
public void Hookmethod () {
/**
* I can add a default action, or I can not write
*/

System.out.println ("Execute default method, load Default theme");

}

}


Code for Subclass ConcreteClass1:

/**
* Basic method of implementing abstract parent class
* @author The5fire
*
*/
public class ConcreteClass1 extends AbstractClass {

@Override
protected void Abstractmethod () {
TODO auto-generated Method Stub
SYSTEM.OUT.PRINTLN ("Implement abstract Method");
}

/**
* Rewrite Hook method
*/
public void Hookmethod () {
System.out.println ("Use The5fire specific theme");
}

}


The result of this final output is:


If the hookmethod is not realistic in subclasses, it is:


As can be seen from the code, the so-called hook method and abstract method are sometimes indistinguishable, that is, when the subclass needs to implement it. The difference is that the abstract method must be implemented, and the hook method may not be implemented. This means that the hook method gives you the option to implement an abstract class.

WordPress is the theme of the first, just start up their own without installing the theme when it will give you a default.


----EOF-----

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.