design patterns - Java interface and abstract class issue -


I am reading a book -

In chapter 2 (page 25), it is referred to as "the new API For example, you can add a method (with a default implementation) to an abstract class without breaking the old implementation of the class "support the abstract class on the interface, because it is easy to develop. What does this mean (especially, "breaking the old implementation of class")?

In the case of an interface, all The methods that are defined in an interface should be implemented by the class implementing it. / P>

Viewing the interface A

  Interface A {public void foo (); }  

and a square b:

  the implementation of class B is a {}  

this is Provide an implementation for the defined method in the interface:

  class B implies {@Override public void foo () {System.out.println ("foo"); }}  

Otherwise it is a compile-time error now with an abstract class a default implementation of a method :

  Take the abstract class C {public void bar () {System.out.println ("bar"). ; }}  

Where a class inherited from this abstract class can look like this:

  class c {} extends {}  

It can even override the implementation of the default method without any error, if it wants to do this.

What the author was saying: If your API is not yet stable and you have interface (yes, the abstract class is also an interface (in OOP-BOL)), then an abstract class without breaking the sections. Allows to add things that are already present, however, it is only true for non-abstract methods. If you add abstract methods, then they still need to be implemented in every derived class. But still, if you have an API that is still developing and many things are already built on it, then it can make your life easier.


Comments