//Shadowing allows for the overlapping scopes of members with the same name and type to exist in both a nested class and the enclosing class simultaneously. Depending on which object we use to call the same variable in a main method will result in different outputs.
class Book {
String type="Nonfiction";
// Nested inner class
class Biography {
String type="Biography";
public void print(){
System.out.println(Book.this.type);
System.out.println(type);
}
}
}
public class Books {
public static void main(String[] args) {
Book book = new Book();
Book.Biography bio = book.new Biography();
bio.print();
}
}
//Nonfiction
//Biography
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter