Let's say I have a super-class Ugrad like this:
public class Ugrad{
int DM = 140;
int perYear() { return DM/4; }
}
And I have a sub-class of that class named Grad , like this:
public class Grad extends Ugrad{
int DM = 28;
int perYear() { return DM/2; };
Now , I have a tester class , in which I do some prints to learn how objects work, like this:
//In the line-comments I denote what the result of the print will be.
public class Tester{
public void main(String[] args){
Grad g = new Grad();
System.out.println(g.DM); // 28
System.out.println(g.perYear()); // 14
Ugrad u = (Ugrad) g;
System.out.println(u.DM); //140
System.out.println(u.perYear()); //14
}
}
My question is how does super-class cast works on objects? Why does u.perYear prints 14 equals to g.DM/2 when u.DM equals to 140?