Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

c#

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

public class Lion extends Animal implements Diurnal() { } 
like image 399
user1369905 Avatar asked Jan 25 '13 08:01

user1369905


People also ask

What's the difference between extends and implements Java?

The keyword extends is used when a class wants to inherit all the properties from another class or an interface that wants to inherit an interface. We use the implements keyword when we want a class to implement an interface.

Can I extends and implements together in Java?

Yes, you can. But you need to declare extends before implements : public class DetailActivity extends AppCompatActivity implements Interface1, Interface2 { // ... }

Does C++ have interfaces like Java?

C++ does not provide the exact equivalent of Java interface : in C++, overriding a virtual function can only be done in a derived class of the class with the virtual function declaration, whereas in Java, the overrider for a method in an interface can be declared in a base class.

How are extend and implement in C#?

Extends : This is used to get attributes of a parent class into base class and may contain already defined methods that can be overridden in the child class. Implements : This is used to implement an interface (parent class with functions signatures only but not their definitions) by defining it in the child class.


2 Answers

  • 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

like image 159
John Woo Avatar answered Oct 03 '22 04:10

John Woo


public class Lion : Animal, // base class must go first                     Diurnal // then interface(s) if any { } 
like image 29
abatishchev Avatar answered Oct 03 '22 05:10

abatishchev