Concept of OOPS in C#

What are the Access Modifiers in C# ?

Different Access Modifier are – Public, Private, Protected, Internal, Protected Internal

  • Public – When a method or attribute is defined as Public , It can be accessed from any code in project. For example in the above Class “Employee” , getName(), setName() etc are public.
  • Private – When a method or attribute is defined as Private , It can be accessed by any code within the containing type only. For example in the above Class “Employee” , attributes name and salary can be accessed with in the Class Employee Only. If an attribute or class are defined without access modifiers , its default access modifier will be private.
  • Protected – When an attribute and methods are defined as protected, it can be accessed by any method in inherited classes and any method within the same class. The protected access modifier cannot be applied to class and interfaces. Methods and fields in a interface cannot be declared protected.
  • Internal – If an attribute or method is defined as Internal , Access is restricted to classes within the current project assembly
  • Protected Internal – If an attribute or method is defined as Protected Internal , Access is restricted to classes within the current project assembly and types derived from the containing class.

Define Static Members in C# ?

If an attributes value had to be same across all the instances of the same class , static keyword is used. For example if the Minimum salary should be set for all employees in the employee class, use the following code.

Define Reference Types in C# ?

Let us explain this with an example . For the code given below,

Here emp2 has an object instance of Employee Class . But emp1 object is set as emp2. What this means is that object emp2 is refereed in emp1 and not that emp2 is copied into emp1. When a change is made in emp2 object, corresponding changes can be seen in emp1 object.

Explain the basic features of OOPs.

The following are the four basic features of OOP:

  • Abstraction – Refers to the process of exposing only the relevant and essential data to the users without showing unnecessary information.
  • Polymorphism – Allows you to use an entity in multiple forms.
  • Encapsulation – Prevents the data from unwanted access by binding of code and data in a single unit called object.
  • Inheritance – Promotes the reusability of code and eliminates the use of redundant code. It is the property through which a child class obtains all the features defined in its parent class. When a class inherits the common properties of another class, the class inheriting the properties is called a derived class and the class that allows inheritance of its common properties is called a base class.

Define Overloading in C# ?

When methods are created with same name , but with different signature its called overloading.  For example , WriteLine method in console class is an example for overloading. In first instance , it takes one variable. In the second instance , “WriteLine” method takes two variable.

Different type of overloading in C# are

  1. Constructor overloading
  2. Function overloading
  3. Operator overloading

Define Constructor Overloading in C# .net ?

In Constructor overloading, n number of constructors can be created for same class. But the signatures of each constructor should vary. For example

Define Function Overloading in C# .net ?

In Function overloading, n number of functions can be created for same class. But the signatures of each function should vary. For example

Define Operator Overloading in C# .net ?

We had seen function overloading in the previous example.For operator Overloading , we will have look at the example below. We define a class rectangle with two operator overloading methods.

Let us call the operator overloaded functions from the method below. When first if condition is triggered, first overloaded function in the rectangle class will be triggered. When second if condition is triggered, second overloaded function in the rectangle class will be triggered.

Define Data Encapsulation in c# ?

Data Encapsulation is defined as the process of hiding the important fields from the end user. In the above example , we had used getters and setters to set value for MinSalary. Idea behind this is that , private field “minimumSalary” is an important part of our classes. So if we give a third party code to have complete control over the field without any validation it can adversely affect the functionality. This is in line with OOPS Concept that an external user should know about the what an object does. How it does it should be decided by the program. So if a user set a negative value for MinSalary , we can put a validation in set method to avoid negative values as shown below

Define Inheritance in C# ?

In object-oriented programming (OOP), inheritance is a way to reuse code of existing objects. In inheritance there will be two classes – base class and derived classes . A class can inherit attributes and methods from existing class called base class or parent class. The class which inherits from base class is called derived classes or child class. For more clarity on this topic , let us have a look at 2 classes shown below. Here Class Car is Base Class and Class Ford is derived class.

Output Generated is given below.

What this means is that , all the methods and attributes of Base Class car is available in Derived Class Ford. When an object of class Ford is created , constructors of the Base and Derived class get invoked. Even though there is no method called DriveType() in Class Ford, we are able to invoke the method because of inheriting Base Class methods to derived class.

Define Multiple Inheritance in C# ?

In C# , derived classes can inherit from only one base class. If you want inherit multiple base classes, use interface.

What is Polymorphism in C# ?

Ability of a programming language to process objects in different ways depending on their data type or class is known as Polymorphism. There are two types of polymorphism

1) Compile time polymorphism. Best example is Overloading

2) Runtime polymorphism. Best example is Overriding

Define Virtual Keyword in C# ?

When we want to give permission to derived class to override a method in base class, Virtual keyword is used . For example lets us look at the classes Car and Ford as shown below.

When following lines of code get executed

Output is as given below.

Define overriding in c# ?

To override a base class method which is defined as virtual , Override keyword is used. In the above example , method DriveType is overrided in derived class.

Define Method Hiding in C# ?

If the derived class doesn’t want to use methods in base class , derived class can implement the same method in derived class with same signature. For example in the classes given below, DriveType() is implemented in the derived class with same signature. This is called Method Hiding.

Define Abstract Class in C#?

If we don’t want a class object to be created define the class as abstract. An abstract class can have abstract and non abstract classes. If a method in abstract id defined as abstract , it must be implemented in derived class. For example , in the classes given below , method DriveType is defined as abstract.

Method DriveType get implemented in derived class.

What are the distinct characteristics of an abstract class?

An abstract class is a class that cannot be instantiated and is always used as a base class.
The following are the characteristics of an abstract class:

  • You cannot instantiate an abstract class directly. This implies that you cannot create an object of the abstract class; it must be inherited.
  • You can have abstract as well as non-abstract members in an abstract class.
  • You must declare at least one abstract method in the abstract class.
  • An abstract class is always public.
  • An abstract class is declared using the abstract keyword.

The basic purpose of an abstract class is to provide a common definition of the base class that multiple derived classes can share.

Define Sealed Classes in c# ?

If a class is defined as Sealed it cannot be inherited in derived class. Example is given below.

Define Interfaces in C# ?

An interface is similar to a class with method signatures. There wont be any implementation of the methods in Interface. Classes which implements interface should have implementation of methods defined in abstract class.

Define Constructor in C# ?

Constructor is a special method that get invoked / called automatically whenever an object of a given class gets instantiated. For example in our class car constructor is defined as shown below

When ever an instance of class car is created from the same class or its derived class(Except Few Scenarios) Constructor get called and sequence of code written in the constructor get executed.

Define Destructor in C# ?

Destructor is a special method that get invoked / called automatically whenever an object of a given class gets destroyed. Main idea behind using destructor is to free the memory used by the object.

Differentiate between an abstract class and an interface.

Abstract Class:

  1. A class can extend only one abstract class
  2. The members of abstract class can be private as well as protected.
  3. Abstract classes should have subclasses
  4. Any class can extend an abstract class.
  5. Methods in abstract class can be abstract as well as concrete.
  6. There can be a constructor for abstract class.
  7. The class extending the abstract class may or may not implement any of its method.
  8. An abstract class can implement methods.

Interface

  1. A class can implement several interfaces
  2. An interface can only have public members.
  3. Interfaces must have implementations by classes
  4. Only an interface can extend another interface.
  5. All methods in an interface should be abstract
  6. Interface does not have constructor.
  7. All methods of interface need to be implemented by a class implementing that interface.
  8. Interfaces cannot contain body of any of its method.

Similar Topics:

We will send you more information on Concept of OOPS in C#.
Enter your email address:
This entry was posted in .Net and tagged . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *