2

So why is that I can cast a parent class as a child but not the other way around?

When I set the object of a parent class to child and vice versa the properties are not copied why?

public class senior {
    private int a = 6;

    public int getA() {
        return a;
    }

    public int x = 1;
}

class junior extends senior {
    public junior() {
        super();
    }

    public int x = 0;
}

public class runner {
    public static void main(String[] args) {
        senior S = new senior();
        junior J = new junior();
        senior S1 = new senior();
        junior J1 = new junior();
        int b = J.getA();
        System.out.println(b);
        S = J; // aliasing ?
        // J 0 S 1
        System.out.println(S.x); // should print 0 but prints 1
        System.out.println(J.x);

        J1 = (junior) S1; // Senior cannot be cast to junior, why?
        System.out.println(S1.x);
        System.out.println(J1.x);// should print 1 but prints 0
    }
}
  • Possible duplicate of [What is variable shadowing used for in a Java class?](http://stackoverflow.com/questions/1092099/what-is-variable-shadowing-used-for-in-a-java-class) – Axel Feb 15 '17 at 12:44
  • It's easy - because Junior IS-A Senior, but it's not true the other way around. Parent classes have no inkling of child classes. – duffymo Feb 15 '17 at 12:44
  • a `Junior` is not a `Senior`, but every `Senior` is a `Junior` + some experience – Andrew Tobilko Feb 15 '17 at 12:44
  • Because Junior has all the properties of Senior but Senior doesnt have all the properties of Junior. If you cast Junior to Senior you will simplify Junior to Senior thus removing all the properties that Junior had. – Luud van Keulen Feb 15 '17 at 12:46
  • Note that if you define a field `x` in both classes the field in the subclass hides that in the superclass, i.e. `junior` (btw, have a look at the Java code conventions) will only see its own `x` (you can access the superclass' `x` via `super.x` but only up one level and it's still bad design). – Thomas Feb 15 '17 at 12:48
  • public class senior{ public int x = 1; } , class junior extends senior{ public int x = 0; } , do you really doubt that variable int x = 1 in senior class was not inherited properly by java in class junior that extends senior? You really had to re-define int x for the fun of java-inheritance-bench testing?? – ShayHaned Feb 15 '17 at 12:50
  • @ShayHaned x is private, so I am not sure if it inherits it. – Varun Narayanan Chakravarthy Feb 15 '17 at 12:53
  • @VarunNarayananChakravarthy, refer to setAccessible(true) method and java.lang.reflect package, you will realize that there is a way to access privacy of variables, methods and classes in java too :). And yes, private variables are not inherited but you have declared them public in the code details – ShayHaned Feb 15 '17 at 13:01
  • @ShayHaned it is actually private private int a = 6; – Varun Narayanan Chakravarthy Feb 15 '17 at 13:03
  • @VarunNarayananChakravarthy **it is actually private**, Please make sure that you make that edit in the original statement of the problem actually, so that there is a little comfort in understanding the true nature of the problem – ShayHaned Feb 15 '17 at 13:05
  • @VarunNarayananChakravarthy Oh great, you were actually talking about int a, privacy, but I was talking about the redefinition of int x in both senior and junior class, and the fact that they are public too – ShayHaned Feb 15 '17 at 13:07
  • @ShayHaned for clarifying things up. :) – Varun Narayanan Chakravarthy Feb 15 '17 at 13:13
  • @VarunNarayananChakravarthy I venerate your problem, but your problem is actually close to a simplified non-inheritance looking problem case like **public class A{ public int x; private int a; public A( int xArg , int aArg ){ int x = xArg; int a = aArg; } }** , and you are trying to understand what makes it compile perfectly – ShayHaned Feb 15 '17 at 13:28

2 Answers2

2

A Child class inherits all the methods and properties of all of its parent class. But the other way is not true since the child class is the one that extends the base class and the base class does not extend the child class. Hope it helped.

Shree Naath
  • 477
  • 2
  • 5
  • 18
1
S = J; //aliasing ?

That is just assigning.

//J 0 S 1
System.out.println (S.x); // should print 0 but prints 1

Variable bound to type. Though the underlying object is j, the type is S (left side)

J1 = (junior)S1; //Senior cannot be cast to junior, why?

Every Truck driver is Driver but you cannot say every Driver is a Truck Driver.

System.out.println (S1.x); 
System.out.println (J1.x);// should print 1 but prints 0

Variable bound to type. object type is j. And variable referring to J. If you want to use the super variable try super.x

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • this is very helpful, but if i had to say J = J1; then the properties will be copied right? – Varun Narayanan Chakravarthy Feb 15 '17 at 12:50
  • @VarunNarayananChakravarthy " if i had to say J = J1; then the properties will be copied right?" - no, nothing is copied. `J = J1` means that `J` now references _the same_ instance that `J1` references and not a copy of that instance. Think of Java references as some kind of pointer. – Thomas Feb 15 '17 at 12:52
  • @VarunNarayananChakravarthy it can be called aliasing in the sense that you have two variables (names) to reference the same instance _after_ the assignment. However it's a very limited form of aliasing, i.e. it only works for object references and only within the class hierarchy. – Thomas Feb 15 '17 at 13:17