Note that there are some explanatory texts on larger screens.

plurals
  1. POJava extends strange output
    text
    copied!<p>I have written a small program in Java, a there is something I just do not understand.</p> <p>Program that I have wrote has 3 classes kingClass, masterClass, and workClass. workClass extends masterClass. </p> <p>Program: in the main class (kingClass) I have declared masterClass and workClass, and with the masterClass I have given values to variables x, and y. In the end of kingClass I have called a addNum function that sum two numbers from the masterClass. </p> <p>Now the problem: I expected when I run the program that it will give me a sum of two numbers I have given with input, not the sum of number that I have given value in constructor. </p> <p>How can write this program so that addNum returns the value of the sum of number I have enterd.</p> <p>Sorry for bad english, Thank you..</p> <p>kingClass</p> <pre><code>public class kingClass { public static void main(String[] args) { masterClass mClass=new masterClass(); mClass.setX(10); System.out.println(mClass.getX()); mClass.setY(5); System.out.println(mClass.getY()); workClass wClass = new workClass(); System.out.println(wClass.addNum()); } } </code></pre> <p>masterClass</p> <pre><code>public class masterClass { private int x; private int y; masterClass() { x=0; y=0; } public void setX(int a) {x=a;} public void setY(int a) {y=a;} public int getX() {return x;} public int getY() {return y;} } </code></pre> <p>workClass</p> <pre><code>public class workClass extends masterClass { int num=getX()+getY(); public int addNum() {return num;} } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload