Computer Applications
In what way does a class enforce data hiding?
Encapsulation & Inheritance in Java
31 Likes
Answer
A class enforces data hiding through access specifiers. Java provides three access specifiers for its class members — public, private and protected. The private members of a class are only accessible within the class. They are hidden to the code outside the class. The protected members are accessible from classes within the same package and subclasses.
Answered By
20 Likes
Related Questions
A number is said to be Armstrong if the sum of cube of its digits is equal to the same number.
For example,
153 = 13 + 53 + 33
Hence, 153 is an Armstrong number.A program using a class is designed below to check and display whether a number 'num' is an Armstrong number or not. There are some places in the program left blank marked with ?1?, ?2?, ?3? and ?4? to be filled with suitable condition/expression.
class Armstrong { private int num; Armstrong(int n) { num = n; boolean check( ) { int nm = num; while(...?1?...) { digit = nm % 10; sum = sum + ...?2?... ; nm = ...?3?... ; } if (sum == num) return(true); else return(false); } void display( ) { boolean ch = check( ); if(ch == ...?4?... ) System.out.println("Number is Armstrong"); else System.out.println("Number is not Armstrong"); } } }Based on the above discussion, answer the following questions:
(a) What will be filled in place of ?1?
(b) What will be filled in place of ?2?
(c) What will be filled in place of ?3?
(d) What will be filled in place of ?4?
What is encapsulation?
What are the types of visibility modes used in Java?
What will happen if data members are declared private.