Is there a way not to call the subclass method when invoking?

Taylor Verikor :

I wanted to create my own database program in Java. I know that there are already solutions out there like sylite etc., but I want to do it myself. As it turned out to be a more complex program than first expected, I used a lot of inheritance to keep the overview. For example:

  • The first class is the RawStreamManager to handle the Stream to a File using FileOutputStream
  • The second class is the SessionManager in order to prevent multiple access using some kind of csma/cd strategy
  • There are a some more classes to do other things.

My problem now, however, is that the SessionManager would use the read() method of a subclass, instead of its own, when I call e.g. the method read(). I guess there is a good reason for that, but in my case it makes no sense to figure out weather the client has the authority and to split the String into a database, when the client does not have the Token (the allowance to read/write without multiple access). So I´m looking for a way to call the read() method of SessionManager (there are a few more cases for which I would Need that) instead of the subclasses read() method

I already tried to manually cast the SessionManager to a SessionManager object before calling read(), but that didn't help. A kind of Workaround could be to give those methods a different Name.

To Show you guys some code without dropping my hole Database-Code here I simplified the Problem:

public class First {
    public First() {
        System.out.println("syso from first:");
        ((First) this).print();
    }

    public void print() {
        System.out.println("FIRST");
    }
}
public class Second extends First{
    public static void main(String[] args) {
        new Second();
    }

    public Second() {
        System.out.println("syso from second:");
        print();
    }

    @Override
    public void print() {
        super.print();
        System.out.println("SECOND");
    }
}

now I would expect the Output to be:

syso from first:
FIRST
syso from second:
FIRST
SECOND

but it is (3rd line):

syso from first:
FIRST
SECOND
syso from second:
FIRST
SECOND

So, has anyone of u a solution for that problem please? Thank you in advance.

Fullstack Guy :

Since you are creating an instance of the Second class, when the constructor of Second is invoked a call to the super class constructor First() is invoked implicitly sue to the implicit invocation of super() in the Second() call.

Although you have type casted this to First it will still invoke the overriden method of the Second class due to the nature of dynamic/runtime polymorphism in Java. At runtime, JVM checks the instance and calls its method not of the reference.

To avoid and give the expected output you need to make the print method static or use a different method name altogether:

class First {
    public First() {
        System.out.println("syso from first:");
        First.print();
    }

    public static void print() {
        System.out.println("FIRST");
    }
}
 class Second extends First{
    public static void main(String[] args) {
        new Second();
    }

    public Second() {
        System.out.println("syso from second:");
        Second.print();
    }


    public static void print() {
        First.print();
        System.out.println("SECOND");
    }
}

Output:

syso from first:
FIRST
syso from second:
FIRST
SECOND

The static version gives the expected output as the method in the case of static belongs to the class rather than the instance. In the class Second I have hidden and not overriden the print() of the base class First.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324481&siteId=1