Non virtual methods in Java
Asked Answered
S

4

10

Just starting to use Java. I find a lot of similarities with .NET, but I see that all methods in Java are virtual by default. So the question is what can I do to make them non virtual ? Is the final keyword the one and right solution ?

Substituent answered 1/9, 2010 at 18:34 Comment(8)
download.oracle.com/javase/tutorial/java/IandI/final.htmlFrowzy
@Garis Can't get your point... How it is connected to question ?Substituent
It would be more correct to say that .net has a lot of similarities with Java, because Java was there first. Microsoft tried to play their embrace-and-extend game with Java but failed, so they introduced their own language with the same basic ideas as Java.Dirham
@Dirham in Java 5 and 6 you can see features taken from C#. But anyway this is not the point. Personally I don't care which is the first which is the best and such type of things... For me the best is one with which I will make the job done... so rephrase it as you wish.Substituent
Why don't you edit the question to take out the reference to .NET? Just remove the "I find a lot of similarities with .NET, but " part. The question is still the same, but without the part that would cause religious wars. I would do this myself if I had the rep.Stumpy
It depends on why you are doing this. If you are doing this for efficiency, you don't need to, the JVM can work this out.Jocelin
I don't say .net is all bad, at least it woke up the Java guys. Much of the good stuff in Java 5 and 6 are the result of that.Dirham
Its more like I have similarities with my father, but he learned a thing or two from me too later on :P.Swaziland
A
9

Yes, or private

Aviate answered 1/9, 2010 at 18:35 Comment(4)
But private - just as in .net - makes the method unavailable for other classes to call, as well as preventing overriding.Octodecimo
But what can I do when I need to have a method in child class with the same name as it is in parent class. In C# there is a keyword new for such cases... is there something similar in Java.Substituent
This is not true - final in Java prevents overriding, it does not make overriding non-virtual.Haemorrhage
@JanMares alright then: let's just say that it prevents overriding.Aviate
S
9

If you´re aiming to make the method non-virtual for performance, let the JIT deal with that until you have evidence that it isn't doing.

If the reason to make the method non-virtual is to be able to define it in a subclass but not involve polymorphism, you're probably subclassing for no real reason (post more code if you'd like to contest this).

If it's for design, I'd suggest making the class final instead of individual methods if possible. IDEA has good inspections for class design. Try not to listen too closely to those who want you to leave everything open so that they can subclass to hack around bugs or limitations; they'll shout at you even more loudly when you accidentally break their subclass.

Provide a way for clients to add their own types rather than subclass yours and they'll probably not even notice that your classes are final.

Summarize answered 1/9, 2010 at 19:51 Comment(0)
B
2

Instead of defining all methods as final, you can also define the entire class as final. I'm not saying whether this good or bad style.

Bentonbentonite answered 1/9, 2010 at 19:38 Comment(0)
S
1

Make it static.

If you call a non-virtual method then you want to know from your code which class method you are calling. The flaw of .net is that you cannot know that from your code.

Example

In Java if you define ClassB as

public class ClassB extends ClassA {
    @Override 
    public void run() {
    }
}

and object

ClassA obj=new ClassB();

If you call obj.run() how will you know if that code is following the rules of polymorphic open/close principle or it will code method related to ClassA? In Java you will know that there is always polymorphism. It is easier to make mocks and it is easier to extend classes and follow Liskov substitution principle.

On the other hand static methods are bounded to a class so if you want to call a method that is related to ClassA you can define that method like this:

public static run(ClassA obj)

and you can call it with

ClassB obj=new ClassB();
ClassA.run(obj);

and from the code you will know that the method you are calling is defined in ClassA and not in ClassB.

Saipan answered 26/1, 2014 at 6:33 Comment(2)
No. Don't make a method static when it belongs to the object. That's really bad design.Bentonbentonite
i don't think that creating static methods is a good approach here.Commotion

© 2022 - 2024 — McMap. All rights reserved.