Encapsulation (computer programming): Difference between revisions

Content deleted Content added
m Improved formatting of examples.
Line 19:
Below is an example in [[C Sharp (programming language)|C#]] that shows how access to a data field can be protected through the use of a <code>private</code> keyword:
<source lang="csharp">
namespace Encapsulation {
class Program {
{
public class ProgramAccount {
{
public class Account
{
private decimal accountBalance = 500.00m;
public decimal CheckBalance() {
{
return accountBalance;
}
}
 
static void Main() {
{
Account myAccount = new Account();
decimal myBalance = myAccount.CheckBalance();
 
//* This Main method can check the balance via the public
// * "CheckBalance" method provided by the "Account" class
// * but it cannot manipulate the value of "accountBalance" */
}
}
Line 49 ⟶ 44:
<source lang="java">
 
public class Employee {
{
private float salary = 50000.00;
public float getSalary() {
{
return salary;
}
 
public static void main() {
{
Employee e = new Employee();
float sal = e.getSalary();