Skip to main content

Data hiding in java

In java it is achieved by using a keyword 'private' keyword and the process is called data hiding. It is used as security such that no internal data will be accessed without authentication. An unauthorized end user will not get access to internal data.

v   Access Specifier

  1. Public
  2. Private
  3. Protected
  4. Default

 

 

Access From

Public

default

private

In the Class

          

In the same package

 

In another Package

 

 

     1. Public :

    -        Public member access with in the class with in the same package and with in another package.

    -        Example:

         When methods, variables, classes, and so on are declared public, then we can access them from anywhere. The public access modifier has no scope restriction. For example,

// Animal.java file
// public class
public class Animal {
    // public variable
    public int legCount;
 
    // public method
    public void display() {
        System.out.println("I am an animal.");
        System.out.println("I have " + legCount + " legs.");
    }
}
 
// Main.java
public class Main {
    public static void main( String[] args ) {
        // accessing the public class
        Animal animal = new Animal();
 
        // accessing the public variable
        animal.legCount = 4;
        // accessing the public method
        animal.display();
    }
}

 

Output:

I am an animal.
I have 4 legs.

 2.  Private :

-        Private member only access in class.

Example:

class Data {
    // private variable
    private String name;
}
 
public class Main {
    public static void main(String[] main){
 
        // create an object of Data
        Data d = new Data();
 
        // access private variable and field from another class
        d.name = "Tech Ocean";
    }
}

 

Output:

Main.java:18: error: name has private access in Data

        d.name = "Tech Ocean";

 -> The error is generated because we are trying to access the private variable of the Data class from the Main class.

->  You might be wondering what if we need to access those private variables. In this case, we can use the getters and setters method. For example,

 

class Data {
    private String name;
 
    // getter method
    public String getName() {
        return this.name;
    }
    // setter method
    public void setName(String name) {
        this.name= name;
    }
}
public class Main {
    public static void main(String[] main){
        Data d = new Data();
 
        // access the private variable using the getter and setter
        d.setName("Tech Ocean");
        System.out.println(d.getName());
    }
}

 

Output:

Tech Ocean

 

 3.  Protected :

-        Protected member access from derived/child  class .

-        When methods and data members are declared protected, we can access them within the same package as well as from subclasses. For example,

class Animal {
    // protected method
    protected void display() {
        System.out.println("I am an animal");
    }
}
 
class Dog extends Animal {
    public static void main(String[] args) {
 
        // create an object of Dog class
        Dog dog = new Dog();
         // access protected method
        dog.display();
    }
}

 

Output:

I am an animal

 4.     Default :

-        Default member access from same class and in the same package.

Example:

class Data {
    // default variable
    String name;
}
 
public class Main {
    public static void main(String[] main){
 
        // create an object of Data
        Data d = new Data();
 
        // access default variable and field from another class
        d.name = "Tech Ocean";
        System.out.println(d.name());
    }
}

 

Output:

Tech Ocean

 

Comments

Post a Comment

Popular posts from this blog

Unveiling the Growth Story: IDFC First Bank Share Price Analysis

Unveiling the Growth Story: IDFC First Bank Share Price Analysis In the dynamic world of finance, the stock market stands as a platform for investors to explore potential growth opportunities. One such intriguing player is IDFC First Bank, a prominent banking institution in India. In this blog, we delve into the journey of IDFC First Bank's share price, analyzing its historical performance, key factors influencing its value, and what the future might hold for investors. Understanding IDFC First Bank: IDFC First Bank, formerly known as IDFC Bank, is a financial institution that offers a wide range of banking services, including retail banking, wholesale banking, and treasury operations. With a customer-centric approach and a focus on digital innovation, the bank aims to provide seamless banking experiences to individuals and businesses alike. Historical Performance: To comprehend the trajectory of IDFC First Bank's share price, let's take a glance at its historical performa...

Java Basics

What is Java? Java is a high-level, general-purpose, object-oriented, and secure programming language developed by James Gosling at Sun Microsystems, Inc. in 1991. Features of Java Simple:  Java is a simple language because its syntax is simple, clean, and easy to understand. Complex and ambiguous concepts of C++ are either eliminated or re-implemented in Java. For example, pointer and operator overloading are not used in Java. Object-Oriented:  In Java, everything is in the form of the object. It means it has some data and behavior. A program must have at least one class and object. Robust:  Java makes an effort to check error at run time and compile time. It uses a strong memory management system called garbage collector. Exception handling and garbage collection features make it strong. Secure:  Java is a secure programming language because it has no explicit pointer and programs runs in the virtual machine. Java contains a security manager that defines the access...

India's Growing Economy

Introduction India's economy has been on a remarkable growth trajectory in recent years, solidifying its position as one of the fastest-growing economies in the world. The country's economic rise has significant implications not only for its citizens but also for the global economic landscape. In this article, we delve into the factors driving India's economic growth, examine its historical perspective, discuss the role of economic reforms, analyze key drivers of growth, explore foreign direct investment (FDI), highlight challenges and opportunities, and provide a glimpse into India's future potential. Historical Perspective To understand the magnitude of India's current economic growth, we must reflect on its historical journey. India gained independence in 1947 and embarked on a path of development and self-reliance. However, progress was slow due to various challenges, including a regulated economy and restrictive policies. It was not until the early 1990s that I...