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 keyword in oop?

749


When we serialize an object does the serialization mechanism saves its references too?

728


Is string passed by reference in java?

766


What is difference between protected and private?

762


What is the functionability stubs and skeletons?

906


You're given a Boolean 2D matrix, can you find the number of islands?

854


Why is inheritance used in java?

833


What do you mean by flow of struts?

1013


What is the difference between jdk and jre?

834


Can we use return in constructor?

689


why would you use a synchronized block vs. Synchronized method? : Java thread

752


Can you declare the main method as final?

726


What is sizeof in java?

868


Is array primitive data type in java?

758


Write a program to print fibonacci series up to count 10.

704