From the course: Oracle Java Certification: 3. Methods and Inheritance

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Type casting on object references

Type casting on object references - Java Tutorial

From the course: Oracle Java Certification: 3. Methods and Inheritance

Start my 1-month free trial

Type casting on object references

- [Instructor] In Java programming, we create objects. Objects are stored in memory and accessed via their references. Object references are often stored as values in variables. Such variables have their own types that may not match the type of the referred objects. This is due to the is-a relationship established through inheritance. Because an object of a child class is also an object of the parent class, we can always use a reference of the parent class type to refer to an object of the child class. Let's look at an Example. This code defines two classes. The Main class extends the Example class and overrides the print method. The Main method defines two object references of different types. Variable m is assigned a reference to an object of the Main class at line 13. Then this object reference is copied between the two variables and is used to invoke the print method in the end. What would this program output?…

Contents