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.
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.
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.
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 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().