---
title: "Visibility symbols +, -, # in classes"
subject: "Object Oriented Analysis and Design"
chapter: 3
key: "semester_6/object_oriented_analysis_and_design:visibility-signs-class-diagram"
source: https://www.bctengineer.com/q/semester_6/object_oriented_analysis_and_design/visibility-signs-class-diagram
site: BCT Engineering
---

# Visibility symbols +, -, # in classes

Object Oriented Analysis and Design · Chapter 3: Object Oriented Design

- Asked in: 2071 Magh, 2+4 marks
- Video: [Lecture 15: Class Visibility](https://www.youtube.com/watch?v=ltM3JIuLDvU) — Multimedia Class
- Page: https://www.bctengineer.com/q/semester_6/object_oriented_analysis_and_design/visibility-signs-class-diagram
- Whole chapter: https://www.bctengineer.com/md/semester_6/object_oriented_analysis_and_design/chapter-3

## Question

You will find - (minus signs), + (plus signs), # (hash marks) in front of operation names and attributes in UML class diagrams. Why they are used for? With the help of any one of the object oriented programming language syntax implement the different semantics of +, -, # sign used in implementation of Object Oriented System.

## Short answer

UML visibility signs (+ public, - private, # protected, ~ package) enforce encapsulation by controlling member access. In Java, private members are class-only, protected allows subclass and package access, public is universal. Example: Account has private balance, protected accountNo, public deposit/getBalance, protected audit; subclass can access accountNo and audit but not balance.

## Answer

#### Why the signs are used
In a UML class diagram the symbols in front of an attribute or operation show its **visibility** (access level): who is allowed to use that member.

| Sign | Visibility | Meaning |
|---|---|---|
| `+` | public | Accessible from any class |
| `-` | private | Accessible only inside the same class |
| `#` | protected | Accessible in the class and its subclasses (and the package in Java) |
| `~` | package | Accessible in the same package |

They support **encapsulation**: data is kept hidden (`-`), and only the operations that form the class interface are public (`+`).

```text
 +----------------------+
 |       Account        |
 |----------------------|
 | - balance : double   |
 | # accountNo : String |
 |----------------------|
 | + deposit(amt)       |
 | + getBalance()       |
 | # audit()            |
 +----------------------+
```

#### Implementation in Java

```java
class Account {
    private double balance;          // - : only Account
    protected String accountNo;      // # : Account, subclasses, package
    public void deposit(double amt) { // + : everyone
        balance += amt;
        audit();
    }
    public double getBalance() { return balance; }
    protected void audit() { System.out.println("audit " + accountNo); }
}

class SavingAccount extends Account {
    void test() {
        accountNo = "S1";           // OK  : protected is inherited
        audit();                    // OK
        // balance = 5;             // ERROR: private to Account
    }
}

class Main {
    public static void main(String[] a) {
        Account acc = new Account();
        acc.deposit(500);                 // OK  : public
        System.out.println(acc.getBalance());
        // acc.balance = 1000000;         // ERROR: private
        // acc.audit();  // not allowed from a different package
    }
}
```

**Semantics shown:** `+` members are the interface of the object, `-` members are hidden implementation, and `#` members are available only to subclasses and package classes so that inheritance can extend the class without exposing it to everybody.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. What do +, - and # before a member in a UML class diagram show?
   A. Data type
   B. Visibility (access level)
   C. Multiplicity
   D. Order of execution
   Answer: B. They show public (+), private (-) and protected (#) visibility. (Key point: Meaning of signs)

2. Who can access a # (protected) member in the Java example?
   A. Only the same class
   B. Any class anywhere
   C. The class, its subclasses and the same package
   D. Only other packages
   Answer: C. Protected members are inherited by subclasses and are also visible in the package. (Key point: Protected in Java)

3. In the example, why is 'balance = 5;' an error inside SavingAccount?
   A. balance is protected
   B. balance is private to Account
   C. balance is static
   D. SavingAccount is abstract
   Answer: B. A private member is accessible only inside its own class, not in subclasses. (Key point: Private member access)

## Related in Object Oriented Analysis and Design

- [Create classes from design class diagrams](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aclasses-from-design-class-diagram) — Object Oriented Analysis and Design, chapter 4, asked 2×
- [Singleton class and visibility types](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Asingleton-and-visibility-types) — Object Oriented Analysis and Design, chapter 3
- [Object oriented system and core concepts](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aoo-system-concepts-explained) — Object Oriented Analysis and Design, chapter 1
- [Algorithmic vs object-oriented decomposition](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aalgorithmic-vs-oo-decomposition) — Object Oriented Analysis and Design, chapter 1
- [XYZ Marina class diagram](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Amarina-class-diagram) — Object Oriented Analysis and Design, chapter 2
- [One-to-many relationship using collections](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aone-to-many-collection-generic-classes) — Object Oriented Analysis and Design, chapter 4

## Related in other subjects

- [Compare procedural and object oriented programming](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprocedural-vs-oop-compare) — Object Oriented Programming, chapter 1
- [Vector class addition by object argument](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avector-class-addition) — Object Oriented Programming, chapter 1
- [SD of marks; effect of adding one](https://www.bctengineer.com/question.md?key=semester_5%2Fprobability_and_statistics%3Asd-marks-increase-by-one) — Probability and Statistics, chapter 1
- [Standard and ladder programming block diagrams](https://www.bctengineer.com/question.md?key=semester_6%2Fdigital_control_system%3Afilter-standard-ladder-block) — Digital Control System, chapter 4
