Shadowing

PHOTO EMBED

Thu Dec 29 2022 12:48:08 GMT+0000 (Coordinated Universal Time)

Saved by @prettyleka #java #generics

//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
content_copyCOPY