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?
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.
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.
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.
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();
}
}
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
{
// ...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With