I've been struggle to understand something that I'm sure is very simple, yet I'm newbie in java so I'm asking you guys:
public class A
{
public int num;
/**
* Constructor for objects of class A
*/
public A(){
num = 222;
}
public A(int n){
num = n;
}
public int getNum(){
return num;
}
public boolean f(A a){
return num == a.num*2;
}
}
public class B extends A
{
public int num;
public B(int n)
{
num = n;
}
public boolean f(B b){
return num == b.num;
}
}
public class Tester
{
public static void main(String[]args){
A a = new B(14);
System.out.print(a.num);
}
}
The output of this is: 222.
My question is why is 222 and not 14? I did put constructor inside B that gets int, and I put that int(14) in a b constructor. So why do I get the result as if I used empty A contractor? Can anyone please explain me the logic of this?
Thanks!