IT Community - Software Programming, Web Development and Technical Support

Abstract classes

This is a discussion on Abstract classes within the PHP Programming forums, part of the Web Development category; A class may be declared as abstract so as to prevent it from being instantiated. However, you may inherit from ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > PHP Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 12-05-2007, 10:09 AM
ragavraj ragavraj is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 92
ragavraj is on a distinguished road
Default Abstract classes

A class may be declared as abstract so as to prevent it from being instantiated. However, you may inherit from an abstract class.
PHP Code:
abstract class MyBaseClass {
    function 
display() {
        print 
"Default display routine being called";
    }

Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-06-2007, 06:15 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: Abstract classes

Hi rajan,

when i try to instantiate to Abstract classes it throw Fatal Error. Can you explain me what is the use of abstract class?

Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 12-06-2007, 08:38 PM
ragavraj ragavraj is offline
D-Web Programmer
 
Join Date: Feb 2007
Posts: 92
ragavraj is on a distinguished road
Default Re: Abstract classes

When designing class hierarchies, you might want to partially leave certain methods for inheriting classes to implement.


It might make sense to implement setCenter($x, $y) in class Shape and leave the implementation of the draw() methods to the concrete classes Square and Circle. You would have to declare the draw() method as an abstract method so that PHP knows you are intentionally not implementing it in class Shape. The class Shape would then be called an abstract class, meaning that it's not a class with complete functionality and is only meant to be inherited from. You cannot instantiate an abstract class. You can define any number of methods as abstract, but once at least one method of a class is defined as abstract, the entire class needs to be declared as abstract, too. This double definition exists to give you the option to define a class abstract even if it doesn't have any abstract methods, and to force you to define a class with abstract methods as abstract so that it is clear to others what you had in mind.

The previous class diagram would translate into PHP code that's similar to the following:

PHP Code:
abstract class Shape {
    function 
setCenter($x$y) {
        
$this->$x;
        
$this->$y;
    }

    abstract function 
draw();
    protected 
$x$y;
}

class 
Square extends Shape {
    function 
draw()
    {
        
// Here goes the code which draws the Square
        
...
    }
}

class 
Circle extends Shape {
    function 
draw()
    {
        
// Here goes the code which draws the Circle
        
...
    }


You can see that the draw() abstract method does not contain any code.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 12-07-2007, 07:55 AM
Kamalakannan Kamalakannan is offline
D-Web Analyst
 
Join Date: May 2007
Posts: 299
Kamalakannan is on a distinguished road
Default Re: Abstract classes

Hi rajan,

Thanks for your reply. Same thing we doing in interface (ie method declaration only) than what is difference between interface and abstract class.

Regards,
R.Kamalakannan.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Difference between abstract class and interface srikumar_l C# Programming 0 12-17-2007 03:18 AM
difference between abstract class and interface vijayanand Java Programming 2 11-27-2007 09:19 PM
How can I implement opaque (abstract) data types in C? prasath C and C++ Programming 1 07-30-2007 04:24 AM
What’s the difference between an interface and an abstract class? Sabari Java Programming 1 07-17-2007 05:00 AM
Abstract Class nhoj Java Programming 1 05-08-2007 05:35 AM


All times are GMT -7. The time now is 05:52 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0