OOPS FAQ’s

Firs of all I would  like to inform and say sorry that you may find the questions in random order. In future if I could build the FAQ section big then definitely I would arrange the FAQ’s in proper seq.

Your feedback/ suggestion is most welcome to make the post more better. Thanks in advance….

In fact in FAQ section we will try to brush-up our OOPS knowledge with the help of some questions,
So, we will start now..

Question/ Scenario 1.0: 

Very frequently we use to write create the instance of a class like 

ClassA objClassA = new ClassA();

what does it mean by new ClassA();  or  what is the significance of new keyword?

Answer:

In order to answer this question I would like to take the example of an Web application,

Suppose we have a website which adds two numbers. Code for adding up the two numbers we have written in some class say Operation.cs or Operation.vb.

Now there are two person’s accessing the website say Person A and Person B. Both the persons may interested in adding the different different numbers for example (2,2) or (7,8) or any thing.

Writting new keword is nothing but intimiating the Framework/ Compiler that I want to create a new instance of the class or do not want to use the same instance which Person A is using to add (2,2). 
Question/ Scenario 2.0: 
We have tow C#/VB.NET classes say BaseClass and DerivedClass. BaseClass is a public class and contains four public methods,

  1. public double Add(double num1, double num2) {.......}
  2. public double Substract(double num1, double num2){.......}
  3. public double Multiply(double num1, double num2){.......}
  4. public double Divide(double num1, double num2){.......}

DerivedClass is also a public class and contains two public methods

  1. public double SimpleInterest(double price, double rate, double time){...............}
  2. public double CompoundInterest(double price, double rate, double time){...............}

Now DerivedClass derives BaseClass

public class DerivedClass : BaseClass { ...........}
Based on above scenario answer the following questions,

Question 2.1
Which of the following is a valid?  

       

  1. BaseClass baseClass = new DerivedClass();
  2. DerivedClass derClass = new BaseClass(); 
  3.  

Ans: 1, i.e. BaseClass baseClass = new DerivedClass();

but like this you would be able to access only public methods of BaseClass not the methods of derived class.

Question 2.2
Instance of the BaseClass is created like
BaseClass baseClass = new DerivedClass();
Which of the following is a valid?

  1. baseClass.SimpleInterest(1000,2,3);
  2. baseClass.Add(3,7);

Ans: 2, i.e. baseClass.Add(3,7);
because we had created object of BaseClass

Question 3.0: Does C# support multiple inheritance?
Answer: No. C# does not support multiple inheritance but multiple inheritance is possible with the help of Interfaces.

Question 4.0: What do you mean by an interface?
Read the illustration and answer the questions,
Answer: Interfaces describe a group of related functionalities that can belong to any class or struct. Interfaces can consist of methods, properties, events, indexers, or any combination of those four member types. An interface cannot contain fields. Interfaces members are automatically public.
When a class or struct is said to inherit an interface, it means that the class or struct provides an implementation for all of the members defined by the interface. The interface itself provides no functionality that a class or struct can inherit in the way that base class functionality can be inherited. However, if a base class implements an interface, the derived class inherits that implementation.

Classes and structs can inherit from interfaces in a manner similar to how classes can inherit a base class or struct, with two exceptions:

  1. A class or struct can inherit more than one interface.
  2. When a class or struct inherits an interface, it inherits only the method names and signatures, because the interface itself contains no implementations.

To implement an interface member, the corresponding member on the class must be public, non-static, and have the same name and signature as the interface member. Properties and indexers on a class can define extra accessors for a property or indexer defined on an interface. For example, an interface may declare a property with a get accessor, but the class implementing the interface can declare the same property with both a get and set accessor. However, if the property or indexer uses explicit implementation, the accessors must match.
Interfaces and interface members are abstract; interfaces do not provide a default implementation. For more information, see Abstract and Sealed Classes and Class Members.
Interfaces can inherit other interfaces. It is possible for a class to inherit an interface multiple times, through base classes or interfaces it inherits. In this case, the class can only implement the interface one time, if it is declared as part of the new class. If the inherited interface is not declared as part of the new class, its implementation is provided by the base class that declared it. It is possible for a base class to implement interface members using virtual members; in that case, the class inheriting the interface can change the interface behavior by overriding the virtual members.

Interfaces Quick Overview
An interface has the following properties:

  1. An interface is like an abstract base class: any non-abstract type inheriting the interface must implement all its members.
  2. An interface cannot be instantiated directly.
  3. Interfaces can contain events, indexers, methods and properties.
  4. Interfaces contain no implementation of methods.
  5. Classes and structs can inherit from more than one interface.
  6. An interface can itself inherit from multiple interfaces.

Question 4.1: Implement an interface say IOperations containing following method signatures

  1. Add : a method to add two double numbers
  2. Multiply : a method to multiply two double numbers


public interface IOperations
{
double Add(doubleb a, double b);
double Multiply(double a, double b);
}