Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
297 views

Abstract Classes in PHP

Abstract classes are base classes that cannot be instantiated and contain some methods without implementation. Child classes must implement abstract methods. Abstract classes ensure conformity by requiring implementation of abstract methods. They allow single inheritance and abstract classes can extend other abstract classes. Child classes must implement all abstract methods of parent abstract classes.

Uploaded by

shijinbgopal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
297 views

Abstract Classes in PHP

Abstract classes are base classes that cannot be instantiated and contain some methods without implementation. Child classes must implement abstract methods. Abstract classes ensure conformity by requiring implementation of abstract methods. They allow single inheritance and abstract classes can extend other abstract classes. Child classes must implement all abstract methods of parent abstract classes.

Uploaded by

shijinbgopal
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Abstract Classes

Abstract classes and methods are introduced in PHP 5 An abstract class is a class which will usually be used as a base class for other classes to inherit from

An abstract class is a class with or without data members that provides some functionality and leaves the remaining functionality for its child class to implement.
The child class must provide the functionality not provided by the abstract class or else the child class also becomes abstract. If a class contains abstract method then the class must be declared as abstract. Any method which is declared as abstract must not have the implementation part but the declaration part only. The child classes which inherits the property of abstract base class, must define all the methods declared as abstract.

Objects of an abstract and interface class cannot be created i.e. only objects of concrete class can be created

To define a class as Abstract, the keyword abstract is to be used In the class example, the method getPrice() in class Furniture has been declared as Abstract. This means that its the abstract Class_Name { of the child //responsibility insert attribute definitions here class to provide the functionality of getPrice(). The BookShelf class is a child of the // insert method definitions here Furniture class and hence provides the function body for getPrice(). } The child classes which inherits the property of abstract base class, must define all the methods declared as abstract. An abstract method has no body

class BookShelf extends Furniture Example of Abstract Class { abstract class Furniture private $price; { private $height, width, length; public setData($h, $w, $l, $p) public function setData($h, $w, $l) { { parent::setData($h, $w, $l); $this->height = $h; $this->price = $p; $this->width = $w; } $this->length = $l; //this is the function body of the parent abstract method } public function getPrice() //this function is declared as abstract and hence the function { //body will have to be provided in the child class return $this->price;
public abstract function getPrice(); }

} }

Private methods cannot be abstract :


An abstract class need not contain any abstract methods

If a method is defined as abstract then it cannot be declared as private (it can only be public or protected). This is because a private method cannot be inherited.

On the other hand, if a class is not declared as abstract, it cannot contain any abstract methods You can create a new abstract class that extends an earlier one abstract class MyAbstractBaseClass { public abstract function doSomething();

}
abstract class MyAbstractSubClass extends MyAbstractBaseClass {} Abstract classes ensure conformity because any classes derived from them must implement all abstract methods derived within the class. Attempting to forgo implementation of any abstract method defined in the class results in a fatal error.

Characteristics of Abstract Classes 1. Single inheritance. Child classes can extend only one class at a time.

2. Abstract classes cannot be instantiated


3. Abstract classes can define class variables of type const only. 4. Abstract class A can be extended by another abstract class B. Abstract class B can implement none or any of the abstract functions in A. 5. In the previous case, a child class C which extends abstract class B must implement all abstract functions in B as well as the abstract functions in A which have not already been implemented in B. 6. The signature of the concrete functions and abstract functions must be the same.

7. The visibility of functions in the child classes must be the same or less restrictive than the parent class. Thus, a
protected abstract function can be implemented as either protected or public but not private. 8. Declaring functions as static abstract throws a strict warning in PHP 5.2 or earlier, however, as of PHP 5.3 this is allowed.

Characteristics of Abstract Classes 1. Single inheritance. Child classes can extend only one class at a time.

2. Abstract classes cannot be instantiated


3. Abstract classes can define class variables of type const only. 4. Abstract class A can be extended by another abstract class B. Abstract class B can implement none or any of the abstract functions in A. 5. In the previous case, a child class C which extends abstract class B must implement all abstract functions in B as well as the abstract functions in A which have not already been implemented in B. 6. The signature of the concrete functions and abstract functions must be the same.

7. The visibility of functions in the child classes must be the same or less restrictive than the parent class. Thus, a
protected abstract function can be implemented as either protected or public but not private. 8. Declaring functions as static abstract throws a strict warning in PHP 5.2 or earlier, however, as of PHP 5.3 this is allowed.

You might also like