Category: 6. OOP (Basic)
-
Create an enum class
Java program to create an enum class Output In the above example, we have created an enum class named Size. The class contains four constants SMALL, MEDIUM, LARGE, and EXTRALARGE. Here, the compiler automatically converts all the constants of the enum into its instances. Hence, we can call the method using the constant as objects. In this call, the this keyword is…
-
Implement private constructors
Java program to create a private constructor Output In the above example, we have created a private constructor of the Test class. Hence, we cannot create an object of the Test class outside of the class. This is why we have created a public static method named instanceMethod() inside the class that is used to create an object of the Test class. And from the Main class,…
-
Determine the class of an object
Check the class of an object using getClass() Output In the above example, we have used the getClass() method of the Object class to get the class name of the objects obj1 and obj2.