Tony
Tony

Reputation: 1165

How to prohibit a subclass from having a method?

In my Java project, I have the method addType1AndType2() which has windows where you expand lists and select objects from the list. It was very complicated and time consuming to create, as things must be scrolled and xpaths keep changing. There are two lists in this which are actual names but, due to company proprietary info, I will just call them Tyep1 and Type2.

Now I have an UpdateType1 class which uses all the complicated methodology in the AddType1AndType2 but has nothing related to Type2 in it. I could copy the AddType1AndType2 and cut everything I do not need, but that would be replicating and changes would have to be duplicated in both classes. This defeats the purpose of inheritance and reusability.

I can make a class UpdateType1 extends AddType1AndType2{} which I have done. But there are still methods like selectType2Value() which are inherited but not possible in the subclass.

If I do an @Override and declare the class as private in the sub class, I get an error that I cannot reduce the visibility in a subclass.

Any idea what I can do? Right now I am just putting a throw new AssertError("Do not use") but that seems kind of lame. Is there a better thing to do that would even give a compile-time error rather than an assert at run time, or is this the best way?

Upvotes: 1

Views: 69

Answers (3)

GhostCat
GhostCat

Reputation: 140633

The thing is: your model is wrong.

Inheritance is more than just putting "A extends B" in your source code. A extends B means: A "is a" B.

Whenever you use a B object, you should be able to put an A object instead (called Liskov substitution principle).

Long story short: if B has methods that A should not have ... then you should not have A extends B.

So the real answer is: you should step back and carefully decide which methods you really want to share. You put those on your base class. Anything else has to go. You might probably define additional interfaces, and more base classes, like

class EnhancedBase extends Base implements AdditionalStuff {

Edit: given your comment; the best way would be:

  1. Create interfaces that denote the various groups of methods that should go together
  2. Instead of extending that base class, use composition: create a new class A that uses some B object in order to implement one/more of those new interfaces.

And remember this as an good example why LSP really makes sense ;-)

Upvotes: 3

bruno
bruno

Reputation: 2273

Create the interfaces

public interface IAddType1 {... /* methods signtatures to add Type1 */}
public interface IAddType2 {... /* methods signtatures to add Type2 */}
public interface IUpdateType1 {...  /* methods signtatures to update Type1 */}

then your current code at AddType1AndType2 will become just a base helper class:

 public abstract class BaseOperationsType1AndType2{  
     //code originally at AddType1AndType2: methods that add Type1 and Type2
 } 

then your new AddType1AndType2 class will be:

public class AddType1AndType2 
     extends BaseOperationsType1AndType2, 
     implements IAddType1 , IAddType2 {
        //nothing special. 
 } 

and your new UpdateType1can be defined as

 public class UpdateType1 
      extends BaseOperationsType1AndType2 
      implements IUpdateType1 {
       //
 }

Voila.

Upvotes: 1

uylmz
uylmz

Reputation: 1562

You can use 'final' keyword to prohibit extending a method in a subclass.

A method with a 'final' modifier cannot be overriden in a subclass.

Upvotes: 0

Related Questions