abstractinterfaces.md - brainchildservices/curriculum GitHub Wiki
What is an abstract class?
A class that is declared by using the keyword abstract is called an abstract class.An abstract class in C# provides only a blueprint and does not
contain the implementation of the class members.
What is the use of abstract class?
Abstract classes cannot be instantiated and are designed to be subclassed. They are used to provide some common functionality across a set of related
classes while also allowing default method implementations.
What is an abstract method?
A method which is declared abstract, has no “body” and declared inside an abstract class.
When to use the abstract method in C#?
Abstract methods are usually declared where two or more subclasses are expected to fulfill a similar role in a different manner.
Why abstract class cannot be instantiated?
Because it is not fully implemented the class as its abstract methods cannot be executed.
Who will provide the implementation (body) for abstract methods?
Implementation of the abstract method will be provided in the subclass which inherits the abstract class.
Can we declare an abstract method as sealed?
No, because it should be allowed to override in subclasses.
Can we declare an abstract method as private?
No, because it should be inherited to subclasses
What is a interface?
An interface is declared by using the interface keyword. It provides total abstraction; means all the methods in an interface are declared with the
empty body, and all the fields are public, static and final by default.
When will a C# compiler give a compile-time error "The modifier 'public/private/protected' is not valid for this item.".
If you use an access modifier in an interface, then the C# compiler will give a compile-time error "The modifier 'public/private/protected' is not
valid for this item.
What is the need of interface when we have the abstract class to define abstract methods?
To achieve security - hide certain details and only show the important details of an object (interface).
C# does not support "multiple inheritance" (a class can only inherit from one base class). However, it can be achieved with interfaces,
because the class can implement multiple interfaces. Note: To implement multiple interfaces, separate them with a comma
How interface is different from a class?
An interface is different from a class in the following ways:
We cannot instantiate an interface.An interface does not contain any constructor or data fields or destructor, etc.All of the methods of an interface are abstract and public by default.An interface is not extended by a class; it is implemented by a class.An interface can extend multiple interfaces.
What are the similarities between the interface and abstract class?
An interface is similar to an abstract class in the following ways
Both interface and the abstract class cannot be instantiated means we cannot create the object.But we can create a reference variable for both interface and abstract class.The subclass should implement all abstract methods.Both cannot be declared as sealed.