Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Information hiding is a unique feature of Object-Oriented Programming. Sometimes

ID: 3770861 • Letter: I

Question

Information hiding is a unique feature of Object-Oriented Programming. Sometimes, we may want to break the regular access rule via friend, package, internal, or inner class. This question has the following setting of an information hiding problem: This question consists of four following sub-questions: Implement the above problem in C# via internal, and test the results in you Main( ) routine; Summarize what are your findings about breaking the conventional access rule. What are the similarity and difference between friend, package, and internal ?

Explanation / Answer

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BankAccountApp
{
    class Person
    {
        private string lastName;
        private string firstName;
        private string gender;
        private string dateOfBirth;
        private string residentialAddress;

        public void Read()
        {
            Console.Write("Enter Last Name: ");
            lastName = Console.ReadLine();
            Console.Write("Enter First Name: ");
            firstName = Console.ReadLine();
            Console.Write("Enter Gender: ");
            gender = Console.ReadLine();
            Console.Write("Enter Date of Birth: ");
            dateOfBirth = Console.ReadLine();
            Console.Write("Enter Residential Address: ");
            residentialAddress = Console.ReadLine();
        }

        public void Display()
        {
            Console.WriteLine("Enter Last Name: {0}", lastName);
            Console.WriteLine("Enter First Name: {0}", firstName);
            Console.WriteLine("Enter Gender: {0}", gender);
            Console.WriteLine("Enter Date of Birth: {0}", dateOfBirth);
            Console.WriteLine("Enter Residential Address: {0}", residentialAddress);
        }

        public void Displays()
        {
            Console.WriteLine("1.Create New Account");
            Console.WriteLine("2.Credit Account");
            Console.WriteLine("3.Withdraw");
            Console.WriteLine("4.Display Account");
            Console.WriteLine("5.Exit");
        }

        class Account : Person
        {
            private string accountNumber;
            private string accountType;
            private double balance;
            private double creditAccount;
            private double withdraw;
            private double overDraftLimit;
            private int dateAccountWasOpened;

            public Account()
            {
                balance = 0;
                overDraftLimit = 1000;
                accountNumber = "";
            }

            public void Credit(double amount)
            {
                balance += amount;
            }

            public void Withdraw(double amount)
            {
                balance -= amount;
            }

            public double GetBalance()
            {
                return balance;
            }

        }

        class Program
        {
            static void Main(string[] args)
            {
                Person person = new Person();

                Console.WriteLine();
                Console.WriteLine("********************************");
                Console.WriteLine("* MAKE YOUR SELECTION *");
                Console.WriteLine("********************************");
                person.Displays();
               
                Account account = new Account();
                               
                double amount;
                int selection;
                string selectionString;
                Console.WriteLine("Enter Selection: ");
                selectionString = Console.ReadLine();
                selection = int.Parse(selectionString);
                                                                 
                switch (selection)
                {
                    case 1: account.Read();
                        break;
                    case 2: Console.Write("Enter Amount to Pay: ");
                        amount = double.Parse(Console.ReadLine());
                        account.Credit(amount);
                        Console.WriteLine("Account Balance : {0}", account.GetBalance());
                        break;
                    case 3: Console.Write("Enter Amount to Withdraw: ");
                        amount = double.Parse(Console.ReadLine());
                        account.Withdraw(amount);
                        Console.WriteLine("Account Balance : {0}", account.GetBalance());
                        break;
                    case 4: Console.Write("Displaying Account Details: ");
                       
                        Console.WriteLine("********************************");
                        Console.WriteLine("*PERSON DETAILS*");
                        Console.WriteLine("********************************");
                        person.Display();
                        Console.ReadKey();
                        Console.WriteLine("*ACCOUNT DETAILS*");
                        Console.WriteLine("Balance before Payment: {0}",account.GetBalance());
                        account.Credit(0);
                        Console.WriteLine();
                        Console.WriteLine("Balance after Payment: {0}", account.GetBalance());
                        account.Withdraw(0);
                        Console.WriteLine();
                        Console.WriteLine("Balance after Withdrawal: {0}", account.GetBalance());
                        Console.ReadKey();
                        break;
                    case 5: Console.Write("... EXITING ");
                        break;
                    default: Console.WriteLine("Invalid input");
                        break;
                } while (selection != 5);

            }
        }
    }
}

SIMILARITY AND DIFFERENCE BETWEEN FRIEND, PACKAGE AND INTERNAL



private hides from other classes within the package. public exposes to classes outside the package. protected is a version of public restricted only to subclasses.

protected-package for the rare cases where we actually needed it, leaving protected to be equivalent to the C++ version of protected.

only the class in which it is declared can see it.

Package Private + can be seen by subclasses or package member.