Skip to content

Commit a726c37

Browse files
committed
inheritance
1 parent a983d1a commit a726c37

File tree

1 file changed

+34
-20
lines changed

1 file changed

+34
-20
lines changed

Inheritance/readme.md

Lines changed: 34 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,50 @@
11
# Inheritance
2+
Inheritance is a fundamental concept of OOP, a class can inherit propertis(data) and behavior (method) from a parent class.
23

3-
Inheritance is one of the main pillar of OOP.Child extends Parent class.
4+
# Inheritance example (php)
45

5-
# SuperClass | SubClass
6+
-> parent class
67

7-
* SuperClass: SuperClass (Parent,Base class) which inherited with another class and that class access parent class attributes and methods.
8+
```php
9+
class Car {
10+
public $make;
11+
public $model;
12+
public $year;
13+
14+
public function getDetails() {
15+
return "Make: " . $this->make . " Model: " . $this->model . " Year: " . $this->year;
16+
}
17+
}
18+
```
819

9-
* SubClass: SubClass (child,derived class) which inherit another class/classess.
20+
-> child class
1021

1122
```php
12-
// Super class
13-
class Info
14-
{
15-
private $name = "kamal";
16-
17-
public function getName()
18-
{
19-
return $this->name;
23+
24+
class SportsCar extends Car {
25+
public $price = 1200;
26+
27+
public function getPrice() {
28+
return $this->price;
2029
}
2130
}
2231

23-
// Subclass
24-
class User extends Info
25-
{
26-
//
27-
}
28-
$user = new User();
29-
$user->getName();
32+
$sportsCar = new SportsCar();
33+
$sportsCar->make = "Porsche";
34+
$sportsCar->model = "911 Turbo";
35+
$sportsCar->year = 2022;
36+
37+
echo $sportsCar->getDetails();
38+
// Output: Make: Porsche Model: 911 Turbo Year: 2022
3039
```
3140

32-
# Inheritance type
41+
# Difference between super and sub class
42+
43+
* `Super class`: Super class known as parent or base class. It inherited with another class (subclass).
44+
45+
* `SubClass`: SubClass is known as child class which inherit another class. Subclass can access properties and method from parent class.
3346

47+
# Inheritance types:
3448
* single
3549
* multi level
3650
* hierarchical

0 commit comments

Comments
 (0)