1

While studying abstract classes and interfaces I get the statement " You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes. "

I don't know what is meant by unrelated classes here.

  • 3
    Unrelated: having nothing [else] in common. (This usually implies that they are not related to each other by inheritance; and the only base type they have in common is Object.) – user2864740 Aug 19 '15 at 05:34

3 Answers3

2

Let's look at some examples from the standard Java API:

The interface Comparable is implemented by many classes, like File and Integer, which are unrelated to each other. There's a whole list of classes which implement Comparable in the link above. That's fine, because Comparable defines only a single method (compareTo) which every class implements on its own way, without some shared code which could be beneficial to every Comparable class.

On the other hand, two classes extending a single abstract class like Format are usually closely related, like DateFormat and NumberFormat. The abstract class allows for the definition of some convenience/utility methods which are useful for every subclass.

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
1

Both abstract classes and interfaces serve to share some functionality.

Classes can be related by inheritance, for example every Button is also a Control and as such has Control's methods, properties etc (everything that a Control has plus its own members).

Interfaces are a way to share some functionality between classes not related by inheritance, for example both String and DateTime implement IComparable, so both have CompareTo method and you can say both are instances of IComparable.

w.b
  • 11,026
  • 5
  • 30
  • 49
0

Like user2864740 already commented

Unrelated: having nothing [else] in common

For example a Lamborghini is unrelated to a Pitbull, but a Lamborghini is related to car.
Because a Lamborghini is obvios a car. So it has all features each car has. Now the Pitbull has no features a car has ergo he is unrelated to car.

WiiMaxx
  • 5,322
  • 8
  • 51
  • 89