• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Casting

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi!
everyone,

Here is the question:

class Parent{

}

public class Child extends parent{

public static void main(){

Child child = new child();
Parent p = child;
((Child)p).publicChild();



}

public void publicChild(){
System.out.println("public child class");
}
}

Here when we create a instance of class Child. JVM save object (instance of class Child ) into Heap and it's address assign to reference varaible child into stack.
when we create referece variable parent of Parent type and assign child reference (address of Child instance object) to parent referece variable.
parent reference variable actually able to access all the method of child class.but it's not and we need to downcast again ... why ?
can anyone explain why ?

Thanks in advance
Komal
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A Parent reference can only see the methods defined in Parent, not methods that are only defined in Child.
[ March 28, 2007: Message edited by: Keith Lynn ]
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Using Parent class reference variable you can only access the methods of the child class, parent class is known of. In other words the Parent class only have information of the methods it is having so at run time if the Parent class reference variable holds reference of the Child class instance, polymorphism is achieved by calling child class methods using Parent class reference variable.

You do downcast in the case you have to access the Child class specific methods using Parent class reference but you may recall, you should do instanceof test before doing so to make yourself confirmed YEAH the Parent class reference refers to Child class. Here compiler relies on you to downcast, So be faithful, do instanceof test and then do downcast to access the Child class specific method.

Downcast is must because using Parent class reference you are only able to access Parent class methods polymorphically on the Child class object.

Did I miss something!

Thanks and Regards,
cmbhatt
 
My name is Inigo Montoya, you killed my father, prepare to read a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic