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();
2
  • 1
    Did your child class(Truck) compile successfully?
    – javanoob
    Jan 16, 2017 at 22:19
  • Perhaps you have a naming conflict...
    – Adrian Larson
    Jan 16, 2017 at 22:19

3 Answers 3

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

6
  • 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
    – user40878
    Jan 16, 2017 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, 2017 at 23:34
  • I'm using the developer console on SFDC
    – user40878
    Jan 17, 2017 at 2:06
  • Can you post the final classes you have in your original question
    – Rao
    Jan 17, 2017 at 3:53
  • looks like the issue is with the developer console. The same code runs fine in Force.com IDE
    – user40878
    Jan 17, 2017 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();
1
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);
1
  • 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
    – user40878
    Jan 16, 2017 at 23:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .