Saturday, July 31, 2010

Constructors: Bottom.java

Courtesy of SCJP Sun® Certified Programmer for Java™ 6 Study Guide Exam (310-065) (9780071591065)
Chapter 2, Question 2











Compiler is expecting to see super() on the first line of  

public Bottom(String s)

hence the error:

Bottom.java:5: cannot find symbol
symbol  : constructor Top()
location: class Top
      public Bottom(String s) {System.out.print("D"); }
                              ^
1 error


When you create a class, the compiler will automatically create a no-arg constructor. However, if you explicitly create a constructor with one or more arguments, then the compiler does NOT automatically create a no-arg constructor, you must do that on your own.

To make this code compile, simply add public Top(){} to class Top{}. It is good practice to also add public Bottom(){} to class Bottom{}.


No comments:

Post a Comment