The first conclusion is that static properties and static methods in Java can be inherited, but not rewritten (overwrite) but hidden.
Static methods and properties belong to the class, and are called directly through the class name. The method name does not require the inheritance mechanism to call if the subclass defines static methods and properties, then the parent class static method or property is called "hidden", if you want to invoke the parent class static methods and properties, Directly through the parent class name. method name or variable name completion, as to whether the subclass is inherited static methods and properties, but unlike instance methods and properties, there is a "hidden" situation.
Polymorphism can be implemented by relying on inherited interfaces and overrides, overloads (the most critical for inheritance and overrides)。 With inheritance and overrides, you can implement a reference to a parent class that can point to objects of different subclasses. The overriding feature is that the subclass has precedence over the parent class after "overriding", but "hidden" does not have that priority.
Static properties, static methods, and non-static properties can be inherited and hidden without being overridden, so polymorphism cannot be implemented, and references to parent classes can point to objects of different subclasses. Non-static methods can be inherited and rewritten, so polymorphism can be implemented.
# #接口中的实现和类中的继承是两个不同的概念, so it's not possible to say that subclasses that implement an interface inherit constants and methods from the interface
Examples are as follows:
Package com.etc;
public class a//Parent class
{
publicstatic String str = "static property";
publicstring name = "Non-static attribute";
publicstatic void Sing ()
{
System.out.println ("static method");
}
Public Voidrun ()
{
SYSTEM.OUT.PRINTLN ("Non-static Method");
}
}
Package com.etc;
public Class B extends A//Sub-Class B
{
publicstatic String str = "b The rewritten static property";
Publicstring name = "B-rewritten non-static property";
publicstatic void Sing ()
{
System.out.println ("B-Rewrite static method");
}
}
Package com.etc;
public Class C extends A//subclass C inherits all the properties and methods in a
{
}
Package com.etc;
public class test//test class
{
publicstatic void Main (string[] args)
{
c C = NEWC ();
System.out.println (C.name);
System.out.println (C.STR);
C.sing ();//The results of the output are non-static properties, static properties, and static methods in the parent class, with the introduction of static properties and static methods that can be inherited
A C1 = NEWC ();
System.out.println (C1.name);
System.out.println (C1.STR);
C1.sing ();//The result of the output is the non-static property, static property and static method in the parent class, and the introduction of static properties and static methods can be inherited
b b = newb ();
System.out.println (B.name);
System.out.println (B.STR);
B.sing ();//The result is a non-static property of a subclass, a static property and a static method, where the inheritance of non-static and non-static classes is the same
A B1 = Newb ();
System.out.println (B1.STR);//The result is a static property of the parent class, stating that the static property cannot be overridden and does not implement polymorphic
System.out.println (b1.name);//The result is a non-static property of the parent class, stating that the non-static property cannot be overridden and does not implement polymorphic
B1.sing ();//results are static methods of the parent class, which means that static methods cannot be overridden and do not implement polymorphic
}
}
Inheritance of static properties and static methods in Java and the nature of polymorphism