:::::: what is Abstract class ? ( Question )

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    :::::: what is Abstract class ? ( Question )

    hello guys.
    first i should say sorry for bad english.

    :D

    look , i visit more than 30 webpage about abstract classes and i don't understand what is abstract class.
    i want know : what should we use abstract ???
    we can create a normal class and extends it to other class , so we don't user abstract :D

    why we should use from abstract classes ???

    regards.

    by3

    #2
    It's all about what you want to model, Abstract classes work by inheritance. Being just special base classes, they model some is-a-relationship.

    For example, a dog is an animal, thus we have
    Code:
    class Dog : Animal { ... }
    As it doesn't make sense to actually create a generic all-animal (what would it look like?), we make this Animal base class abstract - but it's still a base class. And the Dog class doesn't make sense without being an Animal.

    Interfaces on the other hand are a different story. They don't use inheritance but provide polymorphism (which can be implemented with inheritance too). They don't model an is-a relationship, but more of a it does support.

    Take e.g. IComparable - an object that supports being compared with another one.

    The functionality of a class does not depend on the interfaces it implements, the interface just provides a generic way of accessing the functionality. We could still Dispose of a Graphics or a FileStream without having them implement IDisposable. The interface just relates the methods together.

    In principle, one could add and remove interfaces just like extensions without changing the behaviour of a class, just enriching the access to it. You cannot though take away a base class as the object would become meaningless!

    Here's few examples, if your using php

    There is good explanation of that PHP 5 OOP: Interfaces Abstract Classes and the Adapter Pattern - Developer.com

    PHP: Class Abstraction - Manual

    PHP: Class Abstraction - Manual

    Free Mobile Web Scripts by me: Free Youtube Downloader, Tweets Reader, Facebook Wall Posts Reader
    PHP Tutorials: How to Secure Your PHP Script (PHP SECURITY)
    Want to Develop/Edit your WAP/Web Site? Add me to Gtalk (gmail) 'lakshan1989' or PM me.

    Comment


      #3
      Originally posted by firemax View Post
      It's all about what you want to model, Abstract classes work by inheritance. Being just special base classes, they model some is-a-relationship.

      For example, a dog is an animal, thus we have
      Code:
      class Dog : Animal { ... }
      As it doesn't make sense to actually create a generic all-animal (what would it look like?), we make this Animal base class abstract - but it's still a base class. And the Dog class doesn't make sense without being an Animal.

      Interfaces on the other hand are a different story. They don't use inheritance but provide polymorphism (which can be implemented with inheritance too). They don't model an is-a relationship, but more of a it does support.

      Take e.g. IComparable - an object that supports being compared with another one.

      The functionality of a class does not depend on the interfaces it implements, the interface just provides a generic way of accessing the functionality. We could still Dispose of a Graphics or a FileStream without having them implement IDisposable. The interface just relates the methods together.

      In principle, one could add and remove interfaces just like extensions without changing the behaviour of a class, just enriching the access to it. You cannot though take away a base class as the object would become meaningless!

      Here's few examples, if your using php

      There is good explanation of that PHP 5 OOP: Interfaces Abstract Classes and the Adapter Pattern - Developer.com

      PHP: Class Abstraction - Manual

      PHP: Class Abstraction - Manual




      bro why u Remove me in Gtalk Rdxmob@gmail.com

      Comment


        #4
        abstract class will have one or more methods to be abstract, means they are not defined. Abstract methods contain only signature/declaration , not definition. As firemax explained explained, reason behind this is Inheritance. This is used when several classes have several common properties.
        Let say we consider some classes like Circle, Triangle, Rectangle etc. These all will have some common properties and methods like getArea() etc. So here, we will use generalization and Generalize these class in class Geometric which will have abstract method say getArea(). Now All Class like Circle, Triangle, Rectangle will inherit Geometric class so we do not need to redundant those properties. And we can not implement getArea() method in Geometric because all those class will have different method to calculate area.
        You can not instantiate an abstract class, you may inherit it and implement all abstract methods and then only you can instantiate child class.

        Interface is somewhat similar to an abstract class but interface will contain all methods to be abstract and when you implement any interface, you must implement all methods. Interface provide a skeleton to some class.
        Last edited by ksg91; 15.08.11, 05:56. Reason: typos
        Follow me @ksg91 | My Blog: http://ksg91.com | Nokia Blog: http://NokiaTips.in

        Comment


          #5
          All those guys said were too technical. Abstract classes defines shud i say rules for other classes to INHERIT. Typical usage wud b for creating Classes with multiple drivers. For instance a log class that has multiple drivers (database or file) abstract can b used in such instance.

          PHP Code:
          class Log_Driver_Database extends Log_Driver {

          public function 
          write($id$value){
          // some db query to write log to database
          }

          }

          class 
          Log_Driver_File extends Log_Driver {

          public function 
          write($id$value){
          // code to write log to a file, nt database
          }

          }

          abstract class 
          Log_Driver {

          abstract public function 
          write($id$value);

          }

          class 
          Log {

          public static 
          $driver;

          public function 
          __construct(Log_Driver $driver) {
          $this->driver = new $driver;
          }

          public function 
          write($id$value) {
          try{
          return 
          $this->driver->write($id$value);
          }catch(
          Exception $e) {
          // throw any exception from driver to avoid
          // exception without stack frame error
          }
          }

          }

          // usage would be like thus...

          // write logs to file
          $log = new Log(new Log_Driver_File);
          $log->write('filename''Log me baby!');

          // ...to database
          $log = new Log(new Log_Driver_Database);
          $log->write(null'Log me baby!'); 
          there are defo more advanced ways of using abstracts.

          Comment


            #6
            Originally posted by CreativityKills View Post
            All those guys said were too technical. Abstract classes defines shud i say rules for other classes to INHERIT. Typical usage wud b for creating Classes with multiple drivers. For instance a log class that has multiple drivers (database or file) abstract can b used in such instance.

            PHP Code:
            class Log_Driver_Database extends Log_Driver {

            public function 
            write($id$value){
            // some db query to write log to database
            }

            }

            class 
            Log_Driver_File extends Log_Driver {

            public function 
            write($id$value){
            // code to write log to a file, nt database
            }

            }

            abstract class 
            Log_Driver {

            abstract public function 
            write($id$value);

            }

            class 
            Log {

            public static 
            $driver;

            public function 
            __construct(Log_Driver $driver) {
            $this->driver = new $driver;
            }

            public function 
            write($id$value) {
            try{
            return 
            $this->driver->write($id$value);
            }catch(
            Exception $e) {
            // throw any exception from driver to avoid
            // exception without stack frame error
            }
            }

            }

            // usage would be like thus...

            // write logs to file
            $log = new Log(new Log_Driver_File);
            $log->write('filename''Log me baby!');

            // ...to database
            $log = new Log(new Log_Driver_Database);
            $log->write(null'Log me baby!'); 
            there are defo more advanced ways of using abstracts.
            see guys , in above code we create an Abstract class with a method with name : write.

            we use write in 2 other class ( Log_Driver_Database & Log_Driver_File ) , we declare write with diferent code in each write method of classes , so we can , don't create abstract write , and put write method in classes direct.

            sorry for bad english , see blow :

            PHP Code:

            class Log_Driver_Database  {

            public function 
            write($id$value){
            // some db query to write log to database
            }

            }

            class 
            Log_Driver_File {

            public function 
            write($id$value){
            // code to write log to a file, nt database
            }

            }

            ///// I FILTER ABSTRACT CLASS ////////

            //////// abstract class Log_Driver {

            ////////     abstract public function write($id, $value);

            //////// } 
            i directly put write method in classes

            tnx

            Comment

            Working...
            X