'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.