Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php abstract class extending another abstract class

Tags:

php

Is it possible in PHP, that an abstract class inherits from an abstract class?

For example,

abstract class Generic {     abstract public function a();     abstract public function b(); }  abstract class MoreConcrete extends Generic {     public function a() { do_stuff(); }     abstract public function b(); // I want this not to be implemented here... }  class VeryConcrete extends MoreConcrete {     public function b() { do_stuff(); }  } 

( abstract class extends abstract class in php? does not give an answer)

like image 776
Jakub M. Avatar asked Sep 01 '11 11:09

Jakub M.


People also ask

Can abstract class extend another abstract class PHP?

And an abstract class cannot be instantiated, only extended. An abstract class can extend another abstract class.

Can abstract class extend another normal class?

I earlier learned that abstract class can extend concrete class. Though I don't see the reason for it from JAVA designers, but it is ok. I also learned that abstract class that extends concrete class can make overriden methods abstract.

Can abstract be extended?

In Java, abstract means that the class can still be extended by other classes but that it can never be instantiated (turned into an object).


2 Answers

Yes, this is possible.

If a subclass does not implements all abstract methods of the abstract superclass, it must be abstract too.

like image 197
Arnaud Le Blanc Avatar answered Sep 30 '22 10:09

Arnaud Le Blanc


It will work, even if you leave the abstract function b(); in class MoreConcrete.

But in this specific example I would transform class "Generic" into an Interface, as it has no more implementation beside the method definitions.

interface Generic {     public function a();      public function b(); }  abstract class MoreConcrete implements Generic {     public function a() { do_stuff(); }     // can be left out, as the class is defined abstract     // abstract public function b(); }  class VeryConcrete extends MoreConcrete {     // this class has to implement the method b() as it is not abstract.     public function b() { do_stuff(); } } 
like image 20
user2806167 Avatar answered Sep 30 '22 10:09

user2806167