Changes

Jump to: navigation, search

Object Oriented Programming with Visual Basic

1,930 bytes removed, 18:14, 8 August 2007
Subclassing in PHP
The result will a message which reads "John Smith has balance of 1225", since the account fee has now been subtracted from the balance.
== Subclassing in PHP ==
Once a class has been defined it is possible to create a new class derived from it that extends the functionality of the original class. The parent class is called the ''superclass'' and the child the ''subclass''. The whole process is referred to as 'subclassing''.
 
A subclass of a class can be defined using the ''extends'' keyword when declaring the subclass. For example we might choose to create a subclass of of our ''bankAccount'' class called ''savingsAccount'' which defines an interest bearing type of account:
 
<pre>
<?php
class savingsAccount extends bankAccount {
 
private $interestRate = 5;
 
}
</pre>
 
The important point to note here is that savingsAccount inherits all the members and methods of bankAccount and adds a new member (the interest rate).
 
We can extend the class further by adding a new method to return the interest rate:
 
<pre>
<?php
class bankAccount {
 
private $accountNumber;
private $accountname;
 
public function __construct($acctNumber, $acctName) {
$this->accountNumber = $acctNumber;
$this->accountname = $acctName;
}
 
public function __destruct() {
}
 
public function setAccountNumber($acctNumber)
{
$this->accountNumber = $acctNumber;
}
 
public function setAccountName($acctName)
{
$this->accountName = $acctName;
}
 
public function getAccountName()
{
return $this->accountName;
}
 
public function getAccountNumber()
{
return $this->accountNumber;
}
 
}
 
 
class savingsAccount extends bankAccount {
 
private $interestRate = 5;
 
public function getInterestRate()
{
return $this->interestRate;
}
 
}
 
$mySaveObj = new savingsAccount('246810', 'Morgan Freeman');
 
echo "Savings Account Number is " . $mySaveObj->getAccountNumber() . '<br>';
 
echo "Interest Rate = " . $mySaveObj->getInterestRate() . '<br>';
?>
</pre>
== PHP Object Serialization ==

Navigation menu