📜  Java中数据隐藏和抽象的区别

📅  最后修改于: 2022-05-13 01:55:18.255000             🧑  作者: Mango

Java中数据隐藏和抽象的区别

抽象隐藏内部实现,只突出显示服务集。它是通过使用抽象类和接口并进一步实现它们来实现的。只有一个对象的必要特征才能将它与所有其他对象区分开来。只强调重要的细节,其余的都对用户或读者隐瞒。

抽象的真实例子

抽象的类型:基本上有三种抽象类型

  1. 程序抽象
  2. 数据抽象
  3. 控制抽象

1.过程抽象:从词本身来看,就是一系列的过程,以函数的形式依次进行,通过类来实现抽象。

2.数据抽象:从词本身来看,抽象是从描述一个对象的一组数据中实现的。

3.控制抽象:抽象是通过编写程序实现的,其中包含对象细节。

抽象的优点:

  • 用户或社区可以实现安全,因为内部实现没有亮点。
  • 增强将变得非常容易,因为在不影响最终用户的情况下,可以在 内部系统
  • 它为最终用户提供了更大的灵活性,可以非常轻松地使用系统
  • 提高了应用的丰富性

抽象的实现:它被实现为一个只代表重要特征的类,不包括背景细节。仅提供必要的细节并隐藏其所有内部实现。下面是抽象的Java实现:

Java
// Java program showing the working of abstraction
 
// Importing generic libraries
import java.io.*;
 
// Creating an abstract class
// demonstrate abstraction
abstract class Creature {
 
    // Just providing that creatures has legs
    // Hiding the number of legs
    abstract void No_Of_legs();
}
 
// A new child class is extending
// the parent abstract class above
class Elephant extends Creature {
 
    // Implementation of the abstract method
    void No_Of_legs()
    {
 
        // Printing message of function in non abstract
        // child class
        System.out.println("It has four legs");
    }
}
 
// Again a new child class is extended from parent
//  Human class to override function created above
class Human extends Creature {
 
    // Same function over-riden
    public void No_Of_legs()
    {
 
        // Message printed if this function is called or
        // Implementation of the abstract method
        System.out.println("It has two legs");
    }
}
 
public class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
 
        // Creating human object showing the implementation
        Human ob = new Human();
 
        ob.No_Of_legs();
 
        // Creating object of above class in  main
        Elephant ob1 = new Elephant();
 
        // Calling the function in main by
        // creating object of above non abstract class
        ob1.No_Of_legs();
        // Implementation of abstraction
    }
}


Java
// Java Program showing working of data hiding
 
// Importing generic libraries
import java.io.*;
 
// Class created named Bank
class Bank {
 
    // Private data (data hiding)
    private long CurBalance = 0;
 
    // Bank_id is checked for authentication
    long bank_id;
    String name;
 
    // Getter function to modify private data
    public long get_balance(long Id)
    {
 
        // Checking whether the user is
        // authorised or unauthorised
 
        // Comparing bank_id of user and the give Id
        // then only it will get access
        if (this.bank_id == Id) {
 
            // Return current balance
            return CurBalance;
        }
 
        // Unauthorised user
        return -1;
    }
    // Setter function
    public void set_balance(long balance, long Id)
    {
        // Comparing bank_id of user and the give Id
        // then only it will get access
        if (this.bank_id == Id) {
            // Update balance in current ID
            CurBalance = CurBalance + balance;
        }
    }
}
 
// Another class created- Employee
public class Emp {
    public static void main(String[] args)
    {
        // Creating employee object of bank type
        Bank _emp = new Bank();
 
        // Assigning employee object values
        _emp.bank_id = 12345;
        _emp.name = "Roshan";
 
        // _emp.get_balance(123456)
        _emp.set_balance(10000, 12345);
        // This will no get access as bank_id is given wrong
        // so
        // unauthorised user is not getting access that is
        // data hiding
 
        long emp_balance = _emp.get_balance(12345);
        // As this time it is valid user it will get access
 
        // Display commands
        System.out.println("User Name"
                           + "  " + _emp.name);
        System.out.println("Bank_ID"
                           + "  " + _emp.bank_id);
        System.out.println("Current Balance"
                           + "  " + emp_balance);
    }
}



输出
It has two legs
It has four legs

现在,跳到第二个概念,尽管这两个概念都用于以某种方式实现封装,但有一个圆滑的区别,如下所示:

数据隐藏 对外部用户隐藏内部数据。内部数据不应直接进入,即外部人员/类无法直接访问内部数据。它是通过使用访问说明符 - 私有修饰符来实现的。

注意:推荐的数据成员修饰符是私有的。数据隐藏的主要优点是安全

数据隐藏示例:

它用作安全性,以便在未经身份验证的情况下不会访问内部数据。未经授权的最终用户将无法访问内部数据。以编程方式,我们可以通过将数据元素声明为私有来实现数据隐藏。现在为了访问这些数据或进行修改,我们分别有一个称为 getter setter 的特殊方法。

getter用于访问私有数据,setter用于在认证后修改私有数据,简单来说就是对外部用户隐藏内部数据。

它用作安全性,以便在未经身份验证的情况下不会访问内部数据。未经授权的最终用户将无法访问内部数据。以编程方式,我们可以通过将数据元素声明为私有来实现数据隐藏。

现在为了访问这些数据或进行修改,我们分别有一个称为 getter setter 的特殊方法

数据隐藏涉及的概念: Getter 和 setter

getter 用于访问私有数据,setter 用于在认证后修改私有数据。简单来说,就是对外部用户隐藏内部数据。它用作安全性,以便在未经身份验证的情况下不会访问内部数据。未经授权的最终用户将无法访问内部数据。以编程方式,我们可以通过将数据元素声明为私有来实现数据隐藏。现在为了访问这些数据或进行修改,我们分别有一个称为 getter setter 的特殊方法。

getter 用于访问私有数据,setter 用于在认证后修改私有数据。简单来说,就是对外部用户隐藏内部数据。它用作安全性,以便在未经身份验证的情况下不会访问内部数据。未经授权的最终用户将无法访问内部数据。以编程方式,我们可以通过将数据元素声明为私有来实现数据隐藏。

现在为了访问这些数据或进行修改,我们分别有一个称为 getter setter 的特殊方法。 getter 用于访问私有数据,setter 用于在认证后修改私有数据。

数据隐藏的实现:

Java

// Java Program showing working of data hiding
 
// Importing generic libraries
import java.io.*;
 
// Class created named Bank
class Bank {
 
    // Private data (data hiding)
    private long CurBalance = 0;
 
    // Bank_id is checked for authentication
    long bank_id;
    String name;
 
    // Getter function to modify private data
    public long get_balance(long Id)
    {
 
        // Checking whether the user is
        // authorised or unauthorised
 
        // Comparing bank_id of user and the give Id
        // then only it will get access
        if (this.bank_id == Id) {
 
            // Return current balance
            return CurBalance;
        }
 
        // Unauthorised user
        return -1;
    }
    // Setter function
    public void set_balance(long balance, long Id)
    {
        // Comparing bank_id of user and the give Id
        // then only it will get access
        if (this.bank_id == Id) {
            // Update balance in current ID
            CurBalance = CurBalance + balance;
        }
    }
}
 
// Another class created- Employee
public class Emp {
    public static void main(String[] args)
    {
        // Creating employee object of bank type
        Bank _emp = new Bank();
 
        // Assigning employee object values
        _emp.bank_id = 12345;
        _emp.name = "Roshan";
 
        // _emp.get_balance(123456)
        _emp.set_balance(10000, 12345);
        // This will no get access as bank_id is given wrong
        // so
        // unauthorised user is not getting access that is
        // data hiding
 
        long emp_balance = _emp.get_balance(12345);
        // As this time it is valid user it will get access
 
        // Display commands
        System.out.println("User Name"
                           + "  " + _emp.name);
        System.out.println("Bank_ID"
                           + "  " + _emp.bank_id);
        System.out.println("Current Balance"
                           + "  " + emp_balance);
    }
}


输出:

User Name Roshan
Bank_ID 12345
Current Balance 10000