File tree 1 file changed +39
-2
lines changed
1 file changed +39
-2
lines changed Original file line number Diff line number Diff line change 1
1
# Abstraction
2
+ Abstraction is a way to hiding details implementation and exposing only the essential features of the class to the outside world.
2
3
3
- * Abstraction is hiding the internal implementation.
4
+ # Example
4
5
5
- # interface vs abstact class
6
+ ``` php
7
+
8
+ abstract class Car {
9
+ // Abstract classes can have properties
10
+ protected $tankVolume;
11
+
12
+ // Abstract classes can have non abstract methods
13
+ public function setTankVolume($volume)
14
+ {
15
+ $this -> tankVolume = $volume;
16
+ }
17
+
18
+ // Abstract method
19
+ abstract public function calcNumMilesOnFullTank();
20
+ }
21
+
22
+
23
+ class Toyota extends Car {
24
+ // Since we inherited abstract method, we need to define it in the child class,
25
+ // by adding code to the method's body.
26
+ public function calcNumMilesOnFullTank()
27
+ {
28
+ return $miles = $this -> tankVolume*33;
29
+ }
30
+
31
+ public function getColor()
32
+ {
33
+ return "beige";
34
+ }
35
+ }
36
+
37
+ $toyota1 = new Toyota();
38
+ $toyota1 -> setTankVolume(10);
39
+ echo $toyota1 -> calcNumMilesOnFullTank();//330
40
+ echo $toyota1 -> getColor();//beige
41
+ ```
42
+ # Difference between interface and abstraction.
6
43
7
44
* https://codeinphp.github.io/post/abstract-class-vs-interface/
8
45
You can’t perform that action at this time.
0 commit comments