Inheritance (to be added)

inherit



Preface

Recently, I am going to start learning advanced Java knowledge. Before that, I want to consolidate my previous knowledge. The learning plan is roughly to review the basic knowledge -> IO, thread -> design pattern -> HashMap, HashTable -> Spring framework -> JDK source code
Of course, I also hope to look at the JDK source code in the indirect way of learning. The following are personal understandings and only records

1. Inheritance

Inheritance is one of the three major language features of Java, that is, the relationship between people, students, male and female students, ranging from large to small

Two, some code tests

1. Subclass and parent class

The code is as follows (example):

/*Person.java*/
public class Person {
    
    
    protected String name;
    public void sayHello() {
    
    
        System.out.println("Father:"+"---"+"name:"+name);
    }
}
/*Student.java*/
public class Student extends Person{
    
    
    public void sayHello() {
    
    
        System.out.println("Son"+"---"+"name:"+name);
    }
}

2. Test of conversion between parent class and subclass and calling method

The code is as follows (example):

public static void main(String[] args) {
    
    
        Student s =  new Student();
        Person p = (Person)s;
        p.sayHello();
        Student s1 = (Student) new Person();
        s1.sayHello();
        //Object x = new Integer(-1);
        //System.out.println(x.toString());
}

The result is also

Son---name:null
Exception in thread "main" java.lang.ClassCastException: com.sa.extendsexample.Person cannot be cast to com.sa.extendsexample.Student
	at com.sa.extendsexample.ExtendsMain.main(ExtendsMain.java:9)

This shows that the parent class cannot be converted to a subclass, and the execution method is executed according to the type of the actual referenced object. The same test is performed, the method of the subclass is removed, and the variable value of the subclass is changed. The test result is also The method of the type of the referenced object is always called first


3. The order in which the parent class and the subclass call methods

The results are as follows (the code is omitted, only the results are posted):

main
Father-static
Son-Static
Father-CodeBlock
Father-Constructor
Son-CodeBlock
Son-Constructor

4.java.lang.ClassCastException

This exception is a type conversion exception, view the source code

/**
 * Thrown to indicate that the code has attempted to cast an object
 * to a subclass of which it is not an instance. For example, the
 * following code generates a <code>ClassCastException</code>:
 * <blockquote><pre>
 *     Object x = new Integer(0);
 *     System.out.println((String)x);
 * </pre></blockquote>
 *
 * @author  unascribed
 * @since   JDK1.0
 */

There is an example exception, that is, an error will be reported when Integer is converted to String. You may think that it should be possible to convert, but these two objects are not the same class, and there is no inheritance relationship. Of course, two unrelated classes cannot When converting between Integer and String, it should be

Object x = new Integer(0);
System.out.println(String.valueOf(x));

At the same time, String.valueOf is like this

public static String valueOf(Object obj) {
    
    
    return (obj == null) ? "null" : obj.toString();
}

Use the valueOf method to judge whether the object is null. Using this method to convert to String may reduce unexpected exceptions

At last

This is some personal records, if there is still learning, it will be updated later

Guess you like

Origin blog.csdn.net/weixin_55937426/article/details/114604927