answersLogoWhite

0

How class accomplish data hiding?

Updated: 8/10/2023
User Avatar

Wiki User

13y ago

Best Answer

Data hiding is one of the important aspects of a class. Data hiding means to hide the members of the class and providing the access to only some of them. we can make the members of the class private or public. in private,the outside world cannot access those members which been made private.and rest we can make public.only the public members are accessed by the out side world and all the private members can be accessed using only the public members.in this way a class provides security to its data members.

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

16y ago

A class is a collection of data and methods to work with that, and other, data. An object is a specific instance of a class. For example, "ArrayList" is a class that contains methods such as "add" and "remove" for changing the items in the list. The instances (objects) of ArrayList can use these methods to perform operations. It's easier to show the structure of things with a custom class.

public class MyClass

{

//Data / "Fields":

private int num;

//Constructors:

public MyClass()

{

}

public MyClass(int initialNumValue)

{

num = initialNumValue;

}

//"Getters" and "Setters":

public int getNum()

{

return num;

}

public void setNum(int newNumValue)

{

num = newNumValue;

}

}

The above code is an example of a class declaration. It's the blueprint for all MyClass objects. Take note of the use of the keywords "private" and "public". This is an example of data hiding, also called encapsulation. The actual number is hidden so that no other class can modify its values. The other classes have to use the provided public methods to modify the value of num. While in simple cases, this really is not necessary, it is a good practice, because in many cases, you do not want to provide full visibility, or you want to do something special when the value of something is changed. This also helps when debugging a program.

Now let's use the above class to demonstrate some of these concepts. Pretend that this is the main method for AnotherClass:

MyClass myObject = new MyClass(); //myObject is an instance of MyClass

MyClass mySecondObject = new MyClass(2); // mySecondObject is another instance

// of MyClass

//At this point, the number stored in myObject is zero, and the number stored in

// mySecondObject is 2.

//INVALID:

myObject.num = 3;

//AnotherClass cannot directly see the variable num in myObject, because the class

// file MyClass declared it as private. However, the following code is valid because

// it uses the provided public methods:

myObject.setValue( 3 );

//Now the value of num in myObject is 3.

mySecondObject.setValue( myObject.getValue() );

myObject.setValue( 1 );

//Now mySecondObject's num value is 3, and myObject's num value is 1.

Generally, you will always want to (or be required to) encapsulate your data / fields, that is to say, declare them as private, and make public methods that get or set the value stored in them. This will, in the long run, allow your classes to be more flexible and adaptable for other uses.

This answer is:
User Avatar

User Avatar

Wiki User

14y ago

Information hiding is an object-oriented concept in which different classes are kept separate from one another, so that a major change in one class results in no changes in any other classes.

Information hiding depends on code implementing the concepts of low coupling and high cohesion.

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

Data Hiding ensures that members of a class are not visible to classes that dont need to see/view them. This is accomplished using the access modifiers.

An Access Modifier is a key word in java that determines what level of access or visibility a particular java variable/method or class has. There are 4 basic access modifiers in java. They are:

1. Public

2. Protected

3. Default and

4. Private

To implement Data hiding i would make members private.

Members marked private can't be accessed by code in any class other than the class in which the private member was declared. Let's make a small change to the Parent class from an earlier example.

package pack1;

public class Parent {

private String getName() {

return "Parent";

}

}

Now the getName() method is private and is not visible from within the Child class and the same piece of code would throw up the below compilation error when you try to compile the class.

cannot find symbol

symbol : method getName()

This error method looks as if the method getName() does not exist at all. Of course the method exists but unfortunately it is declared private and hence no class (except the class that has the exact code written into it) can access it. This includes a child class that extends the parent class that has the method.

This answer is:
User Avatar

User Avatar

Wiki User

8y ago

Data hiding is a bit of a misnomer because you cannot physically hide data within a class. What you can do is limit access to that data by declaring it private. This is not hiding data, its perfectly visible to anyone with access to the class definition.

What you call data hiding, programmers call encapsulation. Private data is only accessible to class members and friends of the class. By limiting accessibility, class designers ensure that the class representation cannot be invalidated by errant code outside of the class and its friends, that is code that is outwith the control of the class designer.

Class designers can also declare data protected or public, but as soon as they do that the data is no longer encapsulated. Protected data is the same as private data but is also accessible to derived classes. The class designer has no control over derived class implementations and this undermines encapsulation. If encapsulation were not an issue, the data would not need protected let alone be private, it could simply be declared public. The protected keyword should only ever apply to private interfaces (functions or methods) that class implementers also require access to.

The public interface is available to any code and provides the means by which ordinary users of the class can interact with objects of the class. There is no need for an ordinary user to know the implementation details of a class, the public interface should provide everything they require in order to use the class. Class implementers, on the other hand, can make use of the protected interface to provide a more specialised interface, one that suits their specific, derived classes. But only the class designer has access to the private interface.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

A class is a pattern to combine to two different variables which can be used to create an object. Data hiding is to separate public from private class.

This answer is:
User Avatar

User Avatar

Wiki User

10y ago

it is possible by private declaration

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How class accomplish data hiding?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the difference between data hidding and data abstraction?

Abstraction: Abstraction refers to removal/reduction of irrelevant data or unnecessary data or confidential data from a Class. Data hiding: Data hiding is a feature provided by the abstraction for hiding the data from the class.


In ANSI C you can hide data by using private access specifier Justify?

One can always declare a datatype as static which will limit the scope to the file only. this way data hiding can be achived. For more clearance on the same please refer 'the C programming language'. Data hiding means only relevant data is visible to the user and all the background information is hidden from the user. In c++, the variables are named as data members and these can be hidden with the help of private access specifier. In procedural languages, variables(global variables) are free to flow from functions to functions and hence they were not secured. But in C++, only the class in which the data members are being declared can access them by using private specifier. As, the data members and also member functions of a class cannot be accessed outside the class if they have private access so, they get hidden from the user and hence the data hiding is achieved. Also in inheritance when we derive a class from the base class then the derived class cannot access the private members of the base class. In addition, if a class is derived from another class privately i.e. for example syntax : class B : private A , is used then all the public and protected members (not private) becomes private to class B and cannot be accessed outside the class B, even by using the object of class B. Hence, data hiding is achieved in C++ through private access specifier.


How data hiding is achieved in java?

Data hiding in the java is achieved through the use of encapsulation.


Why encapsulation does not implement information hiding in object orientation?

Encapsulation also implements data hiding in an object oriented programming design. By encapsulating various methods & data objects into a single class they can also be hidden from all the other classes. You can declare the variables and methods as private and that way you can hide the data from the other classes in the application.


Data hiding in oops?

Possible. More precisely, it is data-protection.

Related questions

What is the difference between data hidding and data abstraction?

Abstraction: Abstraction refers to removal/reduction of irrelevant data or unnecessary data or confidential data from a Class. Data hiding: Data hiding is a feature provided by the abstraction for hiding the data from the class.


What term in java is used to indicate that a class's internal state is hidden from its clients?

Encapsulation............Hiding the data from others


In ANSI C you can hide data by using private access specifier Justify?

One can always declare a datatype as static which will limit the scope to the file only. this way data hiding can be achived. For more clearance on the same please refer 'the C programming language'. Data hiding means only relevant data is visible to the user and all the background information is hidden from the user. In c++, the variables are named as data members and these can be hidden with the help of private access specifier. In procedural languages, variables(global variables) are free to flow from functions to functions and hence they were not secured. But in C++, only the class in which the data members are being declared can access them by using private specifier. As, the data members and also member functions of a class cannot be accessed outside the class if they have private access so, they get hidden from the user and hence the data hiding is achieved. Also in inheritance when we derive a class from the base class then the derived class cannot access the private members of the base class. In addition, if a class is derived from another class privately i.e. for example syntax : class B : private A , is used then all the public and protected members (not private) becomes private to class B and cannot be accessed outside the class B, even by using the object of class B. Hence, data hiding is achieved in C++ through private access specifier.


How data hiding is achieved in java?

Data hiding in the java is achieved through the use of encapsulation.


What are the benefits of data hiding?

Data hiding enhances security by restricting access to sensitive information, reduces complexity by encapsulating data within a class or module, and promotes modularity by allowing different components to interact without revealing internal details. It also helps in maintaining code integrity and avoiding unintended data manipulation.


Why encapsulation does not implement information hiding in object orientation?

Encapsulation also implements data hiding in an object oriented programming design. By encapsulating various methods & data objects into a single class they can also be hidden from all the other classes. You can declare the variables and methods as private and that way you can hide the data from the other classes in the application.


Which tools are used in robust video data hiding using forbidden zone data hiding?

RA(Repeat Accumulator)


Is data hiding and abstraction are same?

no


What is encapsulation and data hiding in dbms?

ODBMS stands for object oriented database management system. Encapsulation in ODBMS can be defined as binding of data together.


Data hiding in oops?

Possible. More precisely, it is data-protection.


What is Data Hiding in VBNET?

Data hiding is used method used in Object-Oriented programing to hide information within computer code


What is data hidding in oop?

Data hiding in OOP refers to the practice of hiding the internal state of an object from the outside world. This is achieved by encapsulating the data within the object and only allowing access to it through specified methods, known as getters and setters. By hiding the data, we protect it from being modified unintentionally and improve the integrity of the object's internal structure.