views:

115

answers:

6

This is very basic question from programming point of view but as I am in learning phase, I thought I would better ask this question rather than having a misunderstanding or narrow knowledge about the topic.

So do excuse me if somehow I mess it up.

Question:

Let's say I have class A,B,C and D now class A has some piece of code which I need to have in class B,C and D so I am extending class A in class B, class C, and class D

Now how can I access the function of class A in other classes, do I need to create an object of class A and than access the function of class A or as am extending A in other classes than I can internally call the function using this parameter.

If possible I would really appreciate if someone can explain this concept with code sample explaining how the logic flows.

Note

Example in Java, PHP and .Net would be appreciated.

+2  A: 

depending on the access level (would be protected or public in .NET), you can use something like:

base.method(argumentlist);

the base keyword in my example is specific to C#

there is no need for an instance of class A, because you already have a class A inherited instance

David
A: 

Basically you need a reference to the parent class.

In PHP:

parent::example();

From: http://www.php.net/manual/en/keyword.parent.php

<?php
class A {
    function example() {
        echo "I am A::example() and provide basic functionality.<br />\n";
    }
}

class B extends A {
    function example() {
        echo "I am B::example() and provide additional functionality.<br />\n";
        parent::example();
    }
}

$b = new B;

// This will call B::example(), which will in turn call A::example().
$b->example();
?>
zaf
can you explain this in detail ?
Rachel
Added example from php.net Makes sense?
zaf
now lets say if am not creating an function in `class B` than how can I directly called the function in `class A`, do i need to create an instance of `class A` in `class B` before accessing `class A` function even if `class B` is extending `class A`
Rachel
You shouldn't have to create an instance of `class A` to access the methods.
Josh K
so than I can directly use `$this->Class A Method` to access class A functions ?
Rachel
In the code sample you don't see in class B the creation of class A. You just use 'parent::example();' becaue the parent is class A.
zaf
+2  A: 

Let's forget about C and D because they are the same as B. If class B extends class A, then objects of type B are also objects of type A. Whenever you create an object of type B you are also creating an object of type A. It should have access to all of the methods and data in A (except those marked as private, if your language supports access modifiers) and they can be referred to directly. If B overrides some functionality of A, then usually the language provides a facility to call the base class implementation (base.Foo() or some such).

Inheritance Example: C#

public class A
{
     public void Foo() { } 
     public virtual void Baz() { }
}

public class B : A  // B extends A
{
      public void Bar()
      {
          this.Foo();  // Foo comes from A
      }

      public override void Baz() // a new Baz
      {
          base.Baz();  // A's Baz
          this.Bar();  // more stuff
      }
}

If, on the other hand, you have used composition instead of inheritance and B contains an instance of A as a class variable, then you would need to create an object of A and reference it's (public) functionality through that instance.

Composition Example: C#

 public class B // use A from above
 {
     private A MyA { get; set; }

     public B()
     {
         this.MyA = new A();
     }

     public void Bar()
     {
         this.MyA.Foo();  // call MyA's Foo()
     }
 }
tvanfosson
can you elaborate on composition part instead of inheritance as it is not very clear ?
Rachel
@Rachel: with composition, you;re essentially relaying calls to B.Bar() to A.Foo(). This is commonly used when A and B does not form an "is-a" relationship. Example: Ford is-a Car so Ford inherits Car to share implementation; but Car is not a Steering though Car have properties that makes it steerable, composition better reflects that fact than inheritance.
Lie Ryan
@Rachel -- I added an example. Does that help?
tvanfosson
I don't get the two downvotes -- anyone care to correct something I've gotten wrong here?
tvanfosson
@tvanfosson: Surely I understand now better.
Rachel
A: 

I find that the best way to tame the complexity of inheritance is to ensure that I only make B inherit from A when it really is a specialization of the superclass. At that point, I can call A's methods from inside B just as if they were B's own methods, and if B has overridden them then I can only suppose that this must be for a good reason.

Of course, quite often it is useful for B's implementation of a method to invoke A's implementation on the same object, generally because the subclass is wrapping extra behavior around the superclass's basic definition. The way in which you do this varies between languages; for example, in Java you do this:

super.methodName(arg1, ...);
Donal Fellows
A: 

Here's a quick Java example:

public class Aclass
{
    public static void list_examples()
    {
        return("A + B = C");
    }
}
public class Bclass extends Aclass
{
    public static void main(String [] args)
    {
        System.out.println("Example of inheritance "+list_examples);
    }
}

Note that the method for accessing the parent class shouldn't change. Because you are extending you shouldn't have to say parent:: or anything unless you are overriding the parent method / function.

Josh K
A: 

It seems to me that extending your class might not be your best option. Class "B", "C", and "D" should only extend class "A" if they are truly an extension of that class, not just to access some method. For instance "Huffy" should not extend "BrandNames" just because "Huffy" is a brand name and you want access to one of the methods of "BrandNames". "Huffy" should instead extend "Bicycle" and implement an interface so the methods of "BrandNames" can be used. An additional benefit here is that (in Java) multiple interfaces can be used but a class can only be extended once. If in your example class "B"' needed to access a method from class "A" that could work, but if class "C" needed to access a method from class "A" and class "'B"' then you would have to use an interface in class "'C".

typoknig