Java object-oriented basics-Interview Questions, java object-oriented

Source: Internet
Author: User

Java object-oriented basics-Interview Questions, java object-oriented
1. Object-oriented Basics

JAVA basic syntax.

Three features: One encapsulation:★★★★★

Concept: hides the attributes and implementation details of an object and only provides public access.

Benefits:Isolates changes, facilitates usage, improves reusability, and ensures security.

Encapsulation principles:Hide all the content that does not need to be provided externally, hide all attributes, and provide public methods to access them.

 

Singleton design mode:★★★★★(Mandatory)

Solved problems:Ensure the uniqueness of a class in the memory.

For example, when multiple programs read a configuration file, it is recommended that the configuration file be encapsulated into an object. It is convenient to operate on the data, and ensure that multiple programs read the same configuration file object. Therefore, the configuration file object must be unique in the memory.

Runtime ()The method is designed in the singleton design mode.

How can we ensure the uniqueness of objects?

Thoughts:

1. Do not allow other programs to create such objects.

2. Create an object in this class.

3. provide external methods for other programs to obtain this object.

Steps:

1Because the constructor Initialization is required to create objects. As long as the constructor in this class is privatized, other programs cannot create such objects;

2Create an object of this class in the class;

3Define a method and return this object so that other programs can obtain this class object through the method. (Role: Controllable)

Code embodiment:

1, Private constructor;

2To create private and static objects of this class;

3Defines public and static methods and returns this object.

---------------------------------------------

// Hungry Chinese

Class Single {

Private Single (){}//Private constructor.

Private static Single s = new Single ();//Create a private and static class object.

Public static Single getInstance (){//Defines public and static methods and returns this object.

Return s;

}

}

---------------------------------------------

// Lazy: delayed loading mode.

Class Single2 {

Private Single2 (){}

Private static Single2 s = null;

Public static Single2 getInstance (){

If (s = null)

S = new Single2 ();

Return s;

}

}

Inheritance 2★★★★★

Benefits:

1: Improves code reusability.

2: Relationship between classes and classes provides the premise of another feature polymorphism.

Note:

All constructors in the subclass will access the constructor with null parameters in the parent class by default, because the first line of each subclass constructor has the default statement super ();

If there is no constructor with null parameters in the parent class, you must use the super statement in the constructor of the Child class to specify the constructor in the parent class to be accessed.

If the subclass constructor uses this to specify to call the subclass's own constructor, The called constructor will also access the constructor in the parent class.

 

Final features:

1: This keyword is a modifier that can be used to modify classes, methods, and variables.

2: The class modified by final is a final class and cannot be inherited.

3: The final modification method is a final method and cannot be overwritten.

4: The variable modified by final is a constant and can be assigned only once.

Abstract class features:

1: Abstract methods can only be defined in abstract classes. abstract classes and abstract methods must be modified by abstract keywords (classes and methods can be described, but variables cannot be described ).

2: Abstract METHODS only define method declarations, but not method implementations.

3: An abstract class cannot be created (instantiated ).

4: Only inherit the abstract class through subclass and overwriteAllThis subclass can be instantiated only after the abstract method is used. Otherwise, this subclass is still an abstract class.

5:Abstract classes can only be individually inherited.

Abstract class details:

1: Is There Any constructor in the abstract class? Yes. It is used to initialize the subclass object.

2: Can non-abstract methods be defined in abstract classes?

Yes. In fact, abstract classes are not much different from general classes. They are all describing things, but some functions are not specific when abstract classes describe things. Therefore, abstract classes and general classes both need to define attributes and behaviors. However, there is an abstract function more than a general class. In addition, there is less part of the object to be created than the general class.

3: Which of the following abstract keywords cannot coexist? Final, Private, static

4: Can abstract methods not be defined in abstract classes? Yes. Abstract METHODS only aim to prevent this class from creating objects.

Port:★★★★★Differences between abstract classes and interfaces: (there are many questions and conceptual questions)

1: The abstract class can only be inherited and can only be inherited separately.

The interface needs to be implemented and can be implemented in multiple ways.

2: A non-abstract method can be defined in an abstract class, and can be directly inherited and used by subclasses.

There are abstract methods in the interface and must be implemented by sub-classes.

3: The abstract class uses the is a relation.

The like a relationship used by the interface.

4: The member modifier of the abstract class can be customized.

The member modifiers in the interface are fixed. All are public.

Tri-polymorphism (often used in later development)

Multi-state★★★★★(One of the object-oriented features): The function itself has polymorphism, and a specific thing has a different embodiment.

Embodiment:The parent class or interface reference points to its own subclass object. //Animal a = new Cat ();

Benefits of polymorphism:Improves program scalability.

Disadvantages of polymorphism:When the parent class references a subclass object, although it improves the scalability, it can only access the methods in the parent class and cannot access the special methods in the subclass. (Features generated later cannot be used in the early stage, that is, access restrictions)

The premise of polymorphism:

1: It must be related, such as inheritance or implementation.

2: There are usually overwriting operations.

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.