Commented code below are the reasons I think the code is not compiling, is this correct ?
class Building {
}
public class Barn extends Building{
public static void main(String args[]){
Building build1 = new Building();
Barn barn1 = new Barn();
Barn barn2 = (Barn)build1;
/**
* Object is not a Building
* Building is an Object
*/
Object obj1 = Object(build1);
/**
* String is not a Building
*/
String str1 = String(build1);
/**
* Building is not a Barn
* Barn is a Building
*/
Building build2 = Building(barn1);
}
}
Inserted comments are my own. Why the two down-votes, I just didn’t realize the casting syntax was incorrect.
If you are trying to cast, no it is not correct. Casting in Java is done via
If you are trying to construct new objects, you need constructors and and you do it via
Finally, if you want the String representation of an object, you use