Jump to content

Why do we use abstract classes, or abstract methods in abstract classes?


silverglade

Recommended Posts

I do not understand why we would create a class if we are not able to use it, or an abstract class, if we cannot put something like "echo "hello world."; in it because that would be illegal. 

 

in my book it has the following code

 

abstract class Dog {
                  abstract function bark() {;
                             print "Woof!"; // illegal because we are having the abstract class doing something
                    }
                  }

So I was wondering, what is the point of declaring 

abstract class Dog {
           abstract function bark();
}

??
I don't know why we would do that. Any help greatly appreciated.

Link to comment
Share on other sites

A better example would be an Abstract animal class. Why? Because you can extend an animal class to a more specific type of animal.

 

Now, why would you use an Abstract method. To enforce the concrete implemnation to create that method. In the Animal example you might want to

enforce that all Animal Types (classes extending the Abstract Animal must have a makeNoise() method. Why? Becuase all animals (for sake of example) make noise.

 

So what about the methods that aren't abstract? You can use them for shared functionality. Things that Animal Types do the same (the same implementation).

 

So, with that in place, an example:

 

<?php

abstract class Animal {

    public abstract function makeNoise();

    public function goToSleep() {
        echo "zzz";
    }   

}

class Cat extends Animal {

    public function makeNoise() {
        echo "Meow!";
    }   
}

class Dog extends Animal {

    public function makeNoise() {
        echo "Woof!";
    }   
}

$dog = new Dog;

echo " A dog makes noise {$dog->makeNoise()} and now sleeps {$dog->goToSleep()}";

Link to comment
Share on other sites

Thank you both. Thanks Thorpe. Does that mean that the abstract class is basically like a blueprint for all other extended classes? Like abstract class Animal, is the blueprint for all animals, even though the functions of it are not defined they are just listed. So we have like "this is the abstract Animal class, all animals must have the sleep function and this is the blueprint for them", and then you make the "this is the Cat class that extends Animal, it must have the functions that Animal has, done in its own way, so that a cat's noise function is "meow", and a dog's is "bark". So the abstract classes are just blueprints? I could be wrong though.

Link to comment
Share on other sites

Thank you both. Thanks Thorpe. Does that mean that the abstract class is basically like a blueprint for all other extended classes? Like abstract class Animal, is the blueprint for all animals, even though the functions of it are not defined they are just listed. So we have like "this is the abstract Animal class, all animals must have the sleep function and this is the blueprint for them", and then you make the "this is the Cat class that extends Animal, it must have the functions that Animal has, done in its own way, so that a cat's noise function is "meow", and a dog's is "bark". So the abstract classes are just blueprints? I could be wrong though.

 

Basically, yes.  It is a blueprint to ensure that classes that extend other classes will contain necessary stuff.  Another way to put it is if you have a method1 in a class that depends on another method2 in a class, but method2 is defined by the child class, it is to make the child class implement method2, so that method1 will not break.  In other OOP languages, you are expected to do extra things like declare a return type, etc... to further make sure that methods like method2 will not be broken.

 

Building off of thorpe's example:

 

<?php
abstract class Animal {

    public abstract function makeNoise($noise);

    public function goToSleep() {
        $this->makeNoise("zzz"); // will fail for object of Cat!
    }   

}

class Cat extends Animal {

/*
    public function makeNoise($noise) {
        echo $noise;
    }   
*/
}

class Dog extends Animal {

    public function makeNoise($noise) {
        echo $noise;
    }   
}

$dog = new Dog;
$cat = new Cat;

echo " A dog makes noise ";
$dog->makeNoise('woof!');
echo " and now sleeps ";
$dog->goToSleep() . " <br/>";

echo "<br>";

echo " A cat makes noise ";
$cat->makeNoise('meow!'); // doesn't exist!
echo " and now sleeps ";
$cat->goToSleep() . " <br/>";

?>

 

The $cat stuff  will not work.  The goToSleep() method expects makeNoise() to exist (and Ideally have certain arguments and return a certain type).  You expect it because you expect a $cat to be able to make a noise, because it is an Animal.  So by making makeNoise an abstract method in the Animal class, you are forcing the child object to implement that method.  If you don't, it throws an error.

Link to comment
Share on other sites

Great thanks Crayon Violent for that. This cat and dog sleeping and making noise is getting funny. hehe. Thanks for the code. I understand it now.  :) :) Also, I like your paper cuttting quote and when I went to see if I could see your avatar larger, I saw your quote about people's windows issues going away without pirating and pron. HAHAHA> that is TRUE.

Link to comment
Share on other sites

:)

 

my avatar is supposed to be randomly generated.  It used to randomly generate but at some point in time one of the forum upgrades cached the avatar image so it's basically stuck on the last one before the cache :/

 

You can go here and refresh to see it.  Also here is a much bigger version that I based my random avatar off of

 

 

Link to comment
Share on other sites

wow that is cool. Too bad it doesn't refresh anymore. I refreshed it and it was pretty cool changing like different works of art. I think that was called "fractal art" or something. I know it is based on math though. I think God is a great programmer and mathematician. You can see evidence of strange fractal art in the patterns in butterflies, other insects, and birds' wings. Neat.  :D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.