Java program to create members with private access modifier
All Answers
total answers (1)
Severity: 8192
Message: str_replace(): Passing null to parameter #3 ($subject) of type array|string is deprecated
Filename: libraries/Filtered_db.php
Line Number: 23
total answers (1)
In this program, we will create two private members in a class and access them by a method outside the class.
Program/Source Code:
The source code to create members with a "private" access modifier is given below. The given program is compiled and executed successfully.
Output:
Explanation:
In the above program, we created two classes Demo and Main. The Demo class contains two private members num1, num2. And, printed the value of private members using the printValues() method to access them outside the class.
The Main class contains a static method main(). The main() is an entry point for the program. Here, we created the object of the Demo class and called the printValues() method to access values of private members and printed them.