'Extends' and 'Implements' Java equivalents in C#

JavaC#

Java Problem Overview


What is the C# equivalent syntax for the following Java statement:

public class Lion extends Animal implements Diurnal()
{
}

Java Solutions


Solution 1 - Java

  • Animal is Base class
  • Diurnal is an Interface

the inheritance could be declared like this.

public class Lion : Animal, Diurnal
{

}

In C#, you can inherit one base class and can be multiple Interfaces.

One more tip, if you are making an Interface in C#, prefix it with I. eg IDiurnal

Solution 2 - Java

public class Lion : Animal, // base class must go first
                    Diurnal // then interface(s) if any
{
}

Solution 3 - Java

Would look something like this:

public class Lion :Animal, Diurnal {
}

Where Animal is a class and Diurnal is an interface.

Please note, that according to the C# naming convention, interface has to have "I" infront of its name, so finally it should look like this:

public class Lion :Animal, IDiurnal {
}

Solution 4 - Java

In C#, there is uniform syntax for extending class and implementing interface.

public class Lion : Animal, Diurnal {

}

Solution 5 - Java

you need to write down first base class like(Animal is base class), lately interfaces like as(Diurnal is a Interface)

public class Lion : Animal, Diurnal {}

Solution 6 - Java

the first name after : is the extended class, after comes the implemented interfaces

public class Lion : Animal, Diurnal
{
}

c# do not allow multiple class extension, but you can implement many interfaces

Solution 7 - Java

public class Lion : Animal, Diurnal
{
}

interface Diurnal
{
}

class Animal
{
}

Class Animal was inherited by Lion class. Diurnal class is interface.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser1369905View Question on Stackoverflow
Solution 1 - JavaJohn WooView Answer on Stackoverflow
Solution 2 - JavaabatishchevView Answer on Stackoverflow
Solution 3 - JavaTigranView Answer on Stackoverflow
Solution 4 - JavansconnectorView Answer on Stackoverflow
Solution 5 - JavagdmanandamohonView Answer on Stackoverflow
Solution 6 - JavaMassimo ZerbiniView Answer on Stackoverflow
Solution 7 - JavaThu YaungView Answer on Stackoverflow