A detailed explanation and example of Java abstract classes and interfaces

Source: Internet
Author: User

Why the interface exists:

Java is single-inheritance, does not support multiple inheritance, but with the interface, Java can implement multiple interfaces;

A class that implements an interface must implement all the methods declared within the interface (forced execution, even if the empty method is implemented);

Interface Features:

1. All methods within an interface are declared only, there is no method body (implicitly declared), for example: Public abstract void func ();

2. All variables within the interface are public static final by default and must be displayed initialized;

3. The interface does not have the method of construction, cannot be instantiated;

4. The class that implements the interface implements all the abstract methods, otherwise the class must be declared as an abstract class, plus the abstract keyword;

5. Interfaces can not implement interfaces (all interfaces are abstract methods), but the interface supports multiple inheritance yo!

So what does an interface have to do with abstract classes?

First, let's meet the abstract class.

1. General variables and general methods are permitted in abstract classes;

2. Abstract classes must have an abstract keyword;

3. Abstract classes can have no abstract methods, but even so, they cannot be instantiated; (abstract methods must be abstract classes)

4. Abstract classes cannot be instantiated, but they are constructed (derived classes can be extended)

All right, so what do you mean by abstract classes, how to use them, and when to use abstract classes???

When there are multiple classes implementing the same interface, each class implements all the methods of that interface (as above: Implementing the interface, all methods of the interface must be implemented);

So if there are several methods, the implementations in these subclasses are exactly the same, we can use abstract classes to separate these methods as public methods;

The public methods of these subclasses (which are implemented exactly the same way) are implemented by an abstract class, and then these subclasses are OK if they inherit the abstract class.

Then the interface and abstract class application logic comes out.

The first step is to write an interface that declares all the methods that implement this interface.

The second step, the implementation of this interface has a common part, with an abstract class to implement, so that the abstract class has all the methods of the Declaration, as well as the implementation of public methods, so simplifies the implementation of the interface

In the third step, derived classes simply inherit and implement the abstract class ok!

If the above text feels, a bit around, then see the code example:

Declare an interface first

  Public interface interexample{//declares an interface public         abstract  void  function1 ();         public abstract  void  function2 ();           public abstract  void  function3 ();         public abstract  void  pubfunct ();  Common method declarations  

Declares an abstract class that implements the above interface

Public abstract class Absexample  implements interexample{      //  Implement common method publicly      void Pubfunction () {          System.out.println ("This is a common method of a derived class");      } //}

Generic class (Inherits derived classes from abstract classes)

public class ExampleA extends absexample{
        public abstract  void  function1 () {
            System.out.println ("A derived class personalization uses Method 1");
        } public        abstract  void  function2 () {
            System.out.println ("A derived class personalization uses Method 2");
        } public          abstract  void  Function3 () {
            System.out.println ("A derived class personalization uses Method 3");
        }
}

  

public class Exampleb extends absexample{public        abstract  void  function1 () {            System.out.println (" Method 1 "For personalization use of the derived class B);        public abstract  void  function2 () {            System.out.println ("Method 2 for personalization using derived Class B");          public abstract  void  Function3 () {            System.out.println ("B derived class personalization used Method 3");}    }

See in the Code: The Public method is: Pubfunction (), implemented in the abstract class, Examplea,exampleb public, there is no need to write in ExampleA, Exampleb two classes of the same implementation code, respectively;

In addition: If the addition of the subclass Examplec,pubfunction () is different from the ExampleA and Exampleb, then it is better to rewrite the method in Examplec and remain flexible.

Re-organize the abstract class and interface, I hope you can answer questions!

A detailed explanation and example of Java 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.