Crazy java handout Chapter 5 Supplementary Exercises answer to question 8

8. (1) create a class, declare the class initialization block in the class, print the string in it, and then do the same operation in the instance initialization block and constructor to new multiple instances to see how the output is. For example, make four generations of inheritance, and then just create the instance of the class of the smallest generation. Look at the output, including static initialization block, instance initialization block, and constructor;

(2) Reinforcement: create a subclass of this class, operate the same way, and new multiple instances. This subclass has multiple constructors and a parameter constructor. Call the parameterless constructor of this subclass through this().
When I want to call both the parameterized constructor of the parent class through super and the nonparametric constructor of this class through this() to see if it will also call the parent constructor, I find that there is a conflict between super and this. Fortunately, the result is certain. Calling the nonparametric constructor of this class will also call the nonparametric constructor of the parent class. The corresponding parameter constructor is invoked only if super is invoked in the method.

I tried again, calling super in the empty constructor of the subclass, but not calling the two parameter constructor of the parent class, so in fact, super() was written implicitly in the empty constructor. You explicitly wrote super() only covered the implicit super() and did not call the two time, because super() must be the first statement, and could not use the two super().

Through the above experiments, I found that super and this can only be placed in the first statement. In fact, they are the designers of java. In order to achieve the following two effects:
A subclass can only call the constructor of the parent class once
A subclass can only call one constructor of the parent class
But it also brings other effects. A subclass cannot call the overloaded constructor of a subclass and the constructor of a parent class at the same time.

(3) Re enhancement: create a subclass of a subclass, and use super("ha ha") in the constructor to call the parameterized constructor of the parent class. new multiple instances. View the output.

There is only one case where the constructor cannot be extended, that is, the parent class does not provide a default constructor, and the subclass cannot write the default constructor from the. However, if there are only parameters, the creation of other constructors will not report an error

(1)

public class Main
{
	public static void main(String[] args)
	{
		One one1 = new One();
		

		One one2 = new One();
		
	}
}
class One
{
	//Static initialization block
	static 
	{
		System.out.println("One Static initialization block for");
	}
	//Instance initialization block
	{
		System.out.println("One Instance initialization block for");
	}
	//constructor 
	public One()
	{
		System.out.println("One Constructor for");
	}
}

(2)

public class Main
{
	public static void main(String[] args)
	{
		Four four = new Four();
		
	}
}

class One
{
	//Static initialization block
	static 
	{
		System.out.println("One Static initialization block for");
	}
	//Instance initialization block
	{
		System.out.println("One Instance initialization block for");
	}
	//constructor 
	public One()
	{
		System.out.println("One Constructor for");
	}
}
class Two extends One
{
	//Static initialization block
	static 
	{
		System.out.println("Two Static initialization block for");
	}
	//Instance initialization block
	{
		System.out.println("Two Instance initialization block for");
	}
	//constructor 
	public Two()
	{
		System.out.println("Two Constructor for");
	}
}
class Three extends Two
{
	//Static initialization block
	static 
	{
		System.out.println("Three Static initialization block for");
	}
	//Instance initialization block
	{
		System.out.println("Three Instance initialization block for");
	}
	//constructor 
	public Three()
	{
		System.out.println("Three Constructor for");
	}
}
class Four extends Three
{
	//Static initialization block
	static 
	{
		System.out.println("Four Static initialization block for");
	}
	//Instance initialization block
	{
		System.out.println("Four Instance initialization block for");
	}
	//constructor 
	public Four()
	{
		System.out.println("Four Constructor for");
	}
}

(3)

public class Main
{
	public static void main(String[] args)
	{
		Three three = new Three("League of Heroes");
		
	}
}

class One
{
	//Static initialization block
	static 
	{
		System.out.println("One Static initialization block for");
	}
	//Instance initialization block
	{
		System.out.println("One Instance initialization block for");
	}
	//constructor 
	public One()
	{
		System.out.println("One Constructor for");
	}
	//Constructor 2
	public One(String info)
	{
		System.out.println(info);
	}
}
class Two extends One
{
	//Static initialization block
	static 
	{
		System.out.println("Two Static initialization block for");
	}
	//Instance initialization block
	{
		System.out.println("Two Instance initialization block for");
	}
	
	
}
class Three extends Two
{
	//Static initialization block
	static 
	{
		System.out.println("Three Static initialization block for");
	}
	//Instance initialization block
	{
		System.out.println("Three Instance initialization block for");
	}
	//Constructor 1
	public Three()
	{
		System.out.println("Three Constructor for");
	}
	//Constructor 2
	public Three(String info)
	{
		System.out.println(info);
	}
}
class Four extends Three
{
	//Static initialization block
	static 
	{
		System.out.println("Four Static initialization block for");
	}
	//Instance initialization block
	{
		System.out.println("Four Instance initialization block for");
	}
	//constructor 
	public Four()
	{
		System.out.println("Four Constructor for");
	}
}

Keywords: Java Back-end

Added by TheMightySpud on Tue, 07 Dec 2021 03:09:45 +0200