parent type and object as child

One interesting point with inheritance in Java is that, we can use the super type when creating a new object of the sub type.
Suppose Car is the parent class of the Truck class as shown in the following code:
1
2
3
4
5
6
7
8
9
public class Car {
    public void move() {
    }
}
 
public class Truck extends Car {
    public void load() {
    }
}
It’s legal to write:
1
Car vehicle = new Truck();
The rule is: the type on the left side can be super type of the actual object in the right side. Note that the only methods we can invoke are the ones declared in the super type, for example:
1
vehicle.move(); //okay, since Truck inherits Car
Also note that, if the subclass declares a new method, we cannot invoke it using the super type reference:
1
vehicle.load(); // compile error

Open in new window


IF i take parent type and object as child i am getting error.

but when i cast it as below no error

can you please advise why no error

package ParentSuperTypeObjectSubType;

public class TestClass {
	public static void main(String[] args) {
		Car vehicle = new Truck();
		vehicle.move(); //okay, since Truck inherits Car
		vehicle.load(); // compile error
		((Truck) vehicle).load(); // ok
	}

}

Open in new window


below also worked fine if i cast
package ParentSuperTypeObjectSubType;

public class TestClass {
      public static void main(String[] args) {
            Car vehicle = new Truck();
            Truck car = new Car();  // Compile error
            Truck car = (Truck) new Car();  // Compile error
            vehicle.move(); //okay, since Truck inherits Car
            vehicle.load(); // compile error
            ((Truck) vehicle).load(); // ok
      }

}



is above is upcast or downcast
i confuse those similar name?
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

girionisCommented:
The method load() is only available to the child class, therefore you cannot call it from the parent class.

This

 ((Truck) vehicle).load(); // ok

Open in new window


is down-casting (you cast to a subtype).

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.