answersLogoWhite

0

When do you create a method or a class final?

Updated: 8/19/2019
User Avatar

Wiki User

12y ago

Best Answer

The keyword "final" means that the data will not be able to be edited at a later time. The code:

final int accounts = 10;

would make the integer "accounts" equal to ten and would return an error if you were to later do something like:

accounts = 5;

This is useful if you want to make sure that you don't accidentally change data.

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: When do you create a method or a class final?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Discuss final method and final classes?

a method declared final can not be overridden, and a class declared as final can not be extended by its sub class.


When do you declare a method or class final?

if we declare a method as final then it can be changed.


Can you have a final abstract class?

No. The abstract keyword means that you cannot instantiate the class unless you extend it with a subclass. The final keyword means that you cannot create subclasses of that class.Combining them would lead to an unusable class, so the compiler will not let this happen.


Can you override an instance method and make it final?

No. Once a method is declared final in a class, no derivative of that class can override that method.


How can you create a class which cannot be inheritable?

Declare it final.


How to create immutable class in java?

by making that class final


Can be create a method within a method in java?

I don't believe it is possible, and anyways there really isn't any reason to do that. Just create a new method in the same class, or in a "utility" class


How do you prevent method overriding in java?

Use the word "final" directly preceding your method declaration. The presence of final keyword directs java to ensure that, this particular method would not be overridden by any of its child classes.


Why final method are used in java?

Final methods are used when a class is inheritable but should have some functions that must not be overridden for them to operate properly. This allows a class that can be inherited, but still have limits on its customization. Usually, final methods are declared so that a method that accepts a parameter of class A can accept a parameter of class B that inherits from A, and the designer of class A can still be certain that the method will operate as intended regardless of what the developer does in class B.


When do you declare a method final in java?

You declare a method final in Java when you do not want any subclasses of your class to be able to override the method. I have also heard that this allows the Java compiler to make more intelligent decisions. For example, it supposedly allows Java to decide when to make a method inline. (Note that this is all unconfirmed)


What is the relationship between inheritance methods and the keyword final?

They are inversely related. That is: If you declare a method as final you cannot overridden in the child class If you declare a class as final you cannot inherit it in any other class.


How do you create a concrete method inside abstract class?

The same way you create a concrete method in a concrete class. When a class is abstract, it can contain abstract methods. That doesn't mean that all methods must be abstract. Hope this helps.