Some basic features of Java object-oriented

Source: Internet
Author: User

 

Encapsulation

1. The object-oriented language uses class to encapsulate attributes and methods.

2. Attributes must be defined as private and encapsulated for protection.

Examples of inheritance can be seen everywhere. It must conform to the is-a relationship. The parent class is more common and the Child class is more specific ..

Use the super keyword in the subclass:

1. Call the constructor of the parent class to use super

2. You can use super in the subclass to call methods in the parent class.

Polymorphism:

1. open/close principle: You can add new code instead of modifying the original code to use the required changes. Disable modification and enable addition.

2. Subclass can assign a value to the parent class unconditionally.

Implementation of polymorphism:

1. Common class

2. abstract class

3. interfaces (recommended );

 

Java constructor:

Java constructor is a special method in Java classes:

1. This method must have the same name as the class,

2. This method does not return values (neither does void)

3. This method cannot be called manually. It is automatically called by the system when a class instance is created.

4. A class can have no custom constructor. It will use the system's implicit lunch constructor, or it can customize multiple constructor methods, which constitute heavy loads between them and can be called to each other.

5. If a self-defined constructor with parameters is set, the system's default constructor without parameters will be evaluated.

6. constructor cannot be inherited. It determines that the constructor cannot rewrite the constructor. Directly subclass calls the parent class method and uses the super keyword.

 

Interfaces and abstract classes in Java

1. The methods and attributes in the interface must be public.

2. A set of method features, but there is no way to implement it.

3. A class can implement one or more interfaces and can inherit only one class.

4. interfaces are not implemented in a way that cannot be reused. Abstract classes can be used for reuse purposes,

Abstract class features;

1. It cannot be instantiated. It is consistent with the interface and cannot be instantiated.

2. Only constants and abstract methods can be defined in the interface. The non-notification between abstract classes and normal classes only exists in the abstract methods. Inheritance and abstract classes must implement the abstract methods in the inherited classes.

3. An abstract class is an undefined complete class. The abstract method will be delayed to the subclass implementation.

4. abstract class + subclass implementation abstract prevention = complete class,

A few simple questions and answers:

1. Can an abstract class have no abstract methods or methods? It can be the same as a complete class, but the meaning of the abstract class is lost.

2. There may be multiple abstract methods, and the abstract methods must not be implemented.

3. Can a common class contain abstract methods? Of course not. If yes, it will become an abstract class.

4. The subclass of an abstract class must implement abstract methods in the abstract class. It is not necessarily true. Its subclass can be an abstract class,

5. Must the interface implementation class implement multiple defined methods in the interface? If the interface implementation class does not implement the abstract method in the interface, it must be an abstract class

6. interfaces can be inherited.

 

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.