Java abstract classes and interfaces

Source: Internet
Author: User

Abstract classes and interfaces, have been relatively easy to confuse, the characteristics are very similar, but the details are not the same, summarized as follows.

First, you need to understand what an abstract method is.

The abstract method is that only methods that declare no method body are declared with a joint from the abstract, for example

1 Abstract returntype funname (Type t1,type2,t2 ...); 2 // For example 3 Abstract int Help (int k);;

The class that contains the abstract method must be an abstract class. However, abstract classes can have no abstract methods , or they can use their own class variables (static adornments) and instance variables (not static, but outside the method body in the class).

Abstract classes can be inherited, but cannot be instantiated by new. The following appears to be instantiated by new, in effect generating a subclass, implementing the abstract method, instantiating the subclass, and the subclass has no name.

Other words:

An abstract class can actually be instantiated, but instead of creating an object through new, the instantiation of the parent class is indirectly implemented by referencing the parent class to an instance of the subclass (because the child class is bound to instantiate his parent class before it is instantiated). This creates an object that inherits the subclass of the abstract class, and then instantiates its parent class (abstract class).

1  Public Abstract classBowlinggame {2      Public intA =1;3     Abstract intHelpintk);;4     5This usage is also called Anonymous inner class6      Public Static voidMain (String args[]) {7Bowlinggame BL =NewBowlinggame () {8 9 @OverrideTen             intHelpintk) { One                 //TODO auto-generated Method Stub A                 return0; -             }}; -Bl.a=3; theSystem.out.println (BL.A);}

There are a few things to keep in mind when using abstract classes:

1, abstract class can not be instantiated, the work of the instantiation should be left to its subclasses to complete, it only need to have a reference.

2. Abstract methods must be overridden by subclasses.

3. 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.

4, the abstract class can contain specific methods, of course, can not contain abstract methods.

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

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

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

Interfaces: interfaces are abstract abstractions, all of which are non-implemented methods that allow the creator to determine the method name, the argument list, and the return type, but there is no method body. is an extremely abstract class. Implemented with implement. Java is single-inheritance, inheriting only one parent class, but can implement multiple interfaces. This is the advantage of an interface relative to an abstract class.

Interface are born to be implemented, so they are public by default. Imagine, declare them as private protected, can not be realized what the meaning of them!!

There are several issues to be aware of when using the interface:

1, a interface of the party all legal access rights are automatically declared public. Only public, of course, you can display the declaration as protected, private, but the compilation will be wrong!

2, the interface can be defined as "member variables", or immutable constants, because the interface in the "member variable" will automatically become public static final. Direct access is possible through the class name: Implementclass.name.

By default the member variable is public static final

3, there is no method of implementation in the interface. Abstract classes can have implementation methods, abstract classes can not even abstract methods

4. The non-abstract class that implements the interface must implement all methods of the interface. Abstract classes can be used without implementation.

5. You cannot instantiate an interface with the new operator, but you can declare an interface variable that must reference (refer to) An object of the class that implements the interface. You can use instanceof to check whether an object implements a particular interface. For example: if (anobject instanceof comparable) {}.

6, in the implementation of multi-interface must avoid the repetition of the method name.

Reference: http://blog.csdn.net/chenssy/article/details/12858267

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.