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

Started on the old forum in August 2011 · view the original in the archive · old links redirect here

5 replies · 593 views · started 15 years ago · 1 min read

5 participants
ba_maram_shiraz Newbie · 6 posts #1

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

😀

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 😀

why we should use from abstract classes ???

regards.

by3

firemax Developer · 221 posts #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

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

External image (may be gone) 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)](http://coding-talk.com/f14/how-to-secure-you…

redrdx Developer · 80 posts #3

firemax wrote: 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

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

Life On Clicks External image (may be gone) Get More Tricks Add Me On Gtalk ID Rdxmob@gmail.com

ksg91 Developer · 162 posts #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.

Follow me @ksg91 | My Blog: http://ksg91.com | Nokia Blog: http://NokiaTips.in

CreativityKills Lead · 833 posts #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.

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.

ba_maram_shiraz Newbie · 6 posts #6

CreativityKills wrote: 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.

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 :

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

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close