Protected Access Modifier & Example About Inheritance
Example: Consider the following program which uses inheritance to compute the average of 10 floating-point numbers entered by user.
The Class Class1:
The Class Class1:
The Class Class2:
The Class MyClass:
The Output Box:
The output depends on the values entered by user.
Don't worry! Even if there are 3 classes in this program, remember that the executable code is located inside the method main which is located in class MyClass.
So, let us trace (follow) the code written in method main:
Instruction Class2 test=new Class2();
Creates an object test from Class2. But here we have a super class Class1, the default constructor of Class1 is called before the constructor of Class2. (In this example you won't notice any effect of this, but surly this will make a difference if there are print statements for example inside the constructors).
Instruction test.computeAverage(10);
Calls the method computeAverage of object test, passing the number 10 through the method parameter list.
The method computeAverage lets the user enter floating-point numbers, the count of these numbers is determined by the number passed through the parameter list (Which is 10) which is used as a condition for the for loop statement.
All the written below are illustration to what the method computeAverage do:
Each iteration of the for loop, the instruction System.out.println("Please Enter A Floating-Point Number (You Still Need "+(f-i)+" Numbers): "); will print a phrase to the output box, to tell the user that he should enter a floating-point number & there still f-i numbers to enter.
Also, each iteration, the instruction sum=summation (input.nextDouble()); will execute, which lets the user enter a floating-point number, this number is automatically passed as a parameter list for the calling statement of the method summation (method summation is a part of Class1), then the method summation will add the new number to the old sum value (the sum of Class1), the result will be returned to be stored in the sum of Class2.
Instruction test.computeAverage(10);
Calls the method computeAverage of object test, passing the number 10 through the method parameter list.
The method computeAverage lets the user enter floating-point numbers, the count of these numbers is determined by the number passed through the parameter list (Which is 10) which is used as a condition for the for loop statement.
All the written below are illustration to what the method computeAverage do:
Each iteration of the for loop, the instruction System.out.println("Please Enter A Floating-Point Number (You Still Need "+(f-i)+" Numbers): "); will print a phrase to the output box, to tell the user that he should enter a floating-point number & there still f-i numbers to enter.
Also, each iteration, the instruction sum=summation (input.nextDouble()); will execute, which lets the user enter a floating-point number, this number is automatically passed as a parameter list for the calling statement of the method summation (method summation is a part of Class1), then the method summation will add the new number to the old sum value (the sum of Class1), the result will be returned to be stored in the sum of Class2.
The instruction print (); calls the method print of Class1 each iteration, which will print the value of sum of Class1.
After the execution of the for loop, the statement average=sum/f; will compute the average of the 10 numbers entered by user, average will get the result of dividing sum (of Class2) by f (passed by computeAverage which is 10).
The last statement in method computeAverage prints the average value to the output box.
That's all, just remember that every variable has its own life scope which is determined by the parentheses where the variable is declared in (For example, the variable average lives in Class2).
To try the program above, you can download it from here
Note that if Class2 tries to change the value of instance variable x (which is located in Class1) directly, the compiler will detect an error, because x is declared as private.
Note that if Class2 tries to change the value of instance variable x (which is located in Class1) directly, the compiler will detect an error, because x is declared as private.
To overcome the problems which result from inheriting a class contains private data members or methods, we use the protected access modifier instead of private, which is something between public & private.
A protected declared variable is considered as public for the sub-class, but it is private for all other classes. Means that it is accessible by its class & the sub-class only.
Interfaces & Implementation & An example about protected are to be discussed next lesson ...
Interfaces & Implementation & An example about protected are to be discussed next lesson ...
سامر المناصرة
ليست هناك تعليقات:
إرسال تعليق