我们可以在抽象类中创建一个final方法吗?
我们可以在最终类中创建一个抽象方法吗?
请给我解释编码。
感谢您的回答。
答案 0 :(得分:1)
1) can we create the final method in abstract class .
yes we can .
see the below code for further reference /* final method in abstract class */
abstract class base {
public $name;
abstract public function getName();
final public function setName($inputName) {
$this->name = $inputName;
}
}
class child extends base {
public function getName() {
echo $this->name;
}
}
$obj = new child();
$obj->setName('Software Engineer');
$obj->getName();
2 . can we create a abstract method in final class ? No. we can not create the abstract method in final class,
答案 1 :(得分:1)
否不这样做。
class PersonalLoan{
public final String getName(){
return "personal loan";
}
}
class CheapPersonalLoan extends PersonalLoan{
@Override
public final String getName(){
return "cheap personal loan"; //compilation error: overridden method is final
}
}
但是抽象类有抽象方法。必须在继承函数中定义抽象方法,所以它不是
答案 2 :(得分:0)