What is difference between Abstract method in abstract class and Interface method in interface PHP ?
Note: I am not asking difference between Abstract class and Interface. I am asking only methods.
The following is my understanding of why they exist and how they can be useful. The are the same in general. They exist to give flexibility for better OOP design in my mind.
Interface methods must be implemented by any class implementing the Interface.
The same way abstract methods must be implemented by any class that extends the abstract class.
A class can implement multiple Interfaces but can only extend one class (abstract or concrete).
It is possible to have multiple implementation of an Interface with
different behaviors.
It is also possible to implement an Interface in abstract. That is, providing boiler plate for implementation of an Interface with most common methods implemented (at least with default behavior) and leave the implementation of the rest of the Interface methods to concrete class. This abstract implementation though can force the concrete class to implement some abstract methods (on top of Interface methods). These abstract methods may not necessarily be required for other implementations of the same Interface and should not be enforced by the Interface so they should not be in the Interface.
Related
I know the concepts of Abstract Class and Interface Class.
But I want real time example which explains both the concept in our design.
Can anyone help with this?
Both have little different scenerios...
Abstract classes also contains method definition as you know interface does not. But if you defines abstract class you can not inherit more than one classes from child class. But In case of interface you can do that and implement the methods declared inside the interface.
These scenerios are used in the software development as we just declared the signature of the method and then user can write his.her own code inside the method by overriding it.
Here I am mentioning some point what we can do with abstract class and interface
In abstract class you can define abstract methods which should be public or protected. but in Interface you can define public abstract method only.
In abstract class you can define data members and constants but in interface you can define constants only.
In abstract class you can define body of method and you can inherit in sub class, but in interface you can not define body of method.
you can implement multiple interface in a sub class, but you can not inherit multiple abstract classes in a sub class.
Example
In Major Frameworks, uses interfaces for different types of data source class. If we want to make data source class for new introduced database then we must have to implement their interface in our class so it make well maintained code and methods parameters.
I know this is very repetitive question i also read most of the post but not found the satisfactory answers. I know the bookish difference which i read from most of the posts.
Can any one please tell me
why we use abstract class and interface?
when we use this one?
i know that when we have some repeated task but implementing in a different way each time then we use abstract class.
i think we can implement this by a normal class and subclass using overriding then why we use abstract? also their is a difference of access specifier between abstract class and interface as well as the compulsion of implementation in interface.
Interface is quite acceptable that when we want to force to implement all method in that situation we use interface but why abstract class? Is compulsion of implementation is just a difference?
thanks in advance.
Beautifully explained here - https://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface
In a nutshell,
Interfaces are used to define the peripheral abilities of a class. In other words both Human and Vehicle can inherit from a IMovable interface.
An abstract class defines the core identity of a class and there it is used for objects of the same type.
Also take a look at https://msdn.microsoft.com/en-us/library/scsyfw1d(v=vs.71).aspx
Here are some recommendations to help you to decide whether to use an interface or an abstract class to provide polymorphism for your components.
If you anticipate creating multiple versions of your component, create an abstract class. Abstract classes provide a simple and easy way to version your components. By updating the base class, all inheriting classes are automatically updated with the change. Interfaces, on the other hand, cannot be changed once created. If a new version of an interface is required, you must create a whole new interface.
If the functionality you are creating will be useful across a wide range of disparate objects, use an interface. Abstract classes should be used primarily for objects that are closely related, whereas interfaces are best suited for providing common functionality to unrelated classes.
If you are designing small, concise bits of functionality, use interfaces. If you are designing large functional units, use an abstract class.
If you want to provide common, implemented functionality among all implementations of your component, use an abstract class. Abstract classes allow you to partially implement your class, whereas interfaces contain no implementation for any members.
Consider this.
An interface has no implementation. It forces prototypes onto a class but does not help implementing them.
An interface is not tied to a class hierarchy. You can apply the same interface to multiple, entirely different class trees. This is important and one of the things that an interface offers over an abstract class. An interface is implementation independent, you can "slap it onto" any class. This is the most powerful aspect of an interface.
I am quite unsure how to implement the following situation.
ActiveRecord Base class which represents the table in database
Subclasses of that Base class where I want to enforce the implementation of some functions
Implementing an Interface in the subclasses does not feel right and maybe I want to use inheritance.
I would like to use an abstract class for the base class, but I cannot instantiate that class, so I would need another concrete class between the two layers just to make database operations (AR).
What would be the best way?
Abstract class abc{
---
}
I am new to OOP.I want to lean the object oriented programming concept. There is a confusion between abstract classes and interfaces. Where do we have to use this and which condition we have to you abstract and interface class. Please suggest me tutorial links so that i can easily get the functionality. Suppose we are going to drink water because i need it. This is the situation when i need water so i take water. In same case abstract and interface is needed.
An abstract class is a special kind of class that cannot be
instantiated. So the question is why we need a class that cannot be
instantiated? An abstract class is only to be sub-classed (inherited
from).
In other words, it only allows other classes to inherit from it but cannot be instantiated. The advantage is that it enforces certain hierarchies for all the subclasses. In simple words, it is a kind of contract that forces all the subclasses to carry on the same hierarchies or standards.
An interface is not a class. It is an entity that is defined by the
word Interface. An interface has no implementation; it only has the
signature or in other words, just the definition of the methods
without the body.
As one of the similarities to Abstract class, it is a contract that is used to define hierarchies for all subclasses or it defines specific set of methods and their arguments. The main difference between them is that a class can implement more than one interface but can only inherit from one abstract class. Since C# doesn’t support multiple inheritance, interfaces are used to implement multiple inheritance.
Read more - http://www.codeproject.com/Articles/11155/Abstract-Class-versus-Interface
I have a number of situations where I have some BaseInterface that gets implemented by a bunch of objects. These objects also typically should implement interfaces such as Serializable or Counatble. Now it's unclear to me on when to have these additional interfaces be specified as being implemented by the classes implementing BaseInterface or when it's better to have BaseInterface extend these additional interfaces. I am looking for an overview of the implications of either approach, and a set of simple rules by which one can determine which approach should be taken in a given situation.
To increase clarity, I provide a simple example in code of both approaches:
Approach 1: Extend the base interface
interface BaseInterface extends Countable, Serializable {}
class Implementation implements BaseInterface{}
Approach 2: Implement the individual interfaces in the implementations
interface BaseInterface {}
class Implementation implements BaseInterface, Countable, Serializable {}
It seems that often one want to be able to assume something implementing BaseInterface is Countable, though OTOH this might not be needed and hinder implementation of BaseInterface where implementing Countable is simply not needed. Then there is also the interface segregation principle, which makes me wary of extending interfaces as in approach 1.
Note: This question is on where to put the interface implementation clause. It is not about code sharing via inheritance.
I propose a solution:
interface BaseInterface {}
abstract class AbstractBase implements BaseInterface {}
abstract class AbstractBaseCountable implements BaseInterface, Countable {}
abstract class AbstractBaseCountableSerializable implements BaseInterface, Countable, Serializable {}
class Implementation extends AbstractBase/* OR AbstractBaseCountable
OR AbstractBaseCountableSerializable */ {}
Thus, it may create classes with each of the different desired characteristics. Moreover, in certain implementations are abstract classes, so that the implementation classes are cleaner.