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

Which list is sorted in java?

564


In a container there are 5 components. I want to display all the component names, how will you do that?

1532


Does unicode support all languages?

501


Java is pass by value or pass by reference? Explain

510


Tell me the latest versions in java related areas?

554






What mechanism does java use for memory management?

460


Can arraylist contain null values?

547


What is the latest java version?

536


What is data type example?

525


Is string passed by reference in java?

527


What do negative exponents mean?

524


Explain importance of inheritance in java?

583


Can an interface be final?

528


What is the purpose of encapsulation?

520


Under what conditions is an object’s finalize() method invoked by the garbage collector?

555