I have an abstract parent class P, and two concrete child classes C1 and C2. C1 and C2 extend from P.
In parallel to this, I have three interfaces, iP, iC1 and iC2. iC1 and iC2 extend from iP.
Obviously each child class should implement it's respective interface, but should the abstract parent class also implement its interface too?
Given that I will only be instantiating concrete classes, and these classes are bound to iP (by virtue of the interface inheritance hierarchy), I'm not sure if I really need P to implement iP?
Another alternative would be that I scrap the inheritance hierarchy on the interface side...
The sprit of interfaces is allowing decoupling, in other words, to separate the "what" from the "how". It's not a really good practice to hava and interface per class as you are stating in your question since it has no sense.
You can have in iP the signature of all the methods that your classes are going to implement, and abstract class P that partially implements the common methods that are to be reused in your child classes and the signature of the methods that you plan to write later in your child classes.
In this way, its enough that C1 and C2 extends your abstract class P since you are forced to implement all the methods declared in your interface and in your abstract class.
Hard to answer without knowing the business model.
But if you have an abstract class, my best guess is that you don't need interfaces for C1 and C2, just have one interface which P implements and also declares it's abstract method. Then the child classes extends P.
Related
Note: it's a PHP Project
I have a situation where i use 2 API providers for my project.
They are like similar with what info they (API) provides.
I must set this right way since maybe tomorrow there will be some more APIs added.
So my project will need some methods.
Basic, here is what i have so far:
abstract class A {}// first api class, mostly contains API configs and call
abstract class B {}// second api class, also mostly contains API configs and call
//than first API has a sub classes of something like cars and trucks
class Car extends A implements ApiMethodsInterface {} // for the cars
class Truck extends A implements ApiMethodsInterface {} // for the trucks
//now second API has a sub classes for cars , trucks and spaceships
class Car extends B implements ApiMethodsInterface {} // for the cars
class Truck extends B implements ApiMethodsInterface {} // the trucks
class SpaceShip extends B implements ApiMethodsInterface {} // for the space ships
//they all have more or less similar methods
// so i used an Interface that all above classes
interface ApiMethodsInterface
//methods are
public function getModels()
public function getYears()
public function getPrice()
since every sub class implements this interface , i code by interface
Now, i have a situation, where SpaceShips has more methods to add, like getEquipment() and some more info, methods that other classes are not implementing.
Trucks also has more methods that others do not implements , like, hasTrailer(), trailerLength() etc...
My question is , what to do now, should i use Interface segregation, and add Interfaces to classes that implements those methods, and later check if object instantiated is having this method than run, else some exception, or to add missing models into the abstract classes A and B, and override methods into the classes that use those methods, or maybe there is even more good way of solving this. Quite new into Design Patterns btw...
Maybe i am overengineering but i really want to do this in a good way.
Thanks
It would likely help you to draw a diagram but we can talk through it.
It's completely normal for different implementors of an interface to have methods the others do not. The interface specifies the methods that all implementors share in common and when you code to the interface you just call those methods, not others the class may also declare. Any implementation of the iterface can be used interchangeably with any other implementation. The client doesn't care what other methods the implementor has, it is only interested in using the interface methods.
But code that is not acting as a client of the interface may view the implementation in a completely different way. Some other piece of code may construct a new SpaceShip and call getSpaceEquipment(). That's also normal. If you construct a new Truck then you can't call getSpaceEquipment() because that class does not have that method.
It would be a mistake to add methods like getSpaceEquipment() to the interface that Truck and SpaceShip both implement, say Vehicle. getSpaceEquipment() only makes sense for a SpaceShip not for all Vehicles.
You could create an extension of Vehicle called SpaceVehicle and declare the getSpaceEquipment() method in it. Then SpaceShip could implement SpaceVehicle (which makes it also a Vehicle by inheritance). But you would only do that if you were going to have several implementors of SpaceVehicle and you needed to write code that works with SpaceVehicle instances but does not care whether the SpaceVehicle is a SpaceShip or a Satelite or whatever.
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
There are classes A and B both extend class C and there are some common methods in class C that are needed for both class A and B.
There are cases that it needs to invoke the method in class B from class A, which is at the moment achieved by creating an object of class B in class A. But, more and more methods are added into class C, so it is inefficient to create an object of class B inside class A due to the duplication.
My question's that what are the possible solutions to refactoring the classes so that they have better structure, e.g using object oriented, factory pattern, etc.?
Thanks in advance.
You could use Traits (only PHP >= 5.4.0).
If the methods needed by A and B can be factored out into a single responsibility then you might consider adding class D which is then used by class A and B. The Single Responsibility Principal (SRP) is the S in SOLID, which are guidelines for object oriented design.
You can find plenty of information about SOLID in general, and the application of SRP in PHP on Google. I gave a presentation to our local PHP Meetup Group on SRP which can be found online here: SRP Presentation
From a quick Google search and a the wikipedia article on Multiple Inheritance, which quotes:
Multiple inheritance refers to a feature of some object-oriented programming languages in which a class can inherit behaviors and features from more than one superclass. This contrasts with single inheritance, where a class may inherit from at most one superclass.
I understand that PHP doesn't allow multiple inheritance. What I can't find a clear answer on, however, is whether it allows more than one class to extend a superclass. Example:
class a {}
class b extends a {}
class c extends a {}
In terms of what I'm trying to do, I'm making an RPG and want a 'generic' character class to include all the methods and properties that make the template of a character. Then I want classes to include specifics for each type of character (warrior, mage, etc), such as stats modifiers and special attacks.
Is this possible?
Yes, it's perfectly possible. The entire purpose of inheritance is that multiple children can inherit common functionality from a parent.
Yes, multiple classes can extend the same class, per your code example.
This is the foundation of OOP. It's possible.
Yes, any class can extend a base class. In your example, it just isn't possible for class c to extend both a and b.
So in an adjusted example like the one above
class a {}
class b extends a {}
class c extends b {}
c could not get to properties of a ?