1

Working in Java.

I have defined several different abstract classes that each has a static method called aCommonMethod. Each one also has a dependent method that itself calls aCommonMethod, as follows:

public abstract class SomeAbstractClass{
    public static void aCommonMethod() {
    // does something
    }

    public static void someDependentMethod() {
    //some stuff
    aCommonMethod()
    //some more stuff
    }
}

The method someDependentMethod is identical for each of my abstract classes except for what method gets called by invoking aCommonMethod.

What is the idiomatic way, in Java, to implement this? So far I can't find anything that works other than actually copying the aCommonMethod method into each of my abstract classes. Obviously, it would be preferable to write the method just once somewhere and then have my abstract classes inherit it somehow. I cannot find any way to do that in Java.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
AdamHolder
  • 61
  • 5

4 Answers4

1

Fist, you can not override static methods, if you want different classes to have different behaviors probably want the template pattern.

Example:

public abstract class SomeAbstractClass{
    public abstract void aCommonMethod();

    public void someDependentMethod() {
        //some stuff
        aCommonMethod();
        //some more stuff
    }
}

public abstract class SomeClass{
    public void aCommonMethod() {
        // Do your thing
    }
}

public abstract class SomeOtherClass{
    public void aCommonMethod() {
        // Do your thing
    }
}

new SomeClass().someDependentMethod();
new SomeOtherClass().someDependentMethod();
cyroxis
  • 3,661
  • 22
  • 37
0

I am not shure if i got your point, but i´d solve your problem by using these 2 classes:

Mother-Class:

public class mother 
{
    public void sortTest()
    {
        //some stuff
        child.sort();
        //some stuff
    }
}

Child-Class:

public abstract class child extends mother
{
    public static void sort()
    {
        //some stuff
    }
}

You should describe your problem more precisely!

Tobias Marschall
  • 2,355
  • 3
  • 22
  • 40
  • How in the world could I be more precise? I stated the problem in exact mathematical precision and abstracted away all irrelevant details. Even so, that may actually be the answer I need, so thanks. – AdamHolder Apr 14 '16 at 19:55
  • Why an abstract class and not an interface? – Debosmit Ray Apr 14 '16 at 19:57
  • 1
    @AdamHolder - Do you really think you specified the problem with such exact precision and with the exact amount of detail necessary but routined developers still don't understand what you want? It is clear from your question you don't understand basic Java but you still refuse to listen to reasonable queries. – Sason Ohanian Apr 14 '16 at 20:10
  • What's clear is that this forum is unwelcoming to people who are not already experts and are trying to learn. I may not be a java expert, but I know what "mathematical precision" looks like because that is my job. I'm going to try to find someone helpful elsewhere. – AdamHolder Apr 14 '16 at 20:12
0

This is what abstract methods are for.

abstract class SomeAbstractClass {
    public abstract void aCommonMethod();

    public void someDependentMethod() {
        // some stuff
        aCommonMethod();
       // some more stuff
    }
}

class Impl1 extends SomeAbstractClass {
    @Override public void aCommonMethod() {
       System.out.println("Impl1 version of aCommonMethod");
    }
}

Each subclass can implement the abstract method to do its own thing, and the common superclass method will call the concrete subclass' implementation.

Inheritance doesn't work with static methods, make these instance methods instead. The idiomatic answer is to avoid using static for methods that are part of a class hierarchy.

What you have in your example, judging from your defining aCommonMethod as returning void, seems stateful and side-effect-infested, but if what you have is a collection of static methods that are stateless, then it seems likely you may not need a class hierarchy here at all; consider using the Strategy pattern, passing in objects that specify how some operation should be done.

See this question for passing in a strategy using lambdas in Java 8.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • Thanks for a legit answer. However, is it contrary to the WAY of Java to collect all my QuickSort methods into an abstract QuickSort class? That's what I did here. I view this as very similar to the Arrays class of Java.util. It's just a collection of useful functions. And there's no reason why anyone would ever instantiate a QuckSort object as I wrote it. – AdamHolder Apr 15 '16 at 04:37
  • @AdamHolder: things like java.util.Arrays are not made to be inherited. If you have stateless functions then static methods are ok, but if you expect to override behavior then you need instance methods. – Nathan Hughes Apr 15 '16 at 12:28
-1

As of Java 8, now one can pass functions as parameters into other functions using lambda expressions. However programmers circumvented that problem before Java 8, their over-complicated machinations are obsolete.

For example, using the new lambda expressions, one can write

interface PointlessInterfaceObject {
    void onlyFunction();
}

Then someDependentMethod can be defined like this:

someDependentMethod(PointlessInterfaceObject myPointlessInterfaceObject){
// some code
myPointlessInterfaceObject.onlyFunction();
// more code
}

Now, if you first instantiate the anonymous function like this,

PointlessInterfaceObject myAnonymousFunction = ()->SomeAbstractClass.aCommonMethod();

then you can pass it as a parameter into the method as follows:

someDependentMethod(myAnonymousFunction)

This allows you to write a function just like someDependentMethod just once and then pass the other methods into it.

AdamHolder
  • 61
  • 5