Abstract classes and Interfaces

Source: Internet
Author: User

28. Which line of code in the option can replace the title//add code here without generating a compilation error? ()

Public abstract class MyClass {
public int constint = 5; Add code here Public Void Method () { }}

Apublic abstract void Method (int a);

B Constint = Constint + 5;

C public int method ();

D public abstract void Anothermethod () {}

Answer: A

Parsing: Investigating the use of abstract classes.

Principles of abstract class adherence:

(1) The abstract keyword can only be decorated with classes and methods and cannot be decorated with fields. (2) An abstract class cannot be instantiated (an instance of an object cannot be created with the New keyword) and can only be inherited. (3) Abstract classes can include properties, methods, constructor methods, initialization blocks, inner classes, enumeration classes, and ordinary classes, the ordinary method must be implemented, the variable can be initialized or uninitialized but not initialized after the abstract class in the re-assignment or operation of the variable (only in the subclass to change the variable). (4) Abstract methods in abstract classes (methods that add the abstract keyword) cannot be implemented. (5) A class containing an abstract method must be defined as an abstract class. Extension: The difference between abstract classes and interfaces, make a summary:

(1) The interface is public, it cannot have private methods or variables, is used to let others use, and abstract classes can have private methods or private variables.

(2) Abstract class represents an inheritance relationship in the Java language, and a class can only use one inheritance relationship at a time. However, a class can implement multiple interface and implement multiple inheritance. The interface also has the function of identifying (there are no methods, such as remote interfaces) and data sharing (the variables inside are all constants).

(3) The abstract class can have its own data members, but also can have non-ABSTARCT member methods, and in interface, can only have static can not be modified data members (that is, must be static final, but in Data members are not generally defined in interface, and all member methods are the public abstract type by default.

(4) Abstract class and interface reflect the different design concepts. In fact, abstract class represents the "is-a" relationship, interface represents the "has-a" relationship.

(5) The implementation of the interface must be implemented in all the methods defined in the interface, and the implementation of the abstract class can selectively rewrite the required method, the general application, the top-level interface, and then the abstract class implementation interface, finally to the concrete class implementation. Abstract classes can have non-abstract methods. There is no implementation method in the interface.

(6) The variable defined in the interface is the public static final type by default, and must be given its initial value, so the implementation class cannot be redefined or changed. The variables in an abstract class are friendly by default, whose values can be redefined in subclasses or re-assigned in subclasses.

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.