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


Please Help Members By Posting Answers For Below Questions

What is an empirical question?

786


Explain the difference between abstract class and interface in java?

770


How do you use compareto method?

771


Explain a few methods of overloading best practices in java?

766


How do you make an arraylist empty in java?

721


What do you know about the garbate collector?

820


What is the difference between Error, defect,fault, failure and mistake?

910


What is the advantage of preparedstatement over statement?

842


Explain about the interpreter in java?

850


What is the basic concepts of OOPS?

946


How many bits is size_t?

759


How do you convert an int to a double in java?

825


Why do we use bufferedreader?

758


What is autoboxing and unboxing?

786


What is the importance of finally block in exception handling?

808