Monday, October 3, 2011

OOPs concept in php

              1.Inheritance
                            Inheritance is the mechanism of deriving a new class from an existing class. It allows a sub-class / child class to share/inherit the attributes and behaviors of a base-class or parent class. These inherited attributes and behaviors are usually modified by means of extension.
                            Php support multiple inheritance by using interface.

               2.Abstract class
                            It is not allowed to create an instance of a class that has been defined as abstract. Any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method's signature they cannot define the implementation. When inheriting from an abstract class, all methods marked abstract in the parent's class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility
                            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. Objects of an abstract and interface class cannot be created i.e. only objects of concrete class can be created

              3.Interface
                            An interface is a contract between unrelated objects to perform a common function. An interface enables you to specify that an object is capable of performing a certain function, but it does not necessarily tell you how the object does so, this means that it leaves for classes implementing an interface to define its behaviour. To extend from an Interface, keyword implements is used.

              Diff btw abstract & interface
                            For abstract class a method must be declared as abstract. Abstract methods doesn’t have any implementation
                            For interface all the methods by default are abstract methods only. So one cannot declare variables or concrete methods in interfaces.
                            The Abstract methods can declare with Access modifiers like public, internal, protected. When implementing in subclass these methods must be defined with the same (or a less restricted) visibility.
                            All methods declared in an interface must be public.
                            Abstract class can contain variables and concrete methods.
                            Interfaces cannot contain variables and concrete methods except constants.
                            A class can Inherit only one Abstract class and Multiple inheritance is not possible for Abstract class.
                            A class can implement many interfaces and Multiple interface inheritance is possible.

No comments:

Post a Comment