0
interface I
class A implements I
class B extends A
class C extends A
class D extends A
class E extends A

What can I do so that only class D doesnot get the methods of I. I am currently thinking to override all the methods of 'I' in D with no body.

Am I thinking in the right direction or is there a better way?

Edit: D only doesnot want the methods contained in interface I. Rest of A's methods are inherited. A is the base class. interface I is the interface for ordering videos B,C,E are users with different privileges and D is the admin. The admins can not order videos so donot need the methods carried by the interface.

  • 2
    That's exactly the opposite of what inheritance means. http://en.wikipedia.org/wiki/Liskov_substitution_principle You should rethink your design. – SLaks Feb 02 '14 at 14:06
  • 1
    This isn't clear. Are you saying you want `D` to inherit from `A`, but not have any of `A`'s methods? That doesn't really make any sense. – Oliver Charlesworth Feb 02 '14 at 14:06
  • 1
    Why does D inherit from A if it _is not_ an A? – Edwin Dalorzo Feb 02 '14 at 14:07
  • 1
    What are you trying to accomplish with this design? – Kenster Feb 02 '14 at 14:07
  • 1
    @user3262900 You should add a comment instead of editing your post. Your design is still broken if `A` implements `I` and `D` extends `A` but doesn't implement `I`. See the LSP which was linked earlier. – dtech Feb 02 '14 at 14:11

5 Answers5

4

You probably want to rethink your design. If D extends A then D should do everything A can, like implementing the interface I. This is the basis of the very important Liskov Substitution Principle in OOP programming.

You can rework your design to still share the relevant code while retaining logical inheritance:

    A
   /|
  D A' implements I
   /|\
  B C E

It might also be possible that (as @OliCharlesworth pointed out) a solution using composition might be more useful. It depends on the specific case, but if B,C,D and E use A rather than that they are an A composition is more appropriate.

dtech
  • 13,741
  • 11
  • 48
  • 73
  • 1
    +1 for the nice diagram. However, it's probably worth pointing out the whole [*prefer composition over inheritance*](http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance) thing. – Oliver Charlesworth Feb 02 '14 at 14:17
0

If A implements I, then any user of A or any of A's subclasses should be able to expect that the methods of I are implemented. It is part of A's "contract" with the rest of the classes in the program.

For D to inherit from A, it should be a "special case" of A, and all of the functionality defined by A should be implemented. Inheritance is necessarily an "additive" tool; you get all of what is in the base class, and can alter or add things in the subclass.

You say you want to subtract some things -- that is contrary to object-oriented programming, and contrary to the way inheritance (and implements) is implemented in the language.

So I think the best answer is "Yes, there's a better way." We can't tell what it is with only this much information

arcy
  • 12,845
  • 12
  • 58
  • 103
0

I would accomplish this as follows:

interface I
class superClassA
class A extends superClassA implements I
class B extends superClassA implements I
class C extends superClassA implements I
class D extends superClassA
class E extends superClassA implements I

which is the same as:

interface I
class superClassA
class A extends superClassA implements I
class B extends A
class C extends A
class D extends superClassA
class E extends A
stuhlo
  • 1,479
  • 9
  • 17
0

You are probably looking to use something like the Adapter pattern., which can be used to hide or merge interfaces.

lreeder
  • 12,047
  • 2
  • 56
  • 65
0

Working with java 8, you can just make the method in interface I a default method and override it in classes you want it implemented in but not overridden in class D, that should solve your issue. And by the way the default method should not have anything in it's body for it to work properly

Fego
  • 145
  • 1
  • 7