-3

I'm new in OOP and I'm quite puzzled in casting. Please check the code and declaration below for your reference.

enter image description here

Then I declared this:

Person[] people = new Person[10];
people[0] = new Learner("John");  // what type of casting is this?
thox
  • 129
  • 4
  • 13
  • 8
    Please post code as *text* rather than images. Also, I see no sign of casting in your final line. It's just an implicit conversion from `Learner` to `Person` due to inheritance. – Jon Skeet Mar 11 '18 at 08:44
  • Imagine you have a drawer. This drawer is meant to contain Food. Can you store spaghetti in it? Yes, because spaghetti is food. Same here. You have an array (drawer) which is meant to contain Persons. A Learner is a Person. So the array can contain a Learner. – JB Nizet Mar 11 '18 at 08:53

1 Answers1

-1

This is an upcast. You are saving into an object of Type „Person“ so you can’t Access The Elements of Type „Learner“. This was explained in this Post

difference between upcast and downcast

mosiAI
  • 1
  • 1