I want to Call the Parent Method in Java With A child Reference
Truck tv=new Vehicle();
Truck is the child class and Vehicle is a parent Class should i have to use type casting?
I want to Call the Parent Method in Java With A child Reference
Truck tv=new Vehicle();
Truck is the child class and Vehicle is a parent Class should i have to use type casting?
You cannot have a reference variable of base class pointing to the object instantiated of the derived class, better have a reference of base class pointing to its own class i.e,
Truck tv = new Truck();
Thanks, Balaji.
What you are doing will not compile since Truck is a sublass and you are instantiating a superclass.
You need to call Truck's constructor like this:
Truck tv=new Truck();
Using that, all methods that are present in Vehicle can be called on the Truck reference