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