Usage and differences of abstract classes and interfaces in Java

Source: Internet
Author: User

Abstract Class 1, abstract class

A class that contains an abstract method is an abstract class

2. Abstract methods

Methods that are declared without being implemented, the abstract method must be declared using the abstract keyword Word

Public abstract class People {  //keyword abstract, declaring the class to be an abstraction of the public    int. int age;    public void Num () {    } public    abstract Name ();//Declare the method to be an abstract method}

3, abstract class quilt inheritance, subclasses (if not abstract classes) must override all abstract methods in the abstract class

4, abstract class can not be directly instantiated, to be instantiated through its subclasses

5. As long as an abstract class containing an abstract method, the method must be defined as an abstract class, whether or not there are other methods included.

6. Abstract methods in subclasses cannot have the same name as the abstract methods of the parent class.

7. Abstract cannot be modified in the same class as final.

8. Abstract cannot be modified with private, static, final, or native the same method.

Second, the interface

1, interface is the most important concept in Java, interface can be understood as a special class, all of which are composed of global constants and public abstract methods

2, the format of the interface

Interface interfacename{    Global constants    Abstract method}

3, the implementation of the interface must also be through the subclass, using the keyword implements, and the interface can be implemented more

Class A implements inter1,inter2{//inter1 and Inter2 are interfaces    ...}

4. A class can inherit abstract classes and interfaces at the same time

Class A extends Abs implements inter1,inter2{//abs is an abstract class ...    }

5, an interface cannot inherit abstract class, but can inherit multiple interfaces through extends keyword, realize multi-inheritance of interface

Interface Inter implements inter1,inter2{//inter, Inter1, Inter2 are all interfaces ...    }

  

III. abstract class and interface differences

This section is referenced from (Http://www.cnblogs.com/chenssy/chenssy)

(a) Grammatical level
Public abstract class People {  //keyword abstract, declaring the class to be an abstraction type    void Num ();        abstract void Name ();  Declares that the method is an abstract method}interface person {void Num (); void Name ();}
    • Abstract class mode, abstract class can have any range of member data, but also can have their own non-abstract methods,
    • But in the interface mode, it can only have static, cannot modify member data (but we generally do not use the member data in the interface), and all of its methods must be abstract .
    • In a way, an interface is a specialization of an abstract class.
    • In the case of a subclass, it can inherit only one abstract class (which is considered by Java for Data security), but it implements multiple interfaces.
(ii) Design level

1. Different levels of abstraction

An abstract class is an abstraction of a class, and an interface is an abstraction of the behavior. An abstract class is an abstraction of the whole class as a whole, including properties, behaviors, but an interface that abstracts the local (behavior) of a class.

2, cross-domain different

An abstract class spans a class that has similar characteristics, whereas an interface can span different classes of domains . we know that the abstract class is discovering the public part from the subclass, and then generalizing it into an abstract class, and the subclass inherits the parent class, but the interface is different. The subclass that implements it can have no relationship, in common. For example, cats and dogs can be abstracted into an abstract class of animals, with a method called. Birds, airplanes can achieve flying fly interface, with the behavior of flying, here we can not be birds, airplanes share a parent class bar! So the abstract class embodies an inheritance relationship, in order to make the inheritance relationship reasonable, there must be a "is-a" relationship between the parent class and the derived class, that is, the parent class and the derived class should be the same in nature. For an interface, it does not require that the implementation of the interface and the interface definition are inherently consistent in concept, but only the contract that implements the interface definition.

3. Different design levels

For the abstract class, it is designed from the bottom up, we have to know the subclass to abstract out the parent class, and the interface is different, it does not need to know the existence of subclasses, only need to define a rule, as to what sub-class, when how to implement it all do not know. For example, we only have a cat class here, if you are abstract into an animal class, is not the design a bit excessive? We have at least two animals, cats, dogs here, we are in the abstract what they have in common to form an animal abstract class it! So, abstract classes are often reconstructed by refactoring! But the interface is different, for example fly, we do not know what will be to achieve this flight interface, how to realize it is not known, we have to do is to define the pre-flight behavior interface. So the abstract class is a bottom-up abstraction, and the interface is designed from the top down.

Usage and differences of abstract classes and interfaces in Java

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.