jiva -- Inheritance of Classes

1. Defining classes

public class A{
     //1. Attributes / Static Attributes
     //2. Method/Static Method
     //3. Construction Method
     //4. Internal classes
     
     //menber-level
     public static void main(String[] args){
     //method-level
     }
}

//top-level

1) Static static modifier: can only modify member level
2) Access modifiers
i.public/default/package-private modifies top-level and member-level
ii.private/protected can only modify the membership level
3)......
Execution Code - Method Level

2. Life Cycle of Reference and Object
1) Reference: A data type that refers to an object.
2) The life cycle of an object:
i.new Distribution Space Period
ii. Initialization period
iii. active period
iiii. Garbage Period
3) Class loading - run time
i. The process of loading *. class files (bytecode files of classes) into memory from disk
Class Loader is used
ii. Class (main) loading area: method area of memory (mainly method statement data, static attributes, meta-information)
iii. Class loading mechanism: lazy loading (delayed loading), loading only when needed
Use of iiii. classes: * new objects; ** access static properties / methods; *** reflection, etc.
iiiii. Initialization order of static attributes: executed in writing order (initialization at definition, static code block)

3. Compilation and runtime
1) Compilation period: converting source file (. java) to bytecode file (. class)
It's not our source code that runs during compilation, it's a javac program. Our source code is just an input to someone else's program.
2) Running time: Running our code, java will start the JVM virtual machine

4. Inheritance
Purpose: Code reuse (inheritance is a relational class)
Syntax: class parent name {} class subclass name extends parent name {}
Note: Attributes of the parent class, except private ly modified attributes, can not be accessed directly by subclasses.

5. Overwriting of Methods
Method overload Overload: Method name is the same + parameter list is different
Method signature: method name + parameter list
Method Override: The subclass overrides the method of the parent class. The method name is the same, the parameter list is the same, and the return value type is the same (allowing the return value type to be a subclass of the parent class's return value type).
Summary: It must have happened on subclasses
1) Must implement the same method as the parent method name + parameter list + return value type
2) Purpose: For the same behavior, it shows different ways from the parent class.
3) Access qualifiers for subclasses cannot be more closed than for parent classes
4) It is generally recommended that the @override annotation be used for interpretation.

6.super keyword
1) super constructs for accessing parent classes
2) Call parent method explicitly instead of representing parent object
3) super is not a reference

class A{
     public int a=10;
     public void print(){sout("Paternal print");}
}
class B extends A{
     public int a=100;
     public void print(){sout("Subclass print");}
     public void test(){super.a; super.print();}//Clearly invoking methods of parent class a and parent class
}

7. Class inheritance in Java allows only single inheritance

8. Examples of inheritance

Parents:

public class List {
    protected int size = 0;

    public void insert(int index, int element) {
        System.out.println("Not realized");
    }

    public void pushFront(int element) {
        insert(0, element);
    }
    public void pushBack(int element) {
        insert(size, element);
    }
}

Subclass:

import java.util.Arrays;
//Sequence table
public class ArrayList extends List {
    private int[] array = new int[10];

    @Override
    public void insert(int index, int element) {
        for (int i = size; i > index; i--) {
            array[i] = array[i - 1];
        }

        array[index] = element;
        size++;

        int[] a = Arrays.copyOfRange(array, 0, size); // [0,size)
        String s = Arrays.toString(a);
        System.out.println(s);
    }

    public static void main(String[] args) {
        ArrayList list = new ArrayList();
        list.pushBack(1);
        list.pushBack(2);
        list.pushBack(3);
        list.pushFront(10);
        list.pushFront(20);
        list.pushFront(30);
        list.insert(3, 100);
    }
}

Operation results:

9. Relationships between child and parent classes
1) Class Loading Relation: The loading of subclasses must trigger the loading of parent classes, and the loading of parent classes must occur before the loading of subclasses.
2) Object instantiation/construction relationship: The object construction process of a subclass triggers the initialization of attributes in the parent class, which must occur before the initialization of attributes in the subclass.
Initialization of attributes: i. Initialization at definition + construction code in writing order. ii constructor (independent of super calling parent constructor location)

class A {
    static {
        System.out.println("This is A Static block of code");
    }
}

class B extends A {
    static {
        System.out.println("This is B Static block of code");
    }
}

public class ClassLoadOrder {
    public static void main(String[] args) { 
        System.out.println("start");
        new B(); 
        System.out.println("End");
    }
}

Operation results:

class A {
    static {
        System.out.println("This is A Static block of code");
    }
}

class B extends A {
    static {
        System.out.println("This is B Static block of code");
    }
}

public class ClassLoadOrder extends B { //Inherited from B
    public static void main(String[] args) { //static mian calls static methods to trigger class loading - - priority loading parent class
        System.out.println("start");
        new B(); //Class loading is not triggered because the class has been loaded into memory
        System.out.println("End");

    }
}

Operation results:

class AA {
    int a = initA();
    private int initA() {
        System.out.println("AA Initialization a");
        return 0;
    }
    {
        System.out.println("AA Construct blocks of code");
    }
    AA(int x) {
        System.out.println("AA Construction method");
    }
}

class BB extends AA {
    int b = initB();
    private int initB() {
        System.out.println("BB Initialization b");
        return 0;
    }
    {
        System.out.println("BB Construct blocks of code");
    }
    BB(int x) {
        super(x);
        System.out.println("BB A PARAMETRIC CONSTRUCTION METHOD");
    }
    BB() {
        this(100);
        System.out.println("BB Nonparametric Construction Method");
    }
}

public class InstanceOrder {
    public static void main(String[] args) {
        new BB();
    }
}

Operation results:

Keywords: Java jvm

Added by sirTemplar on Mon, 09 Sep 2019 11:04:14 +0300