Pages

Friday, March 7, 2014

Interview Questions C# - Can a class implement interfaces with same method signatures?

Objective

  • To understand the behavior of C# classes when implementing interfaces with same method signatures
  • To know the best practices when implementing interfaces with same method signatures
For us to understand C# behavior when implementing interfaces with same method signatures please take a look at these two(2) interfaces:

interface Interface1{
       void Method1();
}


interface Interface2{
       void Method1();
}


class Class1:Interface1, Interface2{
      void Method1(){
             Console.WriteLine("this method is called");
      }
}


In our Main Class:

public static void Main(string[] args){
      Interface1 interface1 = new Class1();
      Interface2 interface2 = new Class1();
      interface1.Method1();
      interface2.Method2();
Console.ReadLine(); }
When you hit "F5" when you go to Debug > Start Debugging you will see this result.




The answer to our question "Can a class implement interfaces with same method signature?" is "Yes" but,  what if you want different implementation for same method signatures?

Then you should Implement it Explicitly by modifying your Base Class Similar to this:

class Class1:Interface1, Interface2{
      void Interface1.Method1(){
  Console.WriteLine("Implemented Method From Interface1");
}
      void Interface2.Method1(){
  Console.WriteLine("Implemented Method From Interface2");
} }
The result will then be:
















Conclusion:

  • A class can implement interfaces with same method signatures
  • we can apply explicit implementation when we are aiming for different implementation of interfaces with same method signatures


Thursday, March 6, 2014

Introduction to OOP (Object Oriented Programming) - Inheritance

Objective

  • To be able to understand Inheritance
  • To be able to define a base class and base class
  • To implement Inheritance in C#

INHERITANCE

-Is one of the Pillars of OOP (Object Oriented Programming) which are Inheritance, Encapsulation and Polymorphism. Today, we are just going to tackle Inheritance which allows us to reuse, extend and modify the behavior that is defined in another class.

base class / parent class - The class whose members are inherited
derived class / child class - class that inherits the base class / parent class

in C#, all classes are derived from a base class Object (this provides low-level support to all classes).

to inherit a class we use ":".

public class ChildClass : ParentClass{}

by default all class can be inherited.
We can specify that a class cannot be inherited by using the keyword sealed


public sealed class ChildClass : ParentClass{}
in this way no other class can inherit the ChildClass
if you try inheriting a sealed class you will be having a compile time error 'cannot derived from sealed type' error.
to define that a class can only be inherited and cannot be instantiated (creating an instance of) we use the abstract
public abstract class ParentClass{}
in this scenario, we cannot create an instance of the ParentClass, we can only inherit it. If we try creating an instance of ParentClass we will have a 'Cannot create an instance of the abstract class or interface' error.
For our Exercise/Example:
  1. Create abstract class Transportation with properties: string Color, int NoOfWheels; and method Move() - that writes in a console with message "moved".
  2. Create a new class Car and inherit Transportation.
  3. In the Main Program Create an Instance of Car and fill-up the properties and call the Move() method in the created Car instance.
The Code:
Transportation Class:
public abstract class Transportation{
    //Properties
    public string Color {get; set;}
    public int NoOfWheels {get; set;}

    //Method
    public void Move(){
       Console.WriteLine(string.Format("the {0} transportation with {1} wheel/s moved!", this.Color, this.NoOfWheels));
    }
}

Car class inhereting Transportation Class:
public class Car : Transportation{}

The Program where we create an instance of Car and Call the Move() Method:
public class Program{
   Car car = new Car()
   {
   Color="Red",
   NoOfWheels = 4
   }

   car.Move();
}
Points to Remember:
  • a class can only inherit one(1) class
  • we cannot inherit a sealed class
  • we cannot create an instance of an abstract class
References:
  • http://msdn.microsoft.com/en-us/library/dd460654.aspx

Creating Your First C# Program

Hi, this is my first blog post regarding my experiences as a web developer. I decided to start this blog in order for me to be reminded what are the skills that I know and what are the skills that I am bound to learn. I also started this blog for people out there who want to learn about programming (using C# because this is my native language :D, may be will go with Java & PHP too, who knows?). For my first blog post, please allow me to create a "Hello, World!" console application using Visual Studio 2013.

Objective/s

  • To be able to create your first program in C#
  • To be able to convert a Class Library into a Console App

Requirement/s:


  • IDE - Visual Studio - (mine is VS 2013)
Steps
1. Open your VS2013 (or whatever Visual Studio version you have)


2. Press Ctrl+Shift+N or Go To File > New Project


 3. Select Visual C# then select Class Library and click OK. 
(Optional: Change the Project Name to whatever you prefer)



4. Select Project from the Solution Explorer and Click "Alt + Enter" or Right Click on the Project then click on Properties.



5. In the Application tab, select Output Type as Console Application
*by doing these you are telling to run this project as a Console Application



6. Select the class Class1.cs from the Solution Explorer and type these:



7. Run the program by either pressing F5 or by going to Debug > Start Debugging. A Console window will then appear with the message "Hello, World"