what is finalmethod & final variable with example?
Answer Posted / aditi
Final method cannot be overridden by subclasses. This is
used to prevent unexpected behavior from a subclass
altering a method.
Example:
public class MyClass {
public final void myFinalMethod() {...}
Final Variable, it can only be assigned to once and only in
the constructor of class if variable is not a reference to
pther object.
Example:
public class Sphere {
public static final double PI = 3.141592653589793; //
this might as well be a constant
public final double radius;
public final double xpos;
public final double ypos;
public final double zpos;
Sphere(double x, double y, double z, double r) {
radius = r;
xpos = x;
ypos = y;
zpos = z;
}
| Is This Answer Correct ? | 10 Yes | 0 No |
Post New Answer View All Answers
When should you make a function static?
Can a class extend more than one class?
Explain a few methods of overloading best practices in java?
What are the 4 types of characters?
What is the major difference between linkedlist and arraylist?
When do you call copy constructor?
What is a nested class?
What about abstract classes in java?
What two classes are used to read data only?
Does sprintf allocate memory?
What is the final blank variable?
What is the use of runnable interface?
What is functional interface in java?
What is final?
Explain what access modifiers can be used for methods?