One of the Java Knowledge Points and Frequently Asked Questions Summary: Object-Oriented Programming and Frequently Asked Questions

Object Oriented Programming (OOP)

Java is a concurrent, class-based and object-oriented computer programming language. Its advantages are:

    1) Modular code development, easier to maintain and modify;

    2) High code reusability;

    3) Enhance the reliability of the code and =-[Flexibility;

    4) Object-oriented programming has many important features, such as: encapsulation, inheritance, polymorphism and abstraction.

package

    Encapsulation provides objects with the ability to hide internal features and behavior. Objects provide methods that can be accessed by other objects to change data within it. In Java, there are 4 modifiers: public, private, protected and default. Each modifier grants different access rights to other objects in the same package or under different packages. For a detailed introduction to modifiers, please refer to: Java Modifier Details

The benefits of encapsulation:

    1) Protect the internal state of the object by hiding the properties of the object;

    2) Improve the usability and maintainability of the code, because the behavior of objects can be changed and extended individually;

    3) Prohibit bad interactions between objects to improve the modularity of the code;

polymorphism

    Polymorphism is the ability of a programming language to present the same interface to different underlying data types. Operations on a polymorphic type can be applied to values ​​of other types. The so-called polymorphism means that the specific type pointed to by the reference variable defined in the program and the method call issued through the reference variable are not determined during programming, but are determined during the running of the program, that is, which reference variable will point to. The instance object of the class, the method in which the method call issued by the reference variable is the method implemented in the class must be determined during the running of the program. Because the specific class is determined when the program is running, the reference variable can be bound to various class implementations without modifying the source code, so that the specific method called by the reference changes accordingly, that is, the reference variable is not modified. The program code can change the specific code bound when the program runs, so that the program can choose multiple running states, which is polymorphism.

    Three necessary conditions for polymorphism: inheritance, overriding, and upcasting.

inherit

    Inheritance provides objects with the ability to obtain fields and methods from base classes. Inheritance provides reusable lines of code, and it is also possible to add new features to existing classes without modifying the class.

abstract

   Abstraction is the step of separating ideas from concrete instances, so classes are created based on their functionality rather than implementation details. Java supports the creation of abstract classes that expose only interfaces but not method implementations. The main purpose of this abstraction technique is to separate the behavior of the class from the implementation details.

The difference between abstraction and encapsulation

    Abstraction and encapsulation are complementary concepts. On the one hand, abstraction is concerned with the behavior of objects. Encapsulation, on the other hand, focuses on the details of an object's behavior. Generally, encapsulation is achieved by hiding the internal state information of the object. Therefore, encapsulation can be regarded as a strategy used to provide abstraction.


Common java problems

1. What is the Java Virtual Machine? Why is Java called a "Platform Independent Programming Language"?

    The Java virtual machine is a virtual machine process that can execute Java bytecode. Java source files are compiled into bytecode files that can be executed by the Java Virtual Machine. Java was designed to allow applications to run on any platform without requiring programmers to individually rewrite or recompile for each platform.

    The Java Virtual Machine makes this possible because it knows the instruction length and other characteristics of the underlying hardware platform.

2.What is the difference between JDK and JRE?

    The Java Runtime Environment (JRE) is the Java virtual machine that will execute Java programs. It also contains the browser plug-ins needed to execute the applet. The Java Development Kit (JDK) is a complete Java software development kit that includes the JRE, compiler and other tools (eg JavaDoc, Java debugger) that allow developers to develop, compile, and execute Java applications.

3. What does the static keyword mean? Is it possible to override a private or static method in Java?

    The static keyword indicates that a member variable or member method can be accessed without the instance variable of the owning class. Static methods in Java cannot be overridden because method overriding is dynamically bound at runtime, while static methods are statically bound at compile time. A static method is not associated with any instance of the class, so it is not conceptually applicable.

4. Is it possible to access non-static variables in static environment?

     A static variable belongs to a class in Java, and its value is the same in all instances. When a class is loaded by the Java virtual machine, static variables are initialized. If your code tries to access non-static variables without an instance, the compiler will complain, because these variables have not yet been created and are not associated with any instance.

5. What are the data types supported by Java? What is automatic unboxing?

    Java supports 8 basic data types: byte, short, int, long, float, double, char, boolean. Autoboxing is a conversion done by the Java compiler between primitive data types and the corresponding object wrapper types. For example: convert int to Integer, double convert to double, and so on. The opposite is automatic unboxing.

6. What is the difference between an interface and an abstract class?

    Java provides and supports the creation of abstract classes and interfaces. Their implementations have something in common, the differences are:

    1) All methods in an interface are implicitly abstract. An abstract class can contain both abstract and non-abstract methods.
    2) A class can implement many interfaces, but can only inherit one abstract class.
   3) If a class wants to implement an interface, it must implement all the methods declared by the interface. However, the class may not implement all the methods declared by the abstract class, of course, in this case, the class must also be declared abstract.
    4) An abstract class can implement an interface without providing the interface method implementation.
    5) The variables declared in the Java interface are final by default. Abstract classes can contain non-final variables.
    6) Member functions in Java interfaces are public by default. Member functions of abstract classes can be private, protected or public.

    7) The interface is absolutely abstract and cannot be instantiated. An abstract class also cannot be instantiated, but can be called if it contains a main method.

7. What is pass-by-value and pass-by-object?

    For details, please refer to: java value passing and object passing


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325823756&siteId=291194637