dcsimg
call to parent class method wiht object of child class
3 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Mandar_Pohnerkar
Posted On:   Wednesday, April 24, 2002 05:10 AM

HI, class Parent{ public void show(){ System.out.println("in parent"); } } public class Child extends Parent{ public void show(){ System.out.println("in child"); } public static void main(String args[]){ Parent p; Child c=new Child(); } } Now,with the object of child class i want to access parent class method.I tried casting like c=(Parent)p; This way it com   More>>

HI,


class Parent{



public void show(){



System.out.println("in parent");



}



}



public class Child extends Parent{



public void show(){



System.out.println("in child");



}



public static void main(String args[]){



Parent p;



Child c=new Child();



}



}



Now,with the object of child class i want to access

parent class method.I tried casting like

c=(Parent)p;

This way it compiles but gives error at runtime.

and i dont want to use 'super' keyword.

So, is there any way to access parent class method

with the object of child class.?

This is possible in c++.

Why java doesn't support this ?


Thanking u.




Mandar P

   <<Less

Re: call to parent class method wiht object of child class

Posted By:   Benoit_Quintin  
Posted On:   Wednesday, April 24, 2002 08:32 AM

First, your p object isn't instantiated. And doing the c=p overrides your Child object creation. You cannot explicitely call a parent's method when that method is overridden at the child's level. (At least, there are no way I know of)

Re: call to parent class method wiht object of child class

Posted By:   Christopher_Schultz  
Posted On:   Wednesday, April 24, 2002 08:31 AM

Under what conditions do you want the superclass to override the behavior of a subclass?



This is one of the things about C++ that I never really understood: that you can, in fact, in client code choose which implementation of a method you want to call within an object's implementation heierarchy. That seems totally wrong to me.



-chris

Re: call to parent class method wiht object of child class

Posted By:   mikkel_bruun  
Posted On:   Wednesday, April 24, 2002 05:46 AM

you have to call super()...



is there any reason that you do not want to use super???
About | Sitemap | Contact