• 1. What is OOP programming in C#?

    Answer:

    OOP is a type of programming, it is focusing on objects and getting objects are the center of everything. For example, we want to create an application to manage all students of a school, the object for this app will be student, class, teacher, subject ….

  • 2. What is an Object?

    Answer:

    Object in OOP is like an object in the real life, for example, a vehicle, animal, human, student… An object has behaviors and properties. For example, here is a Human object:

    Top 11 C# OOP interview questions and answers for Junior

  • 3. What is the difference between Interface and Abstract Class in C#?

    Answer:

    Interface

    • It contains only a declaration part.
    • Multiple inheritances are achieved by the interface.
    • It does not contain a constructor.
    • It does not contain static members.
    • It only contains public access modifier because everything in the interface is public.
    • The performance of the interface is slow because it requires time to search for the actual method in the corresponding class.
    • It is used to implement peripheral abilities of the class.
    • A class can use multiple interfaces.
    • It should be fully implemented.

    Abstract Class

    • It contains both the declaration and definition parts.
    • Multiple inheritances are not achieved by an abstract class.
    • It can contain one or more constructors.
    • It can contain static members.
    • It can contain different types of access modifiers like public, private, protected, etc.
    • The performance of an abstract class is fast.
    • A class can only use one abstract class.
    • An abstract class can contain methods, fields, constants, etc.
    • It can be fully, partially or not implemented.
  • 4. When should we choose to use Interface and Abstract class? Provide some examples.

    Answer:

    - Interface represents the struct of an Object, It outlines all behaviors and properties of an object will have. 

    - Abstract class is like a parent class, something can share with the sub-class, something can not.

    - A class implements an interface NOT inheritance, so for this reason a class can implement many interfaces but just only one abstract class.

    - Each object should have its own interface, for example, I designed an application to manage students in a school, this app has some objects that use this app, such as Student, Class, and Subject. I will create three interfaces IStudent, IClasses and ISubject

    - Some logic business, you don't want to re-write in many where you can use an abstract class with a virtual method.

  • 5. What are Override and Overload in C#?

    Answer:

    Overloading

    - it means, inside a class can have many methods same name but different parameters, and returns of the method.

    - This feature is very important for OOP, it’s very useful and makes OOP be stronger.

    - See this class Calculator below with Add methods:

    public class Calculator {
        public int Add(int a, int b) => a + b;
        public double Add(double a, double b) => a + b;
        public double Add(double a, int b) => a + b;
        public double Add(double a, double b, int c) => a + b + c;
    }

    Overriding 

    - It means when a class inheritance an abstract class, or normal class and you need to override some methods or properties.

    - Override can be re-write some logic from the parent class or define logic for method override.

    - See an example below:

    public abstract class Vehical
    {
       public abstract void Create();
     
       public virtual void Print(){
          Console.WriteLine("I am a parent");
       }
    }
     
    public class Car : Vehical
    {
        public override void Create()
        {
            throw new NotImplementedException();
        }
     
        public override void Print(){
          Console.WriteLine("I am a child");
       }
    }
    
  • 6. What is property? What feature does it represent in OOP?

    Answer:

    - Property in C# is a member of a class that provides a flexible mechanism for classes to expose private fields.

    - Properties enable a class to expose a public way of getting and setting values for a private variable.

    - Properties can be read-write (they have both a get and a set accessor), read-only (they have a get accessor but no set accessor), or write-only (they have a set accessor, but no get accessor). Write-only properties are rare and are most commonly used to restrict access to sensitive data.

    - An example about properties, I created DateNow property:

    public class DateData
    {
        private DateTime _date;
     
        public DateTime DateNow
        {
            get { return _date;}
            set
            {
                _date = DateTime.Now;
            }
        }
    }

    - Property help protect data so it is the same with the encapsulation feature of OOP.

  • 7. What is the difference between Inheritance and Composition in C#?

    Answer:

    Inheritance

    - it is an is-a relationship between classes, it will answer the question who are you?

    - Inheritance is an important feature of OOP

    - Let's check this example to get more understanding of inheritance:

    public abstract class Vehicle
    {
       public int WheelNumber { get; set; }
       public int Style { get; set; }
     
       public virtual void FuelType(){
          Console.WriteLine("I am using Diesel");
       }
    }
     
    public class Car : Vehicle
    {
        public override void FuelType(){
          Console.WriteLine("I am using gas 95");
       }
    }

    The Car class will contain all properties and methods of Vehicle class. 

    Composition

    - It is a has-a relationship between classes, it will answer the question what do you have?

    Consider this example below:

    public class Car
    {
        private readonly Engine _engine;
        private readonly Seat _seat;
           
        public Vehical()
        {
            _engine = new Engine();
            _seat = new Seat();
        }
     
        public string GetEngine() => _engine.BuildEngine();
        public string GetSeatNumber() => _seat.BuildSeat();
    }
  • 8. Explain Polymorphism OOP in C# and can this feature apply to the interface?

    Answer:

    Polymorphism is an important feature of OOP. it means from a base thing we can create many other things. The base thing is very abstract, it same as the parent having something basic, very basic and other things can inherit it. 

    To get more understanding, we consider an example below:

    Me: What do animals sound like?
    You: Which animal you would like to know?
    Me: Dog, Cat and Duck

    From this conversation, you can see that animal is very abstract because have many types of animals. But dogs, cats and cows are very specific. So:

    The base thing is Animals.
    The other things are dogs, cats and ducks.

    See this example:

    public abstract class Animal{
        public int LegsNumber { get; set; }
        public abstract void SoundLike();
    }
    
    public class Dog : Animal{
        public override void SoundLike(){
            Console.WriteLine("- The dog says: Go go go");
        }
    }
    
    public class Cat : Animal{
        public override void SoundLike(){
            Console.WriteLine("- The cat says: Meo meo meo");
        }
    }
    
    public class Duck : Animal{
        public override void SoundLike(){
            Console.WriteLine("- The duck says: Quak quak quak");
        }
    }
    
    //////////// 
    
    Animal cat = new Cat();
    Animal dog = new Dog();
    Animal duck = new Duck();
    
    cat.LegsNumber = 4;
    cat.SoundLike();
    Console.WriteLine("and it has " + cat.LegsNumber + " legs");
    
    dog.LegsNumber = 4;
    dog.SoundLike();
    Console.WriteLine("and it has " + dog.LegsNumber + " legs");
    
    duck.LegsNumber = 2;
    duck.SoundLike();
    Console.WriteLine("and it has " + duck.LegsNumber + " legs");

    And this is the result:

    - The cat says: Meo meo meo
    and it has 4 legs
    - The dog says: Go go go
    and it has 4 legs
    - The duck says: Quak quak quak
    and it has 2 legs

    You can see I created a base class is Animal and it is an abstract class. From this class, I also created three sub-classes are Dog, Cat and Duck. And these classes are inherited from Animal class. 

    Summary: Polymorphism means is from a base class you can create many other classes (sub-class) that have the same properties and behaviors but the result of properties and behaviors can be very different. 

    Question: Can polymorphism apply in the interface?
    Answer: No, a class implements NOT inherit an interface.

  • 9. Is C# support multiple inheritances? Explain why?

    Answer:

    No, because a class can not inherit from many classes in C#.

    Someone said YES, it can be by using the interface. Please note that a class implements an interface NOT inheritance.

  • 10. What is Constructor and why it is an important thing in OOP?

    Answer:

    Constructors are special methods in C# that are automatically called when an object of a class is created to initialize all the class data members. In C#, a class always has a default constructor with no parameter.

    The constructor of a class is important because it helps instance an object and can take many input data to help create flexible objects.

    An example of constructors:

    public class Student{
        public string Name { get; set; }
        public int Age { get; set; }
     
        public Student() // This is default constructor
        {
            this.Name = "";
            this.Age = 0;
        }
     
        public Student(string name, int age) // This is custom constructor
        {
            this.Name = name;
            this.Age = age;
        }
    }
  • 11. Explain Virtual and Abstract methods, What are they different?

    Answer:

    Virtual method

    Virtual Methods can appear in abstract and non-abstract classes.
    It is not necessary to override a virtual method in derived but it can be.
    The virtual method must have a body ....can be overridden by "override keyword".....
    Useful when all sub-classes methods have the same logic, no need to re-write many times.

    Abstract method

    Abstract Method resides in the abstract class and it has no body.
    Abstract Method must be overridden in non-abstract child class.
    Useful when all sub-classes methods have different logic

RATE