1

I have read this-Downcasting is not possible but not got why actually this is not possible, below is the code snippet

class Parent {

}

public class Inherit  extends Parent{
    public static void main(String[] args) {
        Parent parent = new Inherit()  //No Error
        Inherit sub = new Parent();  // Type mismatch error
    }
}

My question is why creating a parent object of type sub class is possible(Parent a = new Inherit()) but why reverse is not possible? what is the actual reason(Except type mismatch)

Prasanna Kumar H A
  • 3,341
  • 6
  • 24
  • 52

4 Answers4

2

An easy example:

class Parent {
  public String name;
}

class Inherit extends Parent {
  public int age;
}

So each Inherit has a name but a Parent has no age. What is if you call the following:

Inherit sub = new Parent();
sub.age; // ERROR: because a parent has no age

So it is not possible to assign a parent object to a sub class reference because something could be missing like @OH GOD SPIDERS pointed out.

wake-0
  • 3,918
  • 5
  • 28
  • 45
1

Disclaimer: Super (over)simplistic intuitive explanation, without getting into proper technical detail.

I will start by pointing out that downcasting may be possible in special cases; specifically, on a "parent" object that was itself created by upcasting a "child" object. Hopefully the reason for this will be clear in a sec.

Say you have a Parent object with field x.
And a Child object which extends this with another field y.

When you instantiate a Parent object normally, java allocates enough memory to hold x.
When you instantiate a Child object, java allocates enough memory to hold both x and y.

When you upcast a Child object onto a Parent one, the memory layout doesn't change; java simply ignores the memory bit that was reserved for y, and treates all calls to this object as if it only contained x.

But if you were to attempt to downcast a Parent object into a Child one, you would essentially be missing the bit that refers to y in the object's memory footprint. Since the intent is one of 'casting' rather than "copying the object somewhere else", casting in this case is not allowed.

In the case the the Parent object was originally obtained by upcasting a Child object, it is then possible to downcast it back into a Child object; java treats this as a special case, since the memory layout would allow it.

This is also the reason that downcasting is treated as a runtime bug, rather than a compilation one.


PS. Note that the corollary from this is that when one upcasts at instantiation, while this is treated as a Parent object in the code, an entire Child object is stored in memory -- which is why a successful downcast back to a Child object can reoccur at any time.

Tasos Papastylianou
  • 21,371
  • 2
  • 28
  • 57
1

Think about the inherit relationship. An object of the child class is and object of the parent class, it has all its properties and methods, while the inverse is definitely not true.

For example:

class Parent{
    public void methodA(){ ... };
}

class Child extends Parent{
    public void methodB(){ ... };
}

public class Main{
    public static void main(String[] args) {
        Child p = new Parent();
        p.methodA(); //Ok, parent object has method A
        p.methodB(); // parent object does not have method B, 
                     //but Child class exposes it. what happens here?
    }
}
bracco23
  • 2,181
  • 10
  • 28
0

In OPPS concept Inheritance follows IS-A relationship between parent and child class which means if child class (Jeep) extends the parent class(vehicle) then you can say Jeep IS-A vehicle but vice-versa is not possible. Suppose Jeep class have wheels property

class Vehicle {
  public Date madeYear;
  public void start(){}
}

class Jeep extends Vehicle {
  public int wheels;
}

Jeep jeep = new Vehicle(); //Its not valid because vehicle object don't have wheels property which you can try to access from jeep reference variable.

ankit.vishen
  • 1,100
  • 15
  • 29