0

Casting in Apex seems like Black Magic to me. I don't get when should we make an explicit cast, and when it can be implicit. Like:

Recipe.apxc

public virtual class Recipe{
    public string nome;
    protected string instructions;
    private String source = 'Granny';
    
    public Recipe() {}

    public Recipe(String inpNome, String inpInstrucoes) {
        nome = inpNome;
        instructions = inpInstrucoes;
    }
    
    public void printDescription(){        
        system.debug('Name: ' + nome + ', Instructions: ' + instructions);
        return;
    }
}

DrinkRecipe.apxc

public class DrinkRecipe extends Recipe{
    public String nome = 'Luso';
    
    private String glassType;
    
    public DrinkRecipe(String inpNome, String inpInstrucoes){
        
        super(inpNome, inpInstrucoes);
    }
}

in the annonymous window

DrinkRecipe dr = new DrinkRecipe('Whater', 'glu, glu', 'normal');

// why does this work? Shouldn't i always need to cast an object to make it use another constructor, from another class?  
Recipe r1 = dr;
system.debug(r1.nome);

// I thought explicit casting like this would be the only way  
Recipe r2 = (Recipe) dr;
system.debug(r2.nome);

Thanks

  • 1
    `DrinkRecipe` is a subclass of `Recipe`, so it is compatible with `Recipe`. The phenomenon you're describing is called "[Runtime Polymorphism](https://www.google.com/search?q=runtime+polymorphism)," and is a feature of many object-oriented programming languages. It works because `DrinkRecipe` is the *actual* type of the object. – Robert Harvey Dec 15 '20 at 01:27
  • 1
    Further reading: https://th3silverlining.com/2014/09/08/a-beginners-guide-to-object-oriented-programming-with-apex-3-polymorphism – Robert Harvey Dec 15 '20 at 01:31
  • thanks, it's clar now. I also found this: https://www.youtube.com/watch?v=8WqWHBTWv2Y – Luis Aguiar Dec 15 '20 at 13:16

1 Answers1

0

In general, Apex requires you to explicitly convert one data type to another. For example, a variable of the Integer data type cannot be implicitly converted to a String. You must use the string.format method. However, a few data types can be implicitly converted, without using a method. Numbers form a hierarchy of types. Variables of lower numeric types can always be assigned to higher types without explicit conversion. The following is the hierarchy for numbers, from lowest to highest: Integer Long Double Decimal Note Once a value has been passed from a number of a lower type to a number of a higher type, the value is converted to the higher type of number.

In addition to numbers, other data types can be implicitly converted. The following rules apply: IDs can always be assigned to Strings. Strings can be assigned to IDs. However, at runtime, the value is checked to ensure that it is a legitimate ID. If it is not, a runtime exception is thrown. The instanceOf keyword can always be used to test whether a string is an ID.

  • Please [edit] your answer to include [example] of your solution code and improve code formatting. See [answer] – Gander Dec 16 '20 at 08:23
  • @Gander There is no "solution code" because the question doesn't ask for any. – kaya3 Dec 16 '20 at 16:12