We help IT Professionals succeed at work.

Interface vs. Virtual vs. Abstract vs. Inheritance

deleyd
deleyd asked
on
abstract class TwoDShape {
 variables
 methods
 ...
}

public interface ISeries {
  variables
  methods
  ...
}

class MyClass {
  variables
  methods
  ...
}

Open in new window

It seems the syntax in the main code when it comes to defining a class which {inherits a regular class/implements an interface/inherits an abstract class} is the same for all three, is that correct?
Then in main code:
class Triangle : TwoDShape {
}
class ByTwos : ISeries {
}
class MyExt : MyClass {
}

Open in new window

Is the virtual keyword only for methods and variables and not for classes?

Interface - contract "You must implement these methods." Contains no code.
Abstract - class meant to be inherited. Class can not be instantiated. Contains code.

What is the syntax for a class which implements more than one interface?

Virtual - keyword placed on methods "Here's a default method to use unless you override it with something better."

So do I have this correct so far?
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
It seems the syntax in the main code when it comes to defining a class which {inherits a regular class/implements an interface/inherits an abstract class} is the same for all three, is that correct?
An interface definition cannot have variables--only properties and methods (because properties are turned into getter and setter methods under the hood by the compiler).

Is the virtual keyword only for methods and variables and not for classes?
Part yes; part no. It is for methods, but it is not for variables. It is for properties.

Interface - contract "You must implement these methods." Contains no code.
Yes, with the addition of properties that I mentioned above.

Abstract - class meant to be inherited. Class can not be instantiated. Contains code.
Yes.

What is the syntax for a class which implements more than one interface?
You include the interfaces being implemented as a comma-separated list:

class SomeClass : Interface1, Interface2, Interface 3
{

}

Open in new window

Explore More ContentExplore courses, solutions, and other research materials related to this topic.