Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to extend 2 classes at once?

Tags:

c#

.net

I have these classes :

public class myClassPage : System.Web.UI.Page
{
    public myClassPage ()
    {
    
    }
}

public class myClassControl : System.Web.UI.UserControl
{
    public myClassControl ()
    {
    
    }
}

and I'd like have another Class that extends these classes, something like this :

public class myClassData : myClassPage, myClassControl
{
    public myClassData ()
    {
    
    }
}

is it possible to do this or is there something else I could do?

like image 276
markzzz Avatar asked Jul 12 '11 13:07

markzzz


People also ask

Why we Cannot extend multiple classes?

When one class extends more than one classes then this is called multiple inheritance. For example: Class C extends class A and B then this type of inheritance is known as multiple inheritance.

Can a class extend another class?

The extends keyword extends a class (indicates that a class is inherited from another class). In Java, it is possible to inherit attributes and methods from one class to another. We group the "inheritance concept" into two categories: subclass (child) - the class that inherits from another class.

Can we extend 2 classes in PHP?

No you can't, respectively, not really, as manual of extends keyword says: An extended class is always dependent on a single base class, that is, multiple inheritance is not supported.


2 Answers

In the case where you need to extend two classes, you might be served to favor composition over inheritance, and to use interfaces as other answers have mentioned. An example:

Start by definining your interfaces

interface IFoo 
{
    void A(); 
}

interface IBar
{
    void B();
}

Then create concrete classes that implement each interface

class Foo : IFoo
{
    public void A()
    {
         // code
    }
}

class Bar : IBar 
{
    public void B()
    {
         // code 
    }
}

Finally, in the class you want to exhibit both sets of behaviors, you can implement each interface but compose the implementations with the concrete classes.

public class Baz : IFoo, IBar
{
    IFoo foo = new Foo(); // or inject 
    IBar bar = new Bar(); // or inject

    public void A()
    {
        foo.A();
    }

    public void B()
    {
        bar.B();
    }
}
like image 177
Anthony Pegram Avatar answered Oct 10 '22 01:10

Anthony Pegram


This is called Multiple Inheritance. You can find more information on Wikipedia - Multiple inheritance regarding the subject.

Multiple inheritance is supported in some langages:

Languages that support multiple inheritance include: C++, Common Lisp, Curl, Dylan, Eiffel, Logtalk, Object REXX, Scala, OCaml, Perl, Perl 6, POP-11, Python, and Tcl

In C#, interfaces can be used for mimicking multiple inheritance:

Some object-oriented languages, such as C#, Java, and Ruby implement single inheritance, although interfaces provide some of the functionality of true multiple inheritance.

Example:

public class myClassData : myClassPageInterface, myClassControlInterface 
{
    // ...
}
like image 38
Cyril Gandon Avatar answered Oct 10 '22 00:10

Cyril Gandon