Coverage of Java methods and variables during inheritance

Source: Internet
Author: User

I think you have read one or two such Java books. At the beginning, they all point out three main concepts of object-oriented programming: encapsulation, inheritance, and polymorphism. Understanding these three concepts is essential for understanding the Java language, and understanding the coverage of methods is a key part of understanding the concept of inheritance.

This example is taken from the Java language specification.

01: class super
02 :{
03: static string greeting ()
04 :{
05: Return "goodnight ";
06 :}
07:
08: string name ()
09 :{
10: Return "Richard ";
11 :}
12 :}

01: class sub extends super
02 :{
03: static string greeting ()
04 :{
05: Return "hello ";
06 :}
07:
08: string name ()
09 :{
10: Return "dick ";
11 :}
12 :}

01: class test
02 :{
03: public static void main (string [] ARGs)
04 :{
05: Super S = new sub ();
06: system. Out. println (S. Greeting () + "," + S. Name ());
07 :}
08 :}

The result of running the test class is as follows:

Goodnight, Dick

If you get the same output result, you may have a better understanding of method coverage. If your results are inconsistent with the answer, let's work out the cause together, first, let's analyze each class: the super class consists of the Methods greeting and name. The sub class inherits the super class and also contains the greeting and name methods. The test class has only one main method. In row 5th of the test class, we created a sub class instance. Here, you must understand that, although the data type of variable S is super class, it is still an instance of the sub class. If you are confused about this, you can understand it as follows: the variable s is an instance of sub class forced to be converted to super type.
The value returned by S. Greeting () is displayed in the next row (row 6th), followed by the return value of S. Name. The key issue is here. We call the super class method or sub class method. Let's first determine which class's name () method is called, the name () method in both classes is not a static method, but an instance method, because the sub class inherits the super class and has a name () method that is the same as its parent class, so name () in the sub class ()
The method overwrites the name () method in the super class, so the variable s mentioned above is an instance of the sub class, so that the return value of S. Name () is "dick.
So far, we have solved half of the problem. Now we need to determine whether the called greeting () method is of the super class or sub class. Note that the greeting () methods in both classes are static methods, also known as class methods. In fact, the greeting () method of the sub class has the same return type, the same method name, and the same method parameters. However, it does not cover the super class's greeting () method, because the variable S is forcibly converted to super type and the sub class's greeting () method does not cover the super class's greeting () method, so S. the return value of greeting () is goodnight.
Still confused? Remember this rule:"The instance method is overwritten, and the static method is hidden. ".
Now you may ask "what is the difference between hiding and overwriting". You may not understand this. However, we have explained the differences between the two in this super/sub class example. You can use the global name of the class to access the hidden method. Even if the variable s is an instance of the sub class, the greeting () method of the sub class hides the method of the super class with the same name, we can still forcibly convert s to super to access the hidden greeting () method. Unlike the hidden method, for the overwritten method, except for the classes that overwrite them, no other classes can access them. This is why the variable s calls the sub class name () instead of the super class name () method.
Perhaps the best way for you to understand the differences between hiding static methods and overwriting instance methods is to create several classes similar to sub/super, and repeat the rules again, if an instance method is overwritten and a static method is hidden, only the classes that are overwritten can access the method. The way to access the hidden method is to provide the global name of the method. Now you finally understand the answer to the question in the title. When is the "overwritten" method not actually covered? The answer is "never ". In addition, there are several important points to remember:
-- It is illegal to try to hide the instance methods that are the same in the parent class using static subclass methods. The compiler will report an error.
-- It is invalid to try to overwrite the static method that is also identified in the parent class with the subclass instance method. The compiler will report an error.
-- Static and final methods (methods with the keyword final) cannot be overwritten.
-- The instance method can be overwritten.
-- The abstract method must be overwritten in a specific class.

Now let's take a look at the problem of overwrite and hide variables in inheritance. If you think you have understood the overwrite and hide problems in the above method inheritance, and then think that the same is true for variables, so proceed with the following:
Java has six types of variables: class variables, instance variables, method parameters, constructor parameters, exception handling parameters, and local variables. Class variables include static data members defined in the class and static or non-static data members declared in the interface. Instance variables are non-static variables declared in the class body. The term "variable member" refers to class variables and instance variables. The method parameters are used to pass in the method body. The constructor parameters are used to pass in the constructor. Exception Handling parameters are used to pass in catch blocks in a try statement. Finally, the local variable is inCodeBlock or a variable declared in a for statement.

Class base {
Int x = 1;
Static int y = 2;
Int z = 3;
Int method (){
Return X;
}
}

Class subclass extends base {
Int x = 4;
Int y = 5;
Static int z = 6;
Int method (){
Return X;
}
}

Public class test {
Public static void main (string [] ARGs ){
Subclass S = new subclass ();
System. Out. println (S. x + "" + S. Y + "" + S. z );
System. Out. println (S. Method ());

Base B = (subclass) S;
System. Out. println (B. x + "" + B. Y + "" + B. Z );
System. Out. println (B. Method ());
}
}
Run the command to obtain the output:
4 5 6
4
1 2 3
4
From this we can conclude that:
--- Instance variables and class variables can be hidden, and variable members with the same name in the quilt class can be hidden. Local variables and parameters will never be hidden (see the following example ).
Class hidden
{
Public static void main (string ARGs [])
{
Int ARGs = 0; // compile Error
String S = "ABC ";
Int S = 10; // compile Error
}
}
--- How to access hidden variables? You can use the "this" keyword to access the instance variables hidden by local variables in this class. The keyword "super" can access the hidden instance variables in the parent class, class variables can be added with ".. Forced conversion to the parent type.
--- Variables and Methods override and hide different: an instance of a class cannot use a global name or forcibly convert itself to the parent type to access hidden methods in the parent class, however, after the subclass is forcibly converted to the parent type, you can access the hidden variables in the parent class. In addition, the static method cannot overwrite the instance method of the parent class, but the static variable can hide an instance variable with the same name as the parent class. Similarly, the instance method cannot overwrite the static method with the same name as the parent class, however, variables can hide members with the same name as the parent class, regardless of whether the parent class is a class variable or an instance variable.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.