-1

I tried this Swift code in XCode.

File1.swift(Non-Main File):


class A {
    fileprivate func myFun() {
        print("A myFun")
    }
}

class B: A {
    override internal func myFun() {
        super.myFun()
        print("B myFun")
    }
}

To note:

  1. 'A' does not provide access to 'myFun' to an object made outside of File1.
  2. 'B' subclasses A and overrides 'myFun' with an internal access specifier.

main.swift

let s = B()
s.myFun()

Output:

A myFun

B myFun

Program ended with exit code: 0

To note:

  1. I used a subclass(B) to access a part of the superclass(A) which it didn't allow to be accessed.

Is this behavior intended? If yes then how is this useful?

Deveesh
  • 119
  • 2

1 Answers1

0

You can find an answer Here.

In short - fileprivate != private while A and B are in the same file.

Denis Kovzan
  • 118
  • 4