COMP2396
Loading...
Searching...
No Matches
Animal.java
Go to the documentation of this file.
1package pastpaper._2014_2;
2
3// 2014 Dec 1b
4/*
5* The problem is that toString() in Animal attempt to
6* override a public method, Object.toString(),
7* with a private method. Overriding a method does not
8* allow the overriding method to have a more restrictive
9* access modifier. The method in the subclass must have
10* the same or less restrictive access modifier.
11* The fix is to remove the private modifier from the
12* toString() method in Animal and make it public.
13* The output of the program is:
14* Animal()
15* Animal
16*/
17
18public class Animal {
19 public Animal() {
20 System.out.println("Animal()");
21 }
22 // private String toString() {
23 public String toString() {
24 return "Animal";
25 }
26 public static void main(String[] args) {
27 Animal a = new Animal();
28 System.out.println(a);
29 }
30}
static void main(String[] args)
Definition Animal.java:26