This document discusses visibility control and access specifiers in Java, including public, protected, default/friendly, and private access. It also covers Java enumerated data types (enums), which allow defining a data type that contains a fixed set of constants. Enums can be used for things like days of the week or directions. The document notes that Java enums improve type safety, can be used in switch statements, can be traversed, can have fields/constructors/methods, and may implement interfaces but cannot extend classes since they internally extend the Enum class. An example program is provided to demonstrate printing the values of a Season enum.
This document discusses visibility control and access specifiers in Java, including public, protected, default/friendly, and private access. It also covers Java enumerated data types (enums), which allow defining a data type that contains a fixed set of constants. Enums can be used for things like days of the week or directions. The document notes that Java enums improve type safety, can be used in switch statements, can be traversed, can have fields/constructors/methods, and may implement interfaces but cannot extend classes since they internally extend the Enum class. An example program is provided to demonstrate printing the values of a Season enum.
This document discusses visibility control and access specifiers in Java, including public, protected, default/friendly, and private access. It also covers Java enumerated data types (enums), which allow defining a data type that contains a fixed set of constants. Enums can be used for things like days of the week or directions. The document notes that Java enums improve type safety, can be used in switch statements, can be traversed, can have fields/constructors/methods, and may implement interfaces but cannot extend classes since they internally extend the Enum class. An example program is provided to demonstrate printing the values of a Season enum.
Java Visibility Control • Many times it becomes necessary in some situations to restrict the access to certain variables and methods from outside the class. • There are following access specifiers in Java: --public --protected --friendly or default --private public protected default or friendly private Access Protection Table Java Enumerated Data Type: • Enum in java is a data type that contains fixed set of constants. • It can be used for days of the week (SUNDAY, MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY and SATURDAY) , directions (NORTH, SOUTH, EAST and WEST) etc. • The java enum constants are static and final implicitly. It is available from JDK 1.5. • Java Enums can be thought of as classes that have fixed set of constants. Java Enumerated Data Type cont.: Points to remember for Java Enum: • enum improves type safety • enum can be easily used in switch • enum can be traversed • enum can have fields, constructors and methods • enum may implement many interfaces but cannot extend any class because it internally extends Enum class Java Enumerated Data Type cont.: class EnumExample1 { public enum Season { WINTER, SPRING, SUMMER, FALL } public static void main(String[] args) { for (Season s : Season.values()) System.out.println(s); } } Output: WINTER SPRING SUMMER FALL