1

Why is the following code returning an illegal assignment error in anonymous window?

Line: 3, Column: 9 illegal assignment from truck to vehicle

Super Class

public virtual class vehicle {

    public void model(){
         system.debug ('Model method of vehicle class ');
    }
    public void speed(){
        system.debug('Speed of the vehicle depends of the model');
    }
}

Child Class

public class truck extends vehicle 
{
    public override void speed()
    {
        system.debug('Truck has speed of 20 mile/hr');
    }
}

Execute Anonymous Script

vehicle v=new vehicle();
v.speed();
vehicle v2=new truck();
v2.speed();
v2.model();
  • 1
    Did your child class(Truck) compile successfully? – javanoob Jan 16 '17 at 22:19
  • Perhaps you have a naming conflict... – Adrian Larson Jan 16 '17 at 22:19
2

I think you may have to mark your methods as virtual too in your base class.

**

In addition, the extending class can override the existing virtual methods by using the override keyword in the method definition.

**

public virtual void model(){
     system.debug ('Model method of vehicle class ');
}
public virtual void speed(){
    system.debug('Speed of the vehicle depends of the model');
}

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_extending.htm

| improve this answer | |
  • I marked all the method (model & Speed) virtual but the error is still there. The code looks good. I even tried the sample codes in the SFDC, and the error still comes up. This appears to be some compiler error... this is weird – chris_og Jan 16 '17 at 23:03
  • 1
    are you updating the class through an IDE/ directly in the org? The only reason why I see you may still have error is because the classes may not be updated if done through eclipse or some other tool – Rao Jan 16 '17 at 23:34
  • I'm using the developer console on SFDC – chris_og Jan 17 '17 at 2:06
  • Can you post the final classes you have in your original question – Rao Jan 17 '17 at 3:53
  • looks like the issue is with the developer console. The same code runs fine in Force.com IDE – chris_og Jan 17 '17 at 8:14
2

The code you have posted is valid. You should be able to assign a child class to a super variable. Likely causes for this code to fail:

  • Naming conflict
  • Unnoticed compile fail

You can run the following script in Execute Anonymous to demonstrate the basic concept:

class Vehicle { }
class Truck extends Vehicle { }
Vehicle instance = new Truck();
| improve this answer | |
1

Did your truck class even compile?

I just copied your code and I had to make the speed() method virtual, in order to override it in the truck class. Once I did this it worked as you would expect.

//vehicle class
public virtual class vehicle {

    public void model(){
        system.debug ('Model method of vehicle class ');
    }
    public virtual void speed(){
        system.debug('Speed of the vehicle depends of the model');
    }
}

//truck class
public class truck extends vehicle { 
    public override void speed() { 
        system.debug('Truck has speed of 20 mile/hr'); 
    } 
}

//execute anonymous
vehicle v=new vehicle(); 
v.speed(); 

//truck v2 = new truck(); or vehicle v2 = new truck(); both work. 
// I find the truck v2 =... cleaner
truck v2=new truck(); 
v2.speed(); 
v2.model();
system.debug(v2);
| improve this answer | |
  • Both the vehicle and truck class compiled without error... i made all the method virtual but the error still there. Your code did not assign the truck instance to the vehicle object: vehicle v2 = new truck() this assignment should work but for some reason isn't working. This might have something to do with the compiler – chris_og Jan 16 '17 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.