I was trying out some OOP programming in PHP and when I instantiated a class, it generated an error saying that that class needs to be abstract.
When does a class need to be abstract? And how can abstract classes be called? Thanks
I can't think of a situation where you would get an error that a class needs to be abstract. Its more likely that you got an error when trying to instantiate an abstract class. Something along the lines of "Cannot create instance of an abstract class".
Abstract classes cannot not be instantiated directly. Instead they are extended by a child class (a concrete class) that can be instantiated. So an abstract class is like a template that provides some common functionality for other sub-classes. Usually it includes some abstract methods that the child classes must implement.
Related
In real world project, we had a pretty long PHP class, and during refactoring it was broken into 2 smaller classes, ClassA and ClassB, where ClassA extends ClassB (just an example names). There were some issues with legacy code, so we had to do that way.
In ClassB we call some methods from ClassA (they're not overriden, I mean, methods doesn't exists in ClassB). Is that a bad idea in OOP?
Edit: obviosly, in the rest of code we always initiate only ClassA. But wondering is calling parent class from the child a big "NO" in OOP terms?
What you're describing is a common pattern in OOP. The parent class should be an abstract class, so it can't be instantiated by itself, only the child classes can be instantiated.
The child classes are all required to implement methods that aren't in the parent class, but some methods are common to all the classes, and they're implemented in the parent. These common methods then defer to the children for some implementation aspects that are child-specific.
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.
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 searched for hours of the advantages of using a abstract class over a non-abstract class in php. I know that the abstract class can not be instantiated which is a good feature of singleton design pattern. But my point is, since they both can serve as a base class, is there any other reason to use an abstract class? All of the answers like this one What are the advantage if i use abstract class in php? I found didn't mention this, they just answered another question, namely, what is the advantage of using a base class?
So, my question is that is there any reason to use an abstract base class other than a normal base class except for that it cannot be instantiated?
An abstract class cannot be directly instantiated, but it can contain both abstract and non-abstract methods.
If you extend an abstract class, you have to either implement all its abstract functions, or make the subclass abstract.
You cannot override a regular method and make it abstract, but you must (eventually) override all abstract methods and make them non-abstract.
Abstract keywords are used to label classes or methods as patterns. It's similar to interfaces but can contain variables and implementations of methods.
There are a lot of misunderstandings concerning abstract classes. Here is an example of an abstract Dog class. If a developer wants to create some basic Dog class for other developers or for himself to extend he declares the class as abstract. You can't instantiate the Dog class directly (nobody can), but you can extend Dog by your own class. SmartDog extends Dog etc.
All methods that are declared to be abstract by the Dog class must be implemented manually in each class that extends Dog.
For example, the abstract class Dog has an abstract method Dog::Bark(). But all Dogs bark differently. So in each Dog-subclasses you must describe HOW that dog barks concretely, so you must define eg SmartDog::Bark().
My problem has to do with PHP, but it could potentially relate to any other language with an object relational mapper.
Say I have a class Event that extends DbObject and Exception that extends DbObject but I want them both to extend from Abstract Class TimePeriod. Both objects being ORM objects.
Why? Because I don't want to manually code getStartTimeStamp() on each type of class that has a start and end date.
Right now I am using interfaces but I feel like it really hurts the DRY of it.
Do I simply redesign it such that Abstract Class TimePeriod extends Dbobject and implement each object extend from it? Or is there a better solution?
The general solution to the multi-inheritance problem you are facing is aggregation/composition.
It also makes your design clearer. Exception can't be a time period after all.
So what you want is to create a TimePeriod class and pass objects of that class to Exception and Event.
Then you can either call getStartTimeStamp() on the time period object directly like that:
$event->getTimePeriod()->getStartTimeStamp();
Or create wrapping methods and calling them instead.