Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the purpose of abstract classes?

I am trying to learn OOP in PHP, and I have some confusion about interfaces and abstract classes. They both contain no implementations, only definitions, and should be implemented through their sub-classes. What part of abstract classes clearly distinguishes them from interfaces? Also, due to their apparent similarities, based on what reasons should I decide to use one over the other?

like image 362
SpikETidE Avatar asked Feb 18 '10 12:02

SpikETidE


People also ask

What is purpose of abstract classes Mcq?

Explanation: The abstract classes can be used to create a generic, extensible class library that can be used by other programmers.

What is the purpose of abstract classes in Java?

Java Abstract class is used to provide common method implementation to all the subclasses or to provide default implementation. We can run abstract class in java like any other class if it has main() method.


4 Answers

It is possible to create abstract classes that contain concrete members, such as methods or properties. You still can't instantiate the abstract class directly, but any instantiated sub-classes can benefit from implemented members defined in the abstract class.

An interface, by comparison, never contains any implementation logic. It is down to each implementing class to provide the implementation of all members defined in the interface.

In terms of how I view the differences, a sub-class of an abstract is-a class of that type. e.g. Dog is an Animal. I see an interface as a does-a relationship. e.g. ICanDisplayImages tells me that the implementing class can display images, but tells me nothing about what the class actually represents.

like image 97
Paul Alan Taylor Avatar answered Oct 19 '22 07:10

Paul Alan Taylor


An abstract class forms an is-a relationship between itself and the subclass, while an interface creates a follows-a relationship. As such, abstract classes are much more concrete than interfaces and they may also contain concrete implementations (e.g. Template methods) while an interface defines a contractual set of methods an implementing class must follow. This is a much higher level of abstraction as the implementing class must not necessarily be of the abstract class. Use it to standardize your API.

Related questions: https://stackoverflow.com/search?q=abstract+vs+interface

like image 34
Gordon Avatar answered Oct 19 '22 08:10

Gordon


Beyond the OOP philosophy described in other answers, I think the major use of abstract class is kind of a skeleton.

It's usefull when you design an application, or work with a team, because you have a base code to work with and extends it's close to interface but with a more advanced workflow.

like image 3
Boris Guéry Avatar answered Oct 19 '22 09:10

Boris Guéry


They both contain no implementations..

Abstract class can implements all or only part of the methods. But the main philosophy - is extend existing abstract class by to add a new methods in child classes (that extend a basic functionality).

In a child class you can extend only one class (abstract class). Abstract class define functionality that you must implement or just extends by other methods in child classes.

Interfaces are use to define the class behaviors. You can implement more than one interface(and say my child-class must do this,this and this thigs!), but you can extend only one abstract class.

like image 3
SirJ Avatar answered Oct 19 '22 07:10

SirJ