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

Define immutable object?

583


What is the difference between @before and @beforeclass annotation?

541


Why put method is idempotent?

466


What is the difference between integer parseint and integer valueof?

552


What is lastindexof in java?

550






What do you know about java?

526


What is the reason behind using constructors and destructors?

558


Why Set interface contains unique elements, what internally implemented for this so that it contains unique elements?

7425


How do you remove duplicates from an array in java?

533


1.IN CASE OF DYNAMIC METHOD DISPATCH WHY WE USE REFERENCE VARIABLE,WE CAN USE THE DIFFERENT DEFINED OBJECT DIRECTLY TO ACCESS THE DATA MEMBER AND MEMBER FUNCTION OF THAT RESPECTIVE CLASS?WHAT IS THE MAIN FUNCTION OF "REFERENCE VARIABLE" HERE?

3793


What does main method?

566


How do you compare characters in java?

525


Difference between arraylist and hashset in java?

547


Why do we use string?

581


Why is sizeof not a function?

551