Question

In: Computer Science

In JAVA, Explain how classes properly handle data hiding and implementation hiding

In JAVA, Explain how classes properly handle data hiding and implementation hiding

Solutions

Expert Solution

In java Object Oriented Features,Encapsulation is the methodology which ensures data hiding and implementation hiding.That means provides all the functionality for the data and its association but no one in the outer world will be able to see the inner construction and appliance of it.

Encapsulation mechanism in java wraps the data variables and the function acting on the data or methods in a single unit.We can hide the variables and the codes of that particular class from other class and outer world and also these data and method features will be unable to access by the inherited classes if we declare the variables of the class.Though we can view and set the variables by setter and getter methods.

The below code will be helpful to understand the concept explained above :


public class Main
{
        public static void main(String[] args) 
        {
            //declaring class object
            Test t=new Test("John Smith","USA",23,'M');
            //calling the getter methods to view the datas
                System.out.println("Name : "+t.getName());
                System.out.println("Country : "+t.getCountry());
                System.out.println("Age : "+t.getAge());
                System.out.println("Gender : "+t.getGender());
                //we can override by using setter methods
                t.setName("John Cena");
                System.out.println("Name : "+t.getName());
                t.setCountry("Canada");
                System.out.println("Country : "+t.getCountry());
                t.setAge(45);
                System.out.println("Age : "+t.getAge());
                
        }
}

class Test
{
    //declaring variables using private keyword to ensure encapsulation
    private String Name,Country;
    private char Gender;
    private int Age;
    //constructor of the class
    Test(String Name,String Country,int Age,char Gender)
    {
        this.Name=Name;
        this.Country=Country;
        this.Age=Age;
        this.Gender=Gender;
    }
    //as the variables are declared as privade 
    //it cannot be accessed outside the class 
    //it can be accessed oytside this class only by 
    //using setter and getter methods as shown below
    public void setName(String Name)
    {
        this.Name=Name;
    }
    public String getName()
    {
        return this.Name;
    }
    public void setCountry(String Country)
    {
        this.Country=Country;
    }
    public String getCountry()
    {
        return this.Country;
    }
    public void setAge(int Age)
    {
        this.Age=Age;
    }
    public int getAge()
    {
        return this.Age;
    }
    public void setGender(char Gender)
    {
        this.Gender=Gender;
    }
    public char getGender()
    {
        return this.Gender;
    }
}

Related Solutions

In JAVA, Briefly explain how objects are able to achieve both data hiding and implementation hiding.
In JAVA, Briefly explain how objects are able to achieve both data hiding and implementation hiding.
Explain an Is-a relationship between classes in Java
Explain an Is-a relationship between classes in Java
Part A. explain missing values in data and how to handle it Part B. Select true...
Part A. explain missing values in data and how to handle it Part B. Select true or false for the following questions One of the two possible causes or explanations for the differences that occur between groups or treatments in ANOVA is that the differences are due to treatment effects. T F Another possible cause or explanation for the differences that occur between groups or treatments in ANOVA is that the differences occur simply due to chance. T F Post...
how do you handle missing or correupt data in data set
how do you handle missing or correupt data in data set
Using a minimum of 2 classes create a java program that writes data to a file...
Using a minimum of 2 classes create a java program that writes data to a file when stopped and reads data from a file when started. The data should be in a readable format and the program should work in a way that stopping and starting is irrelevant (e.g. all data doesn't have to save just the important elements.) Program should be unique and semi-complex in some way.
in java. Design classes and data structure for a call center. Imagine that a call center...
in java. Design classes and data structure for a call center. Imagine that a call center has one fresher, one Technical Lead and one Manager. An incoming telephone call must be allocated to a fresher. If a fresher can’t handle the call, he or she must escalate the call to technical lead. If the TL is not free or not able to handle it, then the call should be escalated to Manager. If all three are busy , then put...
Code in Java 1. Create a class Flower with data: Name, Price, Color and properly methods....
Code in Java 1. Create a class Flower with data: Name, Price, Color and properly methods. 2. Create another class named ListFlower. This class manages a collection of Flower (may be LinkedList) named a. Implementing some methods for ListFlower: Add: add new item of Flower to a Display: display all items of a sort(): sort as descending by Price and display all items of a search(Flower f): check and return whether f is exists in a or not. delete(int pos):...
(Java) Please describe how API's can be created using abstract classes, interfaces and regular classes.
(Java) Please describe how API's can be created using abstract classes, interfaces and regular classes.
How do objects enhance Java? How do objects relate to classes and methods?
How do objects enhance Java? How do objects relate to classes and methods?
When can the Coase Theorem properly handle a situation where an externality exists? Provide an example....
When can the Coase Theorem properly handle a situation where an externality exists? Provide an example. In what types of situations can the Coase Theorem not properly handle an externality? How can they be addressed instead?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT