0

I am trying to implement a Observer Pattern on Java. It won't compile because notifyAll() seems to be the problem here; Cannot override the final method from Object but I haven't put any 'final' nor 'static' in the code. What am I doing wrong here?

import java.util.ArrayList;
import java.util.List;

interface Observer {
    void update();
}

interface Subject {
    void registerObserver(Observer o);
    void deregisterObserver(Observer o);    
    void notifyAll();    
}

// Concrete Class
class User implements Observer {
    @Override
    public void update() {
        System.out.println("Donald Trump just tweeted, you know");
    }
}

// Concrete Class
class realDonaldTrump implements Subject {
    private boolean hasTweeted = false;
    private ArrayList<Observer> followers = new ArrayList<Observer>();

    public boolean hasTweetedIn24Hours() {
        return hasTweeted;
    }

    public void donaldTweets(boolean hasTweeted) {
        this.hasTweeted = hasTweeted;
        notifyAll();
    }


    @Override
    public void registerObserver(Observer o) {
        followers.add(o);
    }

    @Override
    public void deregisterObserver(Observer o) {
        followers.remove(o);
    }

    @Override
    public void notifyAll() {
        for (Observer o: followers)
            o.update();
    }

}

public class ObserverPatternDemo {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("Hello World");
        System.out.println("Observer Pattern Demo");
        realDonaldTrump tweeterAcc = new realDonaldTrump();

    }

}
younghak
  • 47
  • 1
  • 8

4 Answers4

3

By default all classes extend the Object class which has a final method named notifyAll() . Since the method is final, it cannot be overridden in your class.

 public final native void notifyAll(); // in Object class
TheLostMind
  • 35,966
  • 12
  • 68
  • 104
2

Actually, there are two problems:

  1. You should be doing some research on compiler messages. You see, java compiler errors are really good; most of the time they tell you exactly what is going on. You shouldn't rely on other people explaining them to you; worst case - just search for the error message text. Most of time, that will give you all you need.
  2. You should not assume that method of a certain name does this or that. In other words: You put @Override on notifyAll(); so it should be clear that you are overriding some method. Thing is: you should understand what the methods are supposed to do that you override. And if you don't understand what overriding is; you maybe want to study that concept, too.

Long story short: take this a chance to step back; and for example: read what notify() and notifyAll() should be used for. In order to understand the that the "correct" way would be simply to rename your method notifyAll(), for example to "notifyAllObservers()" and to remove the @Override annotation.

GhostCat
  • 137,827
  • 25
  • 176
  • 248
1

You can't override the notifyAll() method of Object class, since it's final. Rename that method both in the Subject interface and in your realDonaldTrump class.

Eran
  • 387,369
  • 54
  • 702
  • 768
0

There is indirect extends hierarchy resolved to Object the base in java.lang. This method is public void and final , so you cannot override it

https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#notifyAll()

Ramachandran.A.G
  • 4,788
  • 1
  • 12
  • 24