Skip to content

Commit 4479f6d

Browse files
committed
abstraction
1 parent bb45114 commit 4479f6d

File tree

1 file changed

+39
-2
lines changed

1 file changed

+39
-2
lines changed

abstraction/readme.md

+39-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,45 @@
11
# Abstraction
2+
Abstraction is a way to hiding details implementation and exposing only the essential features of the class to the outside world.
23

3-
* Abstraction is hiding the internal implementation.
4+
# Example
45

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.
643

744
* https://codeinphp.github.io/post/abstract-class-vs-interface/
845

0 commit comments

Comments
 (0)