0

I have defined the following two classes:

public abstract class Subject {

    private ArrayList<ClockObserver> clockObserverList = new ArrayList<ClockObserver>();


    public void attach(ClockObserver clockObserver) {
        // begin-user-code
        // TODO Auto-generated method stub
        clockObserverList.add(clockObserver);
        // end-user-code
    }
    public void dettach(ClockObserver clockObserver) {
        // begin-user-code
        // TODO Auto-generated method stub
        clockObserverList.remove(clockObserver);
        // end-user-code
    }

    protected void notify() {
        // begin-user-code
        // TODO Auto-generated method stub
        for(int i= 0; i < clockObserverList.size(); i++)
        {
            clockObserverList.get(i).update();
        }
        // end-user-code
    }
}

and

public class SystemClock extends Subject {
    private int hour;
    private int minute;
    private int second;
    public void setTime(int hour, int minute, int second) {
        this.hour = hour; 
        this.minute= minute; 
        this.second = second;
        notify();
    }
    public ClockTime getTime() {
         ClockTime clockTime = new ClockTime();
         clockTime.hour = this.hour;
         clockTime.minute = this.minute;
         clockTime.second = this.second;
        return clockTime;
    }
    public void displayTime() {

        System.out.println("Time is :" + this.hour + ":" + this.minute + ":" + this.second);
    }
}

I got the following error for notify function:

Multiple markers at this line
    - Cannot override the final method from Object
    - overrides java.lang.Object.notify
    - Cannot reduce the visibility of the inherited method from 

Even when I change its visibility from protected to public, I still have the following error:

Multiple markers at this line
    - Cannot override the final method from Object

Could you please help me what is the problem?

Chetan Kinger
  • 15,069
  • 6
  • 45
  • 82
Alex
  • 303
  • 1
  • 6
  • 17
  • Read about [final methods](https://docs.oracle.com/javase/tutorial/java/IandI/final.html) – Reimeus Apr 04 '15 at 15:26
  • possible duplicate of [When should one use final for method parameters and local variables?](http://stackoverflow.com/questions/154314/when-should-one-use-final-for-method-parameters-and-local-variables) – Chetan Kinger Apr 04 '15 at 15:29
  • Side comment: if you are curious **why** you can't "reduce" the visibility of methods, google for "Liskov substitution principle". If you want to to "inheritance" the "right way" ... you can't introduce subclasses with "fewer" properties than the base class. Sounds complicated, but when you think about it, turns out to be very logical. – GhostCat Apr 04 '15 at 15:42

4 Answers4

1

In Java, every class implicitly extends the Object class, which defines a method called notify. Therefore if you create a method notify in your class, the compiler will think that you tried to override the Object.notify method, which is obviously not the case.

Just rename your method notify and you should be alright.

Neumann
  • 551
  • 2
  • 6
  • 17
0
  1. final methods are such methods that cannot be overriden.
  2. You cannot override public method making it protected.
skozlov
  • 384
  • 1
  • 12
0

The final modifier on a method means that the method cannot and must not be overridden. So you cannot override the method notify.

Regarding the visibility, you cannot "hide" methods you override, they would still be visible anyway simply by casting the variable to the super-class.

Imagine this:

class A {
  protected String toString() { return "hidden"; } // Will not compile
}
A a = new A();
Object stillA = a; // a is an instance of A, so it is an instance of Object too
stillA.toString(); // This is still accessible, since Object.toString is public
Gregor Raýman
  • 3,051
  • 13
  • 19
0

No final method in java can be overridden by a subclass. In your case you are trying to override notify method of Object class which is not possible. If you really want to use the method of the class Object, then define a new method with some other name, write your code and then call notify within your new method. For e.g.

public void notifySubject() {
   for(int i= 0; i < clockObserverList.size(); i++) {
        clockObserverList.get(i).update();
   }
   notify();         
}
Vikas Bansal
  • 147
  • 6