SNG
SNG

Reputation: 357

Preventing super class function overriding in subclass

Is their any way to prevent function overriding of a super class in subclass. if yes please let me know. thanks in advance.

Upvotes: 0

Views: 35

Answers (2)

apokryfos
apokryfos

Reputation: 40730

You can use the final keyword:

Examples:

class CanBeInherited {
    public final function cantBeInherited() {
         ...
    }
}

final class CannotBeInherited {
    public function cantBeInheritedEither() {
         ...
    }
}

class Child extends CanBeInherited { } //This is fine
class Child2 extends CanBeInherited { 
    public function cantBeInherited() {} //Error
}
class Child3 extends CannotBeInherited {} //Error

Upvotes: 1

Mohammed Ahmed
Mohammed Ahmed

Reputation: 455

declare function in this way in super class

class Foo
{
    final public function bar()
    {
        //Do action A
    }
}

Upvotes: 1

Related Questions