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?
Related
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.
In Doctrine, assuming I want to implement different types of vehicles. Maybe a car, a plane, a bicycle and so on ... all these vehicles have many different properties but also very common things like they are all made of a material.
So if i want to get this material ... should i implement a abstract class Vehicle with a property material and implement getter/setter or should i define a interface IVehicle and define the getter/setter in there to be sure there is really a method for getting and setting the material? Or should i even use both in combination?
It feels "professional" using the interface ... but it feels wrong to define getter/setters in interfaces, so my personal feeling is:
Don't use an interface, just use the abstract class.
But is the abstract class approach protected against misuse? For example on another place i definitely expect a Material type returning from the getMaterial function ... is the abstract class approach also save for not returning complete unexpected things (like the interface does)?
So if i extend this vehicle for another concrete class, a developer should not be able to return unexpected things, but should also be able to change the logic in the particular method if needed.
My 2 cents:
You can write an abstract class with methods that return null (or some other predetermined value) that would require you to implement them in every derived class.
This approach would still require you to write some boilerplate code to check against your conditions in every class.
I would personally go with an interface here as the nature of your classes seem to be quite diverse and can only be partially generalized to a single abstract class. However, I don't see anything wrong with going the abstract class way either. In this case, the devil is in the details.
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.
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