Java polymorphism and the influence of parent class constructor on subclass constructor

Detailed Java polymorphism

Polymorphic instance

Polymorphism is the ability of the same behavior to have multiple different manifestations or forms.

Polymorphism is the same interface, using different instances to perform different operations.

Ⅰ. Upper transition objects:

Upward transformation refers to the action of converting a parent class reference to a subclass reference

public class FOREVER_GWC_FIRST 
{
	
	public void FOREVER_GWC()
    {
		System.out.println("FOREVER_GWC_FIRST");      
	}

}
public class FOREVER_GWC_SECOND extends FOREVER_GWC_FIRST
{
	
	public void FOREVER_GWC()
    {
		System.out.println("FOREVER_GWC_SECOND");      
	}
	
}
public class FOREVER_GWC_THIRD 
{
	
	public static void main(String[] args) 
	{
		
		FOREVER_GWC_SECOND second = new FOREVER_GWC_SECOND();
		
		second.FOREVER_GWC();
		
		FOREVER_GWC_FIRST first = second;
		
		first.FOREVER_GWC();
	}
	
}

Output result:

FOREVER_GWC_SECOND
FOREVER_GWC_SECOND

Ⅱ. Under transformation objects

Downward transformation is to convert the object referenced by the parent class to the type of the subclass. The downward transformation must be carried out on the basis of the upward transformation.

public class FOREVER_GWC_THIRD 
{
	
	public static void main(String[] args) 
	{
		
		FOREVER_GWC_FIRST first = new FOREVER_GWC_SECOND();//向上转型

		FOREVER_GWC_SECOND second =(FOREVER_GWC_SECOND)first;//向下转型
	}
	
}

Advantages of polymorphism

  1. Eliminate coupling between types
  2. Replaceability
  3. Scalability
  4. Interface
  5. flexibility
  6. Simplification

The necessary conditions for the existence of polymorphism

  • inherit
  • Rewrite
  • Parent class reference points to child class object

Detail the influence of the parent class construction method on the subclass construction method

The structure of the parameterless construction method is as follows:

	public FOREVER_GWC()
	{
//		super();
	}	

Ⅰ. When the parent class has no parameterless construction method:

  • Subclasses cannot have parameterless constructors.
  • The parent class constructor must be explicitly called in the form of super (parameter) in the subclass constructor.

Ⅱ. When the parent class has no parameter construction method:

  • Subclasses can have no-argument constructors.

  • Subclasses can also have parameterized construction methods.

public class FOREVER_GWC_FIRST 
{
	
	public FOREVER_GWC_FIRST(String name)
    {   
	}

}
public class FOREVER_GWC_SECOND extends FOREVER_GWC_FIRST
{

	public FOREVER_GWC_SECOND()
	{
		this("GWC");
		System.out.println("子类构造方法");
	}
	
	public FOREVER_GWC_SECOND(String name)
    {
		super(name);     
	}
	
}
public class FOREVER_GWC_THIRD 
{
	
	public static void main(String[] args) 
	{
		new FOREVER_GWC_SECOND();
	}

}

to sum up:

  • When the parent class does not have a parameterless constructor, the child class cannot have a parameterless constructor, you must explicitly call the superclass constructor with super.
  • When the parent class has no parameterless constructor and there are multiple parameterized constructors, the child class can only explicitly call one constructor of the parent class.
  • Subclasses can also construct multiple construction methods, as long as each construction method explicitly calls the parent class construction method, there is no requirement for which construction method to call the parent class.
  • When the parent class has a parameterless construction method, the subclass can have a parameterless construction method or a parameterized construction method.
  • You can use super to explicitly call the parent class constructor, or you can call it without super.
Published 19 original articles · praised 0 · visits 1616

Guess you like

Origin blog.csdn.net/FOREVER_GWC/article/details/104789701