Skip to main content
AI Assist is now on Stack Overflow. Start a chat to get instant answers from across the network. Sign up to save and share your chats.
added 62 characters in body
Source Link
Jonathan Komar
  • 3.2k
  • 4
  • 39
  • 47

Java interfaces describe types (as defined by the book, Design Patterns). The limiting factor in Java interfaces is that you can't declare an instance variable needed for your list of observers.

That is where abstract class comes in. You can declare an instance variable like this:

import java.util.List; public abstract class Subject { List<Observer> observers; // this would not be allowed in an interface public abstract void attachObserver(ObjectObserver objectObserver) { // implementation }; public abstract void detachObserver(ObjectObserver objectObserver) { // implementation }; public abstract void notifyObservers(); } 

However, interfaces are better, because they are better at forcing encapsulation. You could add another layer of abstraction by declaring the three methods in an interface that in turn declares those methods/"types". Then the abstract class could implement those methods.

Java interfaces describe types (as defined by the book, Design Patterns). The limiting factor in Java interfaces is that you can't declare an instance variable needed for your list of observers.

That is where abstract class comes in. You can declare an instance variable like this:

import java.util.List; public abstract class Subject { List<Observer> observers; // this would not be allowed in an interface public abstract void attachObserver(ObjectObserver objectObserver); public abstract void detachObserver(ObjectObserver objectObserver); public abstract void notifyObservers(); } 

However, interfaces are better, because they are better at forcing encapsulation. You could add another layer of abstraction by declaring the three methods in an interface that in turn declares those methods/"types". Then the abstract class could implement those methods.

Java interfaces describe types (as defined by the book, Design Patterns). The limiting factor in Java interfaces is that you can't declare an instance variable needed for your list of observers.

That is where abstract class comes in. You can declare an instance variable like this:

import java.util.List; public abstract class Subject { List<Observer> observers; // this would not be allowed in an interface public void attachObserver(ObjectObserver objectObserver) { // implementation }; public void detachObserver(ObjectObserver objectObserver) { // implementation }; public abstract void notifyObservers(); } 

However, interfaces are better, because they are better at forcing encapsulation. You could add another layer of abstraction by declaring the three methods in an interface that in turn declares those methods/"types". Then the abstract class could implement those methods.

Source Link
Jonathan Komar
  • 3.2k
  • 4
  • 39
  • 47

Java interfaces describe types (as defined by the book, Design Patterns). The limiting factor in Java interfaces is that you can't declare an instance variable needed for your list of observers.

That is where abstract class comes in. You can declare an instance variable like this:

import java.util.List; public abstract class Subject { List<Observer> observers; // this would not be allowed in an interface public abstract void attachObserver(ObjectObserver objectObserver); public abstract void detachObserver(ObjectObserver objectObserver); public abstract void notifyObservers(); } 

However, interfaces are better, because they are better at forcing encapsulation. You could add another layer of abstraction by declaring the three methods in an interface that in turn declares those methods/"types". Then the abstract class could implement those methods.