Some time ago to listen to the teacher's lectures, the teacher told me that the subclass can inherit all the properties and methods of the parent class. I was very puzzled at the time, because I remember it very well at the school test: Subclasses can only inherit non-private properties and methods of the parent class. The teacher gave me the explanation is this-first look at the following code
/** * Define parent class * @author CBS*/ Public classFather {PrivateString name;//Private Properties Private Charsex; Public voidTell () {System. out. println (name+sex); } Private voidspeak () {System. out. println (name+"is speaking!"); } PublicString GetName () {returnname; } Public voidsetName (String name) { This. Name =name; } Public CharGetsex () {returnsex; } Public voidSetsex (Charsex) { This. Sex =sex; }}
/** * defines subclasses, and does not write anything in subclasses. * @author CBS*/ Public classChild extends Father {}
/** * Define TEST class * @author CBS*/ Public classTest { Public Static voidMain (string[] args) {child C=NewChild (); C.tell ();//the Tell method is available.//c.speak ();//error, private method of parent class is not visible }}
We can see that the Tell method is still available in the test class, even if the property of the child class object is not assigned a value. If the child class does not inherit the private property of the parent class, then the Tell method should have an error. The result is that the subclass inherits everything from the parent class, and the modifier only affects the property or whether the method is visible externally.
But the individual agrees with the Java Official document that the subclass cannot inherit the private properties of the parent class, but if the public method in the subclass affects the private property of the parent class, then the private property is able to be used by the quilt class.
The role of modifiers in Java is to encapsulate the properties and methods in the class so that they are not visible to the outside, and the official document interpretation is a good consideration of Java inheritance and encapsulation. Inheritance is to improve the reusability of code, and encapsulation protects the invisible part of the code. In the process of writing code, we obviously want to improve the reusability of the code, and do not want some fields in the class to be arbitrarily modified. Then the inheritance mechanism is reasonable. In addition, in the case of life, the interpretation of the official Java documentation also fits. For example, we can not completely inherit all parents (such as personality, etc.), but some of the parents can not inherit something that will still profoundly affect us.
Of course, these reasons are not sufficient, but the author's personal opinion. On the question of inheritance, it is also discussed, the following post summarizes the more comprehensive, the evidence is also very sufficient, we can go to see:
Link: https://www.zhihu.com/question/51345942/answer/145388196 Robin
Can the Java subclass inherit the private properties of the parent class?