Solved

overwriting a method

Posted on 2014-03-24
13
162 Views
Last Modified: 2014-04-01
from
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28396118.html

what is being overwritten

getsummaryline()

is the parent/child overwritten?
0
Comment
Question by:rgb192
  • 4
  • 4
  • 3
  • +1
13 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39951301
Yes, the child class overrides the parent's method. In this case it extends it.

$product1 and $product2 use their own version of getsummaryline()
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39951626
Note that this is a gigantic improvement in the workings of PHP.  In procedural PHP, a duplicate function name causes a fatal error.  Only in OOP can a child class replace the function (method) of a parent.
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39951672
I guess you want to know how the overwriting of the methods work here in detail, so see here:

this is the parent method (ShopProduct):
    function getSummaryLine() {
        $base  = "{$this->title} ( {$this->producerMainName}, ";
        $base .= "{$this->producerFirstName} )";
        return $base;
    }

Open in new window


And here the child CDProduct's method
    function getSummaryLine() {
        $base = parent::getSummaryLine();
        $base .= ": playing time - {$this->playLength}";
        return $base;
    }

Open in new window


As you can see, the latter calls the parent method result and add to it an additional string, so the output of the child method will be the output of the parent method plus a new data specific to the CD, that is the playing time.

The Book product will do the same thing adding the number of pages instead of the playing time. So the parent method returns the title of the product, tthe producer full name and the descendant clases add to these infos their specific data.

Hope to have correctly interpreted your question.

Cheers
0
 

Author Comment

by:rgb192
ID: 39951867
Yes, the child class overrides the parent's method. In this case it extends it.

I think overwrite is
   function getSummaryLine() {
        //I comment this line because this will extend, not overwrite $base = parent::getSummaryLine();
        $base .= ": playing time - {$this->playLength}";
        return $base;
    }
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39952434
Overwriting could totally replace the ancstor mthods, but usually it dosn't: it inherits the methods and add to it something specific. If one dosn't need to inherit a from a commn methods, he can ue interfaces instead. An intrface just lit the mthods one child class must hav, but it doesn't implemet none of thm, so child classes write their implementation of thos method from scratch.

http://www.php.net/manual/en/language.oop5.interfaces.php
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39953256
See if this makes sense.  The Animal can speak().  The exact contents of the vocalization is different for Cat and Dog, therefore they create different contents for the noise property.  And they use different methods to produce the noise.  In class and extensions, there are three versions of speak().  We use the scope-resolution operator to tell PHP which version of the method to run.

<?php // demo/temp_rgb192.php
error_reporting(E_ALL);

// http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28396128.html

Class Animal
{
    protected function speak()
    {
        echo $this->noise;
    }
}
Class Cat Extends Animal
{
    public function __construct()
    {
        $this->noise = 'Meow ';
    }
    public function speak()
    {
        parent::speak();
    }
}
Class Dog Extends Animal
{
    public function speak()
    {
        $this->noise = 'Woof ';
        echo $this->noise;
        parent::speak();
    }
}
$pussy = new Cat;
$pussy->speak();

$puppy = new Dog;
$puppy->speak();

Open in new window

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:rgb192
ID: 39965456
Overwriting could totally replace the ancstor mthods, but usually it dosn't: it inherits the methods and add to it something specific. If one dosn't need to inherit a from a commn methods, he can ue interfaces instead. An intrface just lit the mthods one child class must hav, but it doesn't implemet none of thm, so child classes write their implementation of thos method from scratch.

so adding to $base variable is still considering overwriting

interface parent methods are declared but never used, so when required methods are used in child (implementation) class then this is also considered overwriting









Ray example:
cat uses parent method speak and same noise property from cat

dog echos noise property and then calls parent method speak and same noise property from dog which is echoed again


So both are considering overwriting?
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 39965553
I'm sorry -- I don't know how to explain this in any way that is better or more clear.  I'll have to sign off and let another expert try to help you.
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 39965772
Yes, it is overwriting.
The Ray example is quite simple and clear, and i one of tha classic cases used in oop manuals to explain oop.
There you can see inheritance in action: both dog and cat are animals, so they inherit common characteristic from Animals class. In the example, they inherit tha ability to speak.
But cats and dogs are different and they 'speak' two different 'languages': so, when they born they have two different noises: in the cosntructor method (something like obstetrical department in a hospital) thay have two different noises 'meow' and 'woof'. When thy speak, they use the common method inherited from the fact they are both animals, but speaking they do two different noises.

Interfaces are slightly different: they don't implement any method but force all descendant class to implement the methods they have simply declared. o Interface 'Animals' can declare a method called 'eat' because all animals eat, but it doesn't implement because different animals eat different things, so let the descendant class 'species' speciify what each species eat.

Hope this is clear enough :)
0
 
LVL 34

Assisted Solution

by:Dan Craciun
Dan Craciun earned 250 total points
ID: 39966795
Think of an interface as forced overwriting; you have to write your own method, as the parent class does not provide one.
0
 

Author Comment

by:rgb192
ID: 39966786
Ok I now understand that animal speaking is overwriting because each animal speaks different language and not the same parent language.


Interfaces are slightly different: they don't implement any method but force all descendant class to implement the methods they have simply declared. o Interface 'Animals' can declare a method called 'eat' because all animals eat, but it doesn't implement because different animals eat different things, so let the descendant class 'species' speciify what each species eat.
Is interfaces overwriting?
Is interfaces inheritance?
0
 
LVL 30

Accepted Solution

by:
Marco Gasi earned 250 total points
ID: 39966937
rgb, in oop (and in many other aspect of the life) not all is perfectly defined and distrinct.
For instance, the overwriting exists because inheritance exists. In other words, a  method can be overwritten because it is inherited: if dog didn't inherit the speak method from its parent it couldn't overwrite it.

Overwriting presupposes the inheritance and this is true for interfaces also, but in this case you overwrite ampty methods so it seems wrong to say you're overwriting, but it's so.
0
 

Author Closing Comment

by:rgb192
ID: 39970860
overwrite empty methods
interface as forced overwriting;

thanks
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now