A Brief introduction to Java's overloading overwriting polymorphism static binding dynamic binding

  • 2020-12-22 17:40:23
  • OfStack

This paper mainly studies the contents related to overload, rewrite, polymorphism, static binding and dynamic binding in Java, as follows.

Overloading, overload, refers to the definition of more than one method in a class with the same name, which cannot have the same number, type, and order of arguments. The return type can be the same or different.


public class TstaticOverload {
	static int height;
	TstaticOverload() {
		System.out.println ("Planting a seedling");
		height = 0;
	}
	TstaticOverload(int initialHeight) {
		height = initialHeight;
		System.out.println("Creating new Tree that is "+ height + " feet tall");
	}
	static void info() {
		System.out.println("Tree is " + height + " feet tall");
	}
	static void info(String s) {
		System.out.println(s + ": Tree is " + height + " feet tall");
	}
}

public class testSO {
	public static void main (String [] args) {
		TstaticOverload t = new TstaticOverload(5);
		TstaticOverload.info();
		TstaticOverload.info("overloading method");
		new TstaticOverload();
	}
}

out :  
Creating new Tree that is 5 feet tall 
Tree is 5 feet tall 
overloading method: Tree is 5 feet tall 
Planting a seedling 

Override, called override, refers to a subclass that defines a method that has the same name, return type, or compatible type, and the same parameters as a method in its base class in the case of inheritance. This is a necessary step to achieve polymorphism.

Polymorphism: The ability of the same behavior to have several different forms or forms.


public class StaticSupper {
	public static String staticGet() {
		return "Base staticGet()";
	}
	public String dynamicGet() {
		return "Base dynamicGet()";
	}
}
class StaticSub extends StaticSupper {
	public static String staticGet() {
		return "Sub staticGet()";
	}
	public String dynamicGet() {
		return "Sub dynamicGet()";
	}
}
class StaticMub extends StaticSupper {
	public static String staticGet() {
		return "Mub staticGet()";
	}
	public String dynamicGet() {
		return "Mub dynamicGet()";
	}
}

public class StaticPolymorphism { 
  public static void main (String [] args) { 
    StaticSupper sup1 = new StaticSub(); 
    System.out.println(sup1.staticGet()); 
    System.out.println(sup1.dynamicGet()); 
     
    StaticSupper sup2 = new StaticMub(); 
    System.out.println(sup2.staticGet()); 
    System.out.println(sup2.dynamicGet()); 
  } 
} 

out :  
Base staticGet() 
Sub dynamicGet() 
Base staticGet() 
Mub dynamicGet() 

The concept of program binding:

Binding means that a method call is associated with the class in which the method resides (the body of the method). For java, bindings are static and dynamic. Or early binding and late binding.

Static binding:

Methods are bound before the program executes and are then implemented by a compiler or other linker. For example: C.

Simple bindings for Java that can be understood as program compile-time; In particular, 1 point is made here that the only methods in java are final, static, private and constructors that are pre-bound.

Dynamic binding:

Late binding: Dynamic binding means that the compiler does not know which method to call at compile time until the runtime binds according to the type of the specific object.

If a language implements late binding, it must also provide mechanisms to determine the type of an object at run time and call the appropriate methods separately. That is, the compiler still does not know the type of the object at this point, but the method invocation mechanism can investigate on its own to find the correct method body. Different languages have different implementations for late binding. But the least we can do is think of it this way: they all require some special type of information to be embedded in the object.

Method overloads are static method overloads and normal method overloads. Static method overloading is static binding, and method calls are made by: class name. Methods. Normal method overloading is dynamic binding and method invocation is through: instance object reference. Methods. Constructors can be overridden, but cannot be overridden.

Static methods can be overridden, but do not have a polymorphic effect.

conclusion

Above is the paper on Java overload, rewrite, polymorphism, static binding, dynamic binding all content, hope to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!


Related articles: