Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.18/5 (3 votes)
See more:
Hi,

I made a class that has a virtual method that is to overrided in a derived class, but sometimes I want that method is to call statically by other methods inside the same class.

An example:
Java
class A extends BaseClass {
    public String init() {
        doSomethingWithVirtualMethodResult(virtualMethod());
        super.init();
    }
    protected String virtualMethod() {
        return something;
    }
}
class B extends A {
    public String init() {
        doSomethingWithVirtualMethodResult(virtualMethod());
        super.init();
    }
    protected String virtualMethod() {
        return somethingElse;
    }
}
class Main {
    public Main() {
        BaseClass obj = new B();
        obj.init();
        String virtualResult = obj.virtualMethod();
    }
}


I need that the method doSomethingWithVirtualMethodResult inside the init have to be called with both something and somethingElse, but the content of virtualResult is to be somethingElse.

How to do it?

Thank you
Posted
Comments
Afzaal Ahmad Zeeshan 16-Nov-15 12:34pm    
2 things. First of all the program won't compile.

1. Stupid clause removed.

2. The code seems to be written by an instructor, in a very "pseudo" manner that he forgot to even define the functions. Did you write that function, or the code itself?
Menci Lucio 16-Nov-15 12:48pm    
Yes, of course, the program won't compile, but not for first your reason. I called the class Main, and the public Main() is its constructor. The entry point should be called main and not Main.
The program won't compile because I forgot the return inside the inits methods.
I already had that functions and they are the n-th element of a very long chain of hierarchy, and they have a lot of imports and they are so long. I wrote only a summary.
Afzaal Ahmad Zeeshan 16-Nov-15 12:55pm    
Hm, apologies for that.

Ok the problem is that the static functions (or static scope) can only access the static fields or functions. Instance fields or instance functions, need an instance of the object. It is simple as that! To access an instance function, you need to be having an instance of that class, otherwise, a static field directly cannot access those functions.

http://stackoverflow.com/questions/2042813/calling-non-static-method-in-static-method-in-java

The virtual methods do not make any difference, they are either instance of static.

It all makes no sense at all. Note that the method doSomethingWithVirtualMethodResult, despite its name, has nothing to do with VirtualMethod or its result. It simply can be called with any string.

"Calling statically" sounds like absurd, but in fact, it's simply unclear what would you mean by that. No static members are involved, so what you want has nothing to do with the only well-defined concept related to static in Java technology: static members vs instance members. All your members are instance members, that is, the explicit "this" parameter is passed to instance methods, to reference the particular instance of the class. Essentially, there is no such thing as a call without such instance.

So, you need a solution, right? The solution is: learn how OOP works and pose your problems correctly. Your problem is not solvable or unsolvable, it simply makes no sense as it is put. For now, I'll explain how things work in your case. Your VirtualMethod is virtual and overridden. There is no such thing as "called with both something and something else", because it is not called with any parameter. There is no anything "to call it with". You only have two different return objects. But there is a parameter called explicitly, "this". Say, you create two instances, one of one type and another of another type (important runtime type, the implementation of the method is dynamically chosen based on the reference "this". This is a heart of OOP, its central mechanism. All calls are, by definition, are based on the reference "this", in the case of a virtual method, on its runtime type. Please see: https://en.wikipedia.org/wiki/Dynamic_dispatch[^].

You may ask: what to do? Nothing. Just learn how things work. Your other problem is the question you ask. You are not asking about anything material, something which should really happen in behavior. Then we could help you immediately. But you are asking in terms of some non-existing concept which only exist in your imagination, such as "calling statically", which is, essentially, is not something which happens in observable world. Perhaps you need to learn how to formulate and understand your ultimate goals.

—SA
 
Share this answer
 
Hi,

I think we misunderstood. When I speak about static methods, I didn't understand about the java meaning of "static", then I didn't mean talk about the instance/static members, but just like the opposite meaning of virtual, or dynamic call.

Reading my real code (vs the example I exposed) I found the solution creating private (then final) methods:
Java
class A extends BaseClass {
    public String init() {
        doSomethingWithVirtualMethodResult(staticMethod());
        super.init();
    }
    private String staticMethod() {
        return something;
    }
    protected String virtualMethod() {
        return staticMethod();
    }
}
class B extends A {
    public String init() {
        doSomethingWithVirtualMethodResult(staticMethod());
        super.init();
    }
    private String staticMethod() {
        return somethingElse;
    }
    protected String virtualMethod() {
        return staticMethod();
    }
}
 
Share this answer
 
Comments
Richard MacCutchan 16-Nov-15 13:44pm    
The names virtualMethod and staticMethod have no meaning here. You should go back to your Java documentation to clarify what you are trying to do.
Sergey Alexandrovich Kryukov 16-Nov-15 15:24pm    
This is not a "solution". Better remove this post, to avoid down-voted and abuse reports. Also, I did not receive any notifications. To address to anyone, you can comment on a member's post, such as an answer.

Now, "static" is not "Java meaning" of the word, this is general object-oriented meaning. When I answered, I took into account that you did not mean that commonly used notion. I explain that what you mean does not make sense. You still did not get the basics. Richard is perfectly right (see his comments above): your methods name have no meaning here. Amazingly, method called "*virtual*" is not virtual and the one called "*static*" is not static. You are really lost.

But it does not even matter. You need to grasp the fundamental ideas of OOP and programming in general. Your main problem is lack of understanding that using a result of call to some method has nothing to do with any specific method. You just pass some string object as a parameter, that's all. This is not even about OOP, but about basic understanding of what method and method parameters do.

Conclusion: your "problem" is purely artificial, or imaginary, and your "solution" is purely fake. Your thinking goes along some wrong, imaginary lines; and the worst thing is that you fail to realize it.

—SA
Menci Lucio 17-Nov-15 3:32am    
Hey, I haven't the question of answers I make, elsewhere I wan't make question. It's no a good policy reply "Best remove this post to avoid down-voted" or "You need to grasp the foundamental" or "is a lack of understanding" or "have no meaning here, go to documentation" or something else. I ask for an help in a forum, not wanting to recite a ritual sabbatical. I made only an example, and not put 23k classes to see all of the project.
In .net area of this forum people try to help who needs an help. Evidently in java help people is prohibited. Sorry! Next time someone ask me if is better java or .Net, I reply that .Net forums are serious. Before now I always said that they are two similar and different worlds without needs of preferences. You make me thinking seriously about to leave from codeproject. Are you that deserves a downvote, but I respect everybody and won't do it. Sorry, I'm not like you.
Hey boy, respect all! I touhgt that it is the main of this forum. If you was polity, your reply could be simlpy this:

No, you can't.

End
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 17-Nov-15 10:08am    
This post is another big abuse.
—SA
Menci Lucio 17-Nov-15 10:22am    
I'll say it for you, stop with polemics, you're getting ridiculous

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900