TraitsTraits are similar to interfaces in Java and are created using trait keyword. Show
Abstract ClassAbstract Class is similar to abstract classes in Java and are created using abstract keyword. ExampleLive Demo Following is the program in Scala to show the usage of Traits and Abstract Classes. trait SampleTrait { // Abstract method def test // Non-Abstract method def tutorials() { println("Traits tutorials") } } abstract class SampleAbstractClass { // Abstract method def test // Non-abstract meythod def tutorials() { println("Abstract Class tutorial") } } class Tester extends SampleAbstractClass { def test() { println("Welcome to Tutorialspoint") } } class TraitTester extends SampleTrait { def test() { println("Welcome to Tutorialspoint") } } object HelloWorld { // Main method def main(args: Array[String]) { var obj = new Tester() obj.tutorials() obj.test() var obj1 = new TraitTester() obj1.tutorials() obj1.test() } } OutputAbstract Class tutorial Welcome to Tutorialspoint Traits tutorials Welcome to Tutorialspoint Following are some of the important differences between Traits and Abstract Classes in Scala.
Updated on 16-May-2020 11:33:34
By DevsrealmGuy | On 20 NOV, 2020 So far, we've covered OOP basics in PHP, and Static Methods, & Properties, in this guide, we would cover abstract classes in PHP. Just like I explained in the OOP basics, I said a class describes the concept of
creating an Object, there are not necessarily the objects themselves, but as a code template used to create or generate one or more objects. Look at the point, a class is a code template used to create or generate one or more objects. Abstract class shares that ideology, it is a code template for other classes, it is not a code template for objects but for classes. Which means, it cannot be instantiated but defines the interface for any class that might extend it, like
you would point to it only through a reference to the subclass. You use an abstract class when you want to create a parent class that houses related commonality or functionality of 2 or more classes, and when you don't want the parent class or the base class to be instantiated. Abstract classes contain one or more abstract methods. Abstract methods are used to perform a specific task, normally, in a normal class or concrete class, you'll implement the method, however, an abstract
method is only declared with no implementation, the implementation would have to be carried out in the class that is extending it. To better understand abstract classes, let's think of a good example that would be a good candidate for abstractiveness...yeah...I got one... Suppose, we are building a class around genres of music, and you know different styles of music have somewhat similar characteristics or let me say elements, something like melody, harmony, vocal lyrics, and maybe
percussiveness. For sure, not all music would have the same element, but instead of building different classes for a number of elements, we make an abstract class and define all the common ones, and leave the one that would be different out of it, for example not all genre of music uses percussion, but most of all music has some melodies and harmonies. The abstract class would only do something like:
We would see a practical example in a moment, but let's see one more example, so, you would get the idea fully. Another good example is of a car, as you know, a car has common attributes such as color, size, engine, price, model, etc. So, for every car, this attribute would be common but might be implemented differently depending on the type of car, but the idea here is that abstract class would prepare the general ones for us, which you must implement, but the operation might be different from one class to the other Practical Examples of Creating an Abstract ClassThe following defines an abstract method for the car attributes:
The abstract keyword is used to denote both an abstract method and an abstract class. When creating an abstract method, you ensure that implementation will be available in all concrete child classes, but you leave the details of that implementation to the subclasses that would be implementing. Also, methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private. Now, any car that wants to be instantiated (e.g Bentley, Acura, BMW) must implement all abstract methods.
I can also do the same for BMW or Toyota, here is where it gets interesting, if for some reason the abstract class doesn't have a common method between the cars, I can go ahead to add the custom feature myself:
You might be wondering if you can have non abstract methods inside an abstract class, and the answer is yes, you can, in fact, you can have properties in the non abstract method, and the properties doesn't have to be abstract. So, to get a result, I would instantiate the child class, so, instead of creating a getter method in the child class, I would declare an abstract method since I know it would be common for all my child classes, this would be the end result:
The constructor method is invoked when an object is created. You can use it to prepare the essential properties, and save the hassle of using -> whenever you need to set a new property value for a new object. Any arguments supplied here: $car_acura = new Acura( "Tiger Eye Pearl", "Type S", "3.0-liter single turbo", 30000 ); are passed to the constructor, I am passing the color, the type, engine, and price to the constructor, you don’t have to use the property name as the name of the constructor argument, you can use something meaningful. So, in our example, I am passing the arguments supplied above here: public function __construct( string $color, string $type, string $engine, int $price ) { $this->color = $color; $this->type = $type; $this->engine = $engine; $this->price = $price; } In my abstract definition you can see I am using a type hint: abstract protected function car_color() : string; abstract protected function car_engine() : string; abstract protected function car_model() : string; abstract protected function car_price() : int; abstract protected function getresult() : string; We are using this to specify the expected type, most of the methods should return a string type, with the exception of the car_price method that is expecting an integer. If the abstract class uses type hinting, the child class should use the same. Which is why, I have: protected function car_color() : string { // TODO: Implement car_color() method. return $this ->color; } protected function car_model() : string { // TODO: Implement car_model() method. return $this -> type; } ... ... ... The return $this is used to return whatever is passed into the properties, and we then created a getter method to display out the result: public function getresult() : string { return "Car Color: " . $this->color . " \n" . "Car Type: " . $this->type . " \n" . "Car Engine: " . $this->engine . "\n" . "Car Price: " . $this->price; } and printing the method gives us: print $car_acura->getresult(); // Output => # Car Color: Tiger Eye Pearl # Car Type: Type S # Car Engine: 3.0-liter single turbo # Car Price: 30000 Bottom line is to use an abstract class when you have some commonalities in your sub-classes, and you want to implement them differently based on the type of the objects. InterfacesAbstract classes let you create templates for subclasses, leaving the implementation method to the concrete subclasses, an interface can only define the functionality, with no implementation. Sounds about the same right? Yes, it is very similar with little differences, we would get to that in a moment, the following is an interface:
As you can see, Interfaces are defined in the same way as a class, but with the interface keyword replacing the class keyword. Any class that incorporates this interface commits to implementing all the methods it defines, or it must be declared abstract. Unlike, an abstract class that can have a public, protected, or private method, interface methods must be declared public, that is one difference between abstract and interface classes. A class can implement an interface using the implements keyword in its declaration. The process of implementing an interface is the same as extending an abstract class that contains only the abstract keyword. Practical Examples of Creating InterfaceHere is an example:
As you can see it is no much different from an abstract class, and yes, they both provide some level of abstractions, the following are the key differences:
Also, a class can both extend a superclass and implement any number of interfaces. The extends clause should precede the implements clause like so:
As you can see, the Acura class extends the car class, and implement multiple interfaces in a comma-separated list. Note that. PHP only supports inheritance from a single parent, so the extends keyword can precede a single class name only. TraitsI pointed out above that PHP only supports inheritance from a single parent, but what if you want to share an implementation across inheritance hierarchies? and that is where traits come into play. Traits are used for implementing functionality similar to multiple inheritances. It is similar to abstract class and inheritance in that it cannot itself be instantiated but can be incorporated into classes. Any methods defined in a trait become available as part of any class that uses it. A trait changes the structure of a class but doesn’t change its type. If you want to avoid copy and pasting code from one class to another, then you might want to use a trait to reduce that. Here is how you would use a trait, they are declared using the Trait keyword like so:
Once, you've implemented your trait, you can then incorparte it into your classes like so:
You can include multiple traits in a class by listing each one after the use keyword, separated by commas. For example:
A code example:
If there is other classes that needs to use the method in my trait, I can simply use the trait greeting in those classes, by simply doing: Use greeting; This reduces code duplication as there's no need to redeclare the same method several times, it is as easy as that. If you know you won't be duplicating code, then you don't need it. I like to think of it this way, would I be copying codes from one class to multiple classes, if my answer is yes, then I might consider using trait. What is the difference between a trait and an abstract class in PHP?Trait supports multiple inheritance. Abstract Class supports single inheritance only. Trait can be added to an object instance. Abstract class cannot be added to an object instance.
What is the difference between a trait and an abstract class?Traits does not contain constructor parameters. Abstract class contain constructor parameters. Traits are completely interoperable only when they do not contain any implementation code. Abstract class are completely interoperable with Java code.
When should I use trait in PHP?In PHP, a trait is a way to enable developers to reuse methods of independent classes that exist in different inheritance hierarchies. Simply put, traits allow you to create desirable methods in a class setting, using the trait keyword. You can then inherit this class through the use keyword.
Is PHP trait good?PHP Traits are Good
But multiple inheritance isn't possible in PHP. Interfaces are instead offered as a substitute, and an unsuccessful one at that. Interfaces are not intended to be used in this way, rather to act as contracts which force any classes that implement it to deliver its functionality.
|