Final method:
A final method is a method that cannot be overridden.
To declare a method as final, you need to prefix the function name with the ‘final’ keyword.
eg: class Teacher {
final public function attendance() {
echo "Teacher attendance called";
}
}
class Student extends Teacher {
//this will cause Compile error
public function attendance() {
echo "Student attendance called";
}
}
$st = new Student();
$st->attendance();
Results in Fatal error: Cannot override final method Teacher::attendance()
final class:
A final class is a class that cannot be extended. To declare a class as final, you need to prefix the ‘class’ keyword with ‘final’. Example below.
final class Teacher {
public function attendance() {
echo "Teacher attendance called";
}
}
//this will cause Compile error
class Student extends Teacher {
}
$c = new Student();
$c->attendance();
Results in Fatal error: Class ChildClass may not inherit from final class (Teacher)
A final method is a method that cannot be overridden.
To declare a method as final, you need to prefix the function name with the ‘final’ keyword.
eg: class Teacher {
final public function attendance() {
echo "Teacher attendance called";
}
}
class Student extends Teacher {
//this will cause Compile error
public function attendance() {
echo "Student attendance called";
}
}
$st = new Student();
$st->attendance();
Results in Fatal error: Cannot override final method Teacher::attendance()
final class:
A final class is a class that cannot be extended. To declare a class as final, you need to prefix the ‘class’ keyword with ‘final’. Example below.
final class Teacher {
public function attendance() {
echo "Teacher attendance called";
}
}
//this will cause Compile error
class Student extends Teacher {
}
$c = new Student();
$c->attendance();
Results in Fatal error: Class ChildClass may not inherit from final class (Teacher)
No comments:
Post a Comment