What is Inheritance, Parent-Child Relationship, Constructor, __toString() function : MarcusCode. (2017, January 11). Inheritance in PHP. MarcusCode. https://marcuscode.com/lang/php/inheritance
Constructor, Inheritance, final keyword, Overriding inherited method, Inheritance and the Protected Access Modifier : W3Schools. (n.d.). PHP OOP Inheritance. W3Schools. https://www.w3schools.com/php/php_oop_inheritance.asp
<?php
class parentclass{
public function __construct(){
echo "This is parent constructor\n";
}
}
class childclass extends parentclass {
public function __construct(){
parent::__construct();
echo " This is Child class destructor\n";
}
}
$obj = new childclass();
?>
class parentclass {
public parentclass() {
System.out.println("This is parent constructor");
}
}
class childclass extends parentclass {
public childclass() {
super();
System.out.println("This is Child class destructor");
}
}
public class Main{
public static void main(String[] args) {
childclass obj = new childclass();
}
}
class parentclass:
def __init__(self):
print("This is parent constructor")
class childclass(parentclass):
def __init__(self):
super().__init__()
print("This is Child class destructor")
obj = childclass()
This is parent constructor
This is Child class destructor
<?php
class Vehicle{
public $brand;
public $price;
public function __construct($brand, $price){
$this->brand = $brand;
$this->price = $price;
}
protected function print(){
echo "Brand : {$this->brand}, Price : {$this->price}";
}
}
class Car extends Vehicle {
public function type(){
echo " is a Car\n";
}
}
$Car = new Car("Tesla",200);
$Car->print();
$Car->type(); #เกิด error ขึ้นเพราะ print() มีระดับการเข้าถึงแบบ protected
?>
public class Vehicle {
public String brand;
public int price;
public Vehicle(String brand, int price) {
this.brand = brand;
this.price = price;
}
protected void print() {
System.out.print("Brand : " + this.brand + ", Price : " + this.price);
}
}
public class Car extends Vehicle {
public Car(String brand, int price) {
super(brand, price);
}
public void type() {
System.out.println(" is a Car");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car("Tesla", 200);
car.print();
car.type();
}
}
class Vehicle:
def __init__(self, brand, price):
self.brand = brand
self.price = price
def print(self):
print(f"Brand : {self.brand}, Price : {self.price}", end='')
class Car(Vehicle):
def __init__(self, brand, price):
super().__init__(brand, price)
def type(self):
print(" is a Car")
car = Car("Tesla", 200)
car.print()
car.type()
เกิด error ขึ้นเนื่องจากอ็อบเจค Car ไม่สามารถเข้าถึงเมธอด print() ซึ่งอยู่ภายในคลาส Vehicle ได้
<?php
class Vehicle{
public $brand;
public $price;
public function __construct($brand, $price){
$this->brand = $brand;
$this->price = $price;
}
protected function print(){
echo "Brand : {$this->brand}, Price : {$this->price}";
}
}
class Car extends Vehicle {
public function type(){
$this->print();
echo " is a Car\n";
}
}
$Car = new Car("Tesla",200);
$Car->type();
?>
class Vehicle {
public String brand;
public int price;
public Vehicle(String brand, int price) {
this.brand = brand;
this.price = price;
}
protected void print() {
System.out.print("Brand : " + this.brand + ", Price : " + this.price);
}
}
class Car extends Vehicle {
public Car(String brand, int price) {
super(brand, price);
}
public void type() {
print();
System.out.println(" is a Car");
}
}
public class Main {
public static void main(String[] args) {
Car car = new Car("Tesla", 200);
car.type();
}
}
class Vehicle:
def __init__(self, brand, price):
self.brand = brand
self.price = price
def print(self):
print("Brand : {}, Price : {}".format(self.brand, self.price), end='')
class Car(Vehicle):
def __init__(self, brand, price):
super().__init__(brand, price)
def type(self):
self.print()
print(" is a Car")
car = Car("Tesla", 200)
car.type()
<?php
class Vehicle{
public $brand;
public $price;
public function __construct($brand, $price){
$this->brand = $brand;
$this->price = $price;
}
public function __toString(){
return "Brand : {$this->brand}, Price : {$this->price}";
}
}
$Vehicle = new Vehicle("Tesla",200);
echo $Vehicle;
?>
public class Vehicle {
public String brand;
public int price;
public Vehicle(String brand, int price) {
this.brand = brand;
this.price = price;
}
@Override
public String toString() {
return "Brand : " + this.brand + ", Price : " + this.price;
}
public static void main(String[] args) {
Vehicle vehicle = new Vehicle("Tesla", 200);
System.out.println(vehicle);
}
}
<?php
class Vehicle{
public $brand;
public $price;
public function __construct($brand, $price){
$this->brand = $brand;
$this->price = $price;
}
public function print(){
echo "Brand : {$this->brand}, Price : {$this->price}\n";
}
}
class Car extends Vehicle {
public function print(){
echo "Brand : {$this->brand}, Price : {$this->price} is a Car\n";
}
}
class Bike extends Vehicle {
public function print(){
echo "Brand : {$this->brand}, Price : {$this->price} is a Bike\n";
}
}
class Truck extends Vehicle {
public function print(){
echo "Brand : {$this->brand}, Price : {$this->price} is a Truck\n";
}
}
$Car = new Car("Tesla",200);
$Car->print();
$Bike = new Bike("Yamaha",50);
$Bike->print();
$Truck = new Truck("Ford",350);
$Truck->print();
?>
class Vehicle{
public String brand;
public double price;
public Vehicle(String brand,double price){
this.brand=brand;
this.price=price;
}
public void print(){
System.out.println("Brand : "+this.brand+", Price : "+this.price);
}
}
class Car extends Vehicle{
public Car(String brand, double price) {
super(brand, price);
}
public void print(){
System.out.println("Brand : "+this.brand+", Price : "+this.price+" is a Car");
}
}
class Bike extends Vehicle{
public Bike(String brand, double price) {
super(brand, price);
}
public void print(){
System.out.println("Brand : "+this.brand+", Price : "+this.price+" is a Bike");
}
}
class Truck extends Vehicle{
public Truck(String brand, double price) {
super(brand, price);
}
public void print(){
System.out.println("Brand : "+this.brand+", Price : "+this.price+" is a Truck");
}
}
public class Inheritance{
public static void main(String[] args){
Car car = new Car("Tesla",200);
Bike bike = new Bike("Yamaha",50);
Truck truck = new Truck("Ford",350);
car.print();
bike.print();
truck.print();
}
}
class Vehicle:
def __init__(self,brand,price):
self.brand = brand
self.price = price
def print(self):
print("Brand : "+self.brand+", Price : "+self.price)
class Car(Vehicle):
def __init__(self,brand,price):
super().__init__(brand,price)
def print(self):
print("Brand : ",self.brand,", Price : ",self.price," is a Car")
class Bike(Vehicle):
def __init__(self,brand,price):
super().__init__(brand,price)
def print(self):
print("Brand : ",self.brand,", Price : ",self.price," is a Bike")
class Truck(Vehicle):
def __init__(self,brand,price):
super().__init__(brand,price)
def print(self):
print("Brand : ",self.brand,", Price : ",self.price," is a Truck")
car = Car("Tesla",100)
car.print()
bike = Bike("Yamaha",50)
bike.print()
truck = Truck("Ford",350)
truck.print()
Brand : Tesla, Price : 200 is a Car
Brand : Yamaha, Price : 50 is a Bike
Brand : Ford, Price : 350 is a Truck
<?php
final class Vehicle{
public function __construct(){
echo " This is parent class constructor\n";
}
}
class Car extends Vehicle {
public function __construct(){
parent::__construct();
echo " This is Child class destructor\n";
}
}
?>
final class Vehicle {
public Vehicle() {
System.out.println("This is parent class constructor");
}
}
class Car extends Vehicle {
public Car() {
super();
System.out.println("This is Child class constructor");
}
}
public class Main{
public static void main(String[] args) {
Car car = new Car();
}
}
public class Vehicle {
public Vehicle() {
System.out.println("This is parent class constructor");
}
}
public class Car extends Vehicle {
public Car() {
super();
System.out.println("This is Child class constructor");
}
public static void main(String[] args) {
Car car = new Car();
}
}
#include <stdio.h>
typedef struct {
void (*constructor)(struct Vehicle*);
} Vehicle;
void vehicle_constructor(Vehicle* self) {
printf("This is parent class constructor\n");
}
typedef struct {
Vehicle base;
} Car;
void car_constructor(Car* self) {
vehicle_constructor(&self->base);
printf("This is Child class constructor\n");
}
int main() {
Car car;
car_constructor(&car);
return 0;
}