Wednesday, 23 April 2014

Posted by Unknown  
Tagged as:
21:54

when you're talking about "new mutable field and override getter of an existing previously immutable field" you're talking about new class. Class immutability is not inherited property. It is quite possible to create mutable class by extending immutable one. Also let me point out that getter has no special connection to corresponding field from JVM point of view - it is just programming paradigm, design pattern if you will. So when you override getter you don't do anything to final field of the immutable class - let me show you: 
public class Immutable1 { 
public final int a; 
public Immutable1(int a){ 
this.a = a; 


public int getA(){ 
return a; 



class Mutable extends Immutable1 { 
int a; 
public Mutable(int a){ 
super(a); 
this.a = a; 

public int getImmutableA(){ 
return super.getA(); 


public int getA(){ 
return a; 


public static void main(String[] args){ 
Mutable aa= new Mutable(5); 
aa.a= 6; 

System.out.println("Mutable "+aa.getA()); 
System.out.println("Immutable"+aa.getImmutableA()); 


About the Author

Write admin description here..

0 comments:

Pages

What they says

Proudly Powered by Ujwala Rathod.
back to top