Java Basics - Inheritance, Encapsulation, Polymorphism

1. What is inheritance and what are the characteristics of inheritance?

The subclass inherits the characteristics and behaviors of the parent class, so that the subclass has various properties and methods of the parent class. Or a subclass inherits methods from the parent class, so that the subclass has the same behavior as the parent class.

Features: In the inheritance relationship, the parent class is more general, and the child class is more specific. The parent class has more general characteristics and behaviors, while the child class has some special characteristics and behaviors of its own in addition to the characteristics and behaviors of the parent class.

in an inheritance relationship. The parent class and the child class need to satisfy the is-a relationship. A subclass is a parent class.

Terms for superclass and subclass: superclass and subclass, superclass and subclass, base class and derived class, they mean the same thing.

2. Why do we need inheritance? When should it be inherited?

Using inheritance can effectively achieve code reuse and avoid the appearance of duplicate code.

When two classes have the same characteristics (attributes) and behaviors (methods), the same parts can be extracted and put into one class as a parent class, and the other two classes inherit this parent class.

Inheritance implements the object-oriented principle: write once, only once (write once, and write once

3. How to implement inheritance?

In the Java language, the extends keyword is used to indicate that a class inherits from another class.

Only some common properties and methods are defined in the parent class.

Subclasses automatically inherit the properties and methods of the parent class, and subclasses can define specific properties and methods. Or the subclass can redefine the properties of the parent class and override the methods of the parent class to obtain different functions from the parent class.

4. What is method overriding?

If a method defined in a subclass whose name, return type, and parameter list exactly match the name, return type, and parameter list of a method in the superclass, then it can be said that the subclass's method overrides the superclass Methods.

Overriding methods in different classes is a necessary condition for implementing polymorphism.

5. The usage and location of the super keyword, the super keyword calls the constructor of the parent class, and the super keyword calls the method of the parent class?

In the constructor of the subclass, use the super keyword to call the constructor of the superclass.

If the subclass overrides the superclass's method, you can call the superclass's method through the super keyword.

father:

private String name;
private String sex;
public xinxin1(String name,String sex){
this.name=name; this.sex=sex; }
public void hello(){     System.out.println("Hi! I'm " +name+" I'm "+sex+ " child"); } Subclass: public xinxin2(String name,String sex){ // Call the constructor of the parent class super (name,sex); } public void hello(){ System.out.println("I'm new here!"); // Call the method of the parent class super .hello(); }

 

Location Note: The statement (super statement) that calls the constructor of the parent class must be the first statement in the constructor.

Because when creating an object, you need to create the parent class object first, and then create the child class object.

Note: When creating an object, create the parent class object first, and then create the child class object. If the constructor of the parent class is not explicitly called, the no-argument constructor of the parent class will be called automatically.

6, the boss of all classes (ancestor) Object.

All classes directly or indirectly inherit the java.lang.Object class. The Object class defines the same behavior that all java objects have and is the ancestor of all classes.

If a class does not use the extends keyword, then the class directly inherits from the Object class.

7. What is polymorphism?

Polymorphism is characterized by exhibiting multiple forms and having multiple implementations. Or polymorphism is a trait that has the ability to exhibit multiple forms. Or the same implementation interface, using different instances to perform different operations.

8. Why do we need to use polymorphism? The benefits of polymorphism?

It can enhance the extensibility and maintainability of the program and make the code more concise.

Not only can reduce the workload of coding, but also can greatly improve the maintainability and scalability of the program.

9. How to implement polymorphism?

The general approach is: write a method that only receives the parent class as a parameter, and write code that only deals with the parent class. When calling this method, instantiate a different subclass object (new an object).

More specifically:

(1) The subclass overrides the method of the superclass. Make subclasses have different method implementations.

(2) The parent class type is used as the parameter type, and the parent class and its subclass objects are transferred as parameters.

(3) At runtime, which method to use is dynamically determined according to the type of object actually created.

At runtime, the java virtual machine decides which method to use based on the type of object actually created. This is generally referred to as dynamic binding.   

10. Polymorphism Summary:

Polymorphism is closely related to inheritance and method overriding. We receive the parent class type as a parameter in the method, and call various methods of the parent class type in the method implementation. When a subclass is passed as a parameter to this method, the Java virtual machine will call the corresponding method in the subclass (when there is a method override) according to the type of object actually created.

11. Package

Encapsulation can be thought of as a protective barrier that prevents the code and data of that class from being randomly accessed by code defined by the outer class. That is to set the methods and properties in a class, generally set the properties to private to restrict access, and set the properties to be invisible to the outside world.

  inside the class Inside the package Subclass other
public
protected ×
default(空) × ×
private × × ×

 

Guess you like

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