Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding access to a parent's property in the subclass

Tags:

oop

ios

swift

I feel like this should be relatively simple but I can't find a way to accomplish it.

Let say I have

class Parent {
   public var file: PFFile?
}

and a subclass

class Child : Parent {
    // some functionality that hides access to super.file
}

Problem is I can't mess with the Parent class, but I don't want anyone using the Child class to have access to 'file'. How can I accomplish this in Swift?

like image 844
Eric Smith Avatar asked Nov 03 '15 06:11

Eric Smith


1 Answers

Perhaps this one fix it:

class Parent {
   public var file: PFFile?
}

class RestrictedParent : Parent {
   private override var file: PFFile?
}

class Child : RestrictedParent {
    // some functionality that hides access to super.file
}

Here in RestrictedParent, we can hide any functionality that should not be visible to any of the child class inheriting to it.

EDIT:

A part from doc:

class Car: Vehicle {
    var gear = 1
    override var description: String {
        return super.description + " in gear \(gear)"
    }
}
like image 179
NeverHopeless Avatar answered Oct 21 '22 05:10

NeverHopeless