Java interface-Abstract class parsing

Source: Internet
Author: User

For object-oriented programming, abstraction is one of its three main features (abstraction, inheritance, polymorphism). In Java, there are two ways to embody the abstraction of OOP: interfaces and abstract classes.

The two are both similar and different. You will be silly when you are beginners the difference between the interface and the abstract class, most of them are at random use, but it is not.

First, abstract class
  classes declared with the abstract keyword are abstract classes . This is not a conflict with the notion that a class containing abstract methods is an abstract class, and an abstract method is a special method: it only declares, without a specific implementation. Abstract class declarations that contain abstract methods are as follows:

 Public Abstract class helloabstractor {        abstractvoid  sayhi ();}

Abstract classes must be decorated with the abstract keyword before the class, and the abstraction method is also the same. Abstract classes cannot be instantiated because of abstraction.

In the Java programming idea, it is not true that abstract classes are defined as "classes containing abstract methods", and at least irresponsible. We can arbitrarily declare an abstract class that does not contain abstract methods, as follows:

 Public Abstract class helloabstractor {    //  abstract void Sayhi ();     Public void Sayhi () {        System.out.println ("Hello world!" );    }}

  

There is absolutely no problem with the above abstract class declaration, and perhaps someone might think that it is useless to do so, completely meaningless. I beg to differ from those who do not agree with these ideas, for ignorant, ignorant and fearless people, just want to say stupid.

Anyway

 Package com.charles.common.utils;  Public Abstract class commonutils {        publicstaticvoid  main (string[] args) {        = " Charles ";        SayHello (name);    }      Public Static void SayHello (String name) {        System.out.println ("Hello" + name);    }    }

For the above statement, to remove the abstract keyword above, I believe a lot of people are familiar with, here, I use the above code to explain why a class does not define an abstract method is declared as an abstract class, its (here) for a class that does not need to be instantiated, it can be declared as abstract, For example, we often define the tool class, constant class and so on.

Abstract classes, like normal classes, can also have member variables and normal member methods. Note that there are three main differences between abstract and ordinary classes:

1) The abstract method must be public, one of the protected, or the compilation will not pass by default to public.

Explanation: The abstract method is declared private, it cannot be inherited by the quilt class, and the subclass cannot implement the method

2) Abstract classes cannot be used to create objects;

Explanation: Just because the abstract

3) A class inherits an abstract class, and if the parent class contains an abstract method, the child class must implement the abstract method of the parent class, otherwise the subclass must be declared as an abstract class.

In other respects, there is no difference between an abstract class and an ordinary class.

Two. Interface
Interface is interface, in software engineering, the interface refers to the method or function for others to call. From here, we can realize the original intention of the Java language Designer, which is the abstraction of the behavior. In Java, the form of an interface is as follows:

 Package Com.charles.spring.service;  Public Interface timertask {        voidthrows  Exception;}

  

The interface can contain variables and methods, and the variables in the interface are implicitly specified as public static final variables (and can only be public static final variables, which are reported as a compilation error with private adornments), and methods are implicitly specified as public Abstract method and can only be public abstract method (with other keywords, such as private, protected, static, final and so on will be reported compile errors), and the interface of all methods can not have a concrete implementation, that is to say, The methods in the interface must all be abstract methods. From here we can see the difference between interface and abstract class, interface is an extremely abstract type, it is more "abstract" than abstract class, and generally does not define variables in the interface.

Interface implementation:

 Package Com.charles.spring.service.impl; Import Com.charles.spring.service.TimerTask;  Public class Implements timertask {    @Override    publicvoidthrows  Exception {        System.out.println ("Hello Timer");}    }

As you can see, a class is allowed to follow a specific interface. If a non-abstract class follows an interface, you must implement all the methods in that interface. Abstract methods in the interface may not be implemented for abstract classes that follow an interface.

 Package com.charles.common.utils; Import Com.charles.spring.service.TimerTask;  Public Abstract class Implements timertask {    @Override    publicabstractvoidthrows Exception;}

Three. The difference between abstract classes and interfaces

1. Differences in the grammatical level

1) Abstract classes can provide implementation details of member methods, and only public abstract methods exist in interfaces;

2) member variables in an abstract class can be of various types, whereas member variables in an interface are only public static final types;

3) The interface cannot contain static code blocks and static methods, while abstract classes can have static code blocks and static methods;

4) A class can inherit only one abstract class, while a class may implement multiple interfaces.

2. Differences at the design level

  1) Abstract class is an abstraction of a thing, that is, an abstraction of a class, and an interface is an abstraction of a behavior. abstract classes are abstractions of the whole class as a whole, including properties, behaviors, and interfaces that abstract the local or behavior of a class.

  2) The design level is different, abstract class as a lot of subclasses of the parent class, it is a template design, interface is a code of conduct, it is a radiation design. in short, the template design is to modify the template/abstract class, the subclass automatically contains related changes, radiation design is to modify the interface, the implementation of its subclasses need to make changes.

Java interface-Abstract class parsing

Related Article

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.