Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2009
    Location
    Pennsylvania, United States
    Posts
    54
    Thanks
    16
    Thanked 0 Times in 0 Posts

    Question OOP -> abstract classes.

    Today, I've been brushing up on my coding skills, so I decided to review the PHP manual (Object oriented Programming). Most of it came very easily to me, but I just do not get the point of abstract classes.

    Heres what I know about them so far:

    1. Any member declared abstract, the class itself must be declared as abstract
    2. You can not instantiate an abstract class


    Basically, from my understanding, abstract classes just hold methods and properties that MUST be defined in the child class is this correct? If not, can someone please explain to me what the point of abstract classes are and how to use them?

    Any input is greatly appreciated, Thanks!

  • #2
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    Abstract base classes (ABCs) have a couple primary uses:

    1) As an interface, you can define a generic interface that derived classes must implement. That lets you treat multiple similar but different objects the same. Especially since not all languages have a specific interface construct.

    2) It allows you to not define all the functions, so if you build a base class for say shapes and the base class can have the functions common to all types of shapes. But you could have a draw function that is abstract since that would be specific to the shape.
    OracleGuy

  • Users who have thanked oracleguy for this post:

    johnnnn (11-30-2009)

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Best phrase I can think of is minimal.
    While interfaces IMO have exceedingly more power than an extension ever will, abstracts are the perfect minimal implementation of an interface. If you have an interface with say a public function loadINI() method on it, and every class uses the same INI file, then it makes sense to use an abstract to provide this minimal implementation detail instead of having the exact method on every class implementing it. This is especially true if it makes use of a method that has unknown implementation at the time, allowing you're class to call this method; if there is no call to a required method, a concrete adapter may make more sense (an adapter provides non-implementation by throwing exceptions or providing no implementation details for the concrete class). You may also have a base class that does not implement a provided interface and still defines a method, and then later extend it and implement this method. That will also work, though the base is not itself considered a typeof the given interface.
    Note that the above can be easily modified to create an unrecognized bug in PHP (as in, the PHP team threw out my bug report indicating normal behaviour) when it is not the case. If you use this class structure:
    PHP Code:
    <?php

    interface IInterface
    {
        public function 
    test();
    }

    class 
    Grandfather
    {
        private function 
    __construct()
        {
        }

        private function 
    test()
        {
            
    printf("%s\n"__METHOD__);
        }
    }

    class 
    Father extends Grandfather implements IInterface
    {
        public function 
    __construct()
        {
            
    printf("%s\n"__METHOD__);
        }

        public function 
    test()
        {
            
    printf("%s\n"__METHOD__);
        }
    }

    class 
    Son extends Father
    {
        protected function 
    __construct()
        {
            
    parent::__construct();
            
    printf("%s\n"__METHOD__);
        }

        public static function 
    createInstance()
        {
            return new 
    self;
        }

        private function 
    test()
        {
            
    parent::test();
            
    printf("%s\n"__METHOD__);
        }
    }


    ?>
    Grandfather must not implement the interface and declare the same method signature as private.
    And attempt to use the test method on an instance of Son, it will succeed and throw an illegal access error. This is incorrect as the error should be thrown when the linking occurs for the Son to Father - a child cannot demote the access level of a parent. This bug chews on me so much.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    Rockstar Coder
    Join Date
    Jun 2002
    Location
    USA
    Posts
    9,074
    Thanks
    1
    Thanked 328 Times in 324 Posts
    It should also be said like all things relating to OOP, it is very easy to over do it. ABCs aren't needed all the time, you have to use care and consideration when designing your objects.
    OracleGuy


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •