Back to questions
JuniorTheoryCommon
What happens when this program is compiled and run?
public class Main {
public static void main(String[] args) {
Shape s = new Shape();
System.out.print(s.sides());
System.out.print(s.area());
}
}
abstract class Shape {
int sides() { return 4; }
abstract int area();
}It does not compile
- Expecting
4to be printed first:newon an abstract class is a compile error, so the program never starts and nothing is printed. - Expecting
0fromarea(): an abstract method has no body to run and no default result, and its class can have no objects of its own.
- How would you change the program so that it prints the area of a square?
- Can an abstract class with no abstract methods be created with
new?