The immutable
class has the property that once the object is created it cannot be updated. But if the class can be extended, then its method can be overridden
to modify the existing data members later, in the below example, the addPrice
method is overridden and now can be used to modify the data members as shown below.
package org.wesome.dsalgo;
import java.util.Objects;
public class Apple {
String name;
float price;
public Apple(String name, float price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public float getPrice() {
return price;
}
/* add the new price to the existing price */
public float addPrice(float price) {
return this.price + price;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Apple apple = (Apple) o;
return Float.compare(apple.price, price) == 0 && Objects.equals(name, apple.name);
}
@Override
public int hashCode() {
return Objects.hash(name, price);
}
}
package org.wesome.dsalgo;
public class AppleExt extends Apple {
public AppleExt(String name, float price) {
super(name, price);
}
/* override the method to update the data member variable */
@Override
public float addPrice(float price) {
this.price = price;
return this.price;
}
}
package org.wesome.dsalgo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class AppleTest {
@Test
public void finalFieldTest() {
Apple apple = new AppleExt("Mcintosh", 1.1f);
apple.addPrice(2.2f);
Assertions.assertAll(() -> {
Assertions.assertEquals("Mcintosh", apple.getName());
Assertions.assertEquals(2.2f, apple.getPrice());
});
}
}
plugins {
id 'java'
id "io.freefair.lombok" version "6.2.0"
}
group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}
test {
useJUnitPlatform()
}
Immutable Class are Final
To make the class immutable
, the class should be final
, so that it cannot be extended
and its method cannot be overridden
to modify the data member later.
package org.wesome.dsalgo;
import java.util.Objects;
/* immutable class are final */
public final class Apple {
String name;
float price;
public Apple(String name, float price) {
this.name = name;
this.price = price;
}
public String getName() {
return name;
}
public float getPrice() {
return price;
}
/* add the new price to the existing price */
public float addPrice(float price) {
return this.price + price;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Apple apple = (Apple) o;
return Float.compare(apple.price, price) == 0 && Objects.equals(name, apple.name);
}
@Override
public int hashCode() {
return Objects.hash(name, price);
}
}
now Apple Class cannot be extended, this will throw a compilation error.
package org.wesome.dsalgo;
public class AppleExt extends Apple {
public AppleExt(String name, float price) {
super(name, price);
}
/* override the method to update the data member variable */
@Override
public float addPrice(float price) {
this.price = price;
return this.price;
}
}
package org.wesome.dsalgo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class AppleTest {
@Test
public void finalFieldTest() {
Apple apple = new AppleExt("Mcintosh", 1.1f);
apple.addPrice(2.2f);
Assertions.assertAll(() -> {
Assertions.assertEquals("Mcintosh", apple.getName());
Assertions.assertEquals(2.2f, apple.getPrice());
});
}
}
plugins {
id 'java'
id "io.freefair.lombok" version "6.2.0"
}
group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8
configurations {
compileOnly {
extendsFrom annotationProcessor
}
}
repositories {
mavenCentral()
}
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter:5.6.2'
}
test {
useJUnitPlatform()
}