46 terms

Object Oriented Programming

What is object-oriented programming (OOP)?
OOP is a technique to develop logical modules, such as classes that contain properties, methods, fields, and events. An object is created in the program to represent a class. Therefore, an object encapsulates all the features, such as data and behavior that are associated to a class. OOP allows developers to develop modular programs and assemble them as software. Objects are used to access data and behaviors of different software modules, such as classes, namespaces, and sharable assemblies. .NET Framework supports only OOP languages, such as Visual Basic .NET, Visual C#, and Visual C++.
Describes all the attributes of objects, as well as the methods that implement the behavior of member objects. It is a comprehensive data type, which represents a blue print of objects. It is a template of object.

It can be defined as the primary building block of OOP. It also serves as a template that describes the properties, state, and behaviors common to a particular group of objects.

It contains data and behavior of an entity. For example, the aircraft class can contain data, such as model number, category, and color and behavior, such as duration of flight, speed, and number of passengers.

It inherits the data members and behaviors of other classes by extending from them.
They are instance of classes. Attributes and behavior of an object are defined by the class definition.
What is the difference between procedural and object-oriented programming?
Procedural programming is based upon the modular approach in which the larger programs are broken into procedures. Each procedure is a set of instructions that are executed one after another.

On the other hand, OOP is based on objects. An object consists of various elements, such as methods and variables. Access modifiers are not used in procedural programming, which implies that the entire data can be accessed freely anywhere in the program. In OOP, you can specify the scope of a particular data by using access modifiers - public, private, internal, protected, and protected internal.
What is the relationship between a class and an object?
A class acts as a blue-print that defines the properties, states, and behaviors that are common to an object.
What are the basic features of OOP?
Abstraction, Polymorphism, Encapsulation, and Inheritance
Refers to the process of exposing only the relevant and essential data to the users without showing unnecessary information.
Allows you to use an entity in multiple forms.
Prevents the data from unwanted access by binding of code and data in a single unit called object.
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.
What is the difference between arrays and collection?
You need to specify the size of an array at the time of its declaration. It cannot be resized dynamically.
The members of an array should be of the same data type.

The size of a collection can be adjusted dynamically, as per the user's requirement. It does not have fixed size.
Collection can have elements of different types.
What are collections and generics?
A collection can be defined as a group of related items that can be referred to as a single unit. The System.Collections namespace provides you with many classes and interfaces. Some of them are - ArrayList, List, Stack, ICollection, IEnumerable, and IDictionary.

Generics provide the type-safety to your class at the compile time. While creating a data structure, you never need to specify the data type at the time of declaration. The System.Collections.Generic namespace contains all the generic collections.
How can you prevent your class to be inherited further?
By defining it with the sealed keyword.
What is the index value of the first element in an array?
0 (zero).
Can you specify the accessibility modifier for methods inside the interface?
No. All the methods inside an interface are always public by default.
Is it possible for a class to inherit the constructor of its base class?
No, a class cannot inherit the constructor of its base class.
How is method overriding different from method overloading?
Overriding involves the creation of two or more methods with the same name and same signature in different classes (one of them should be parent class and other should be child).

Overloading is a concept of using a method at different places with same name and different signatures within the same class.
What is the difference between a class and a structure?
A class is a reference type. While instantiating a class, CLR allocates memory for its instance in heap. Classes support inheritance. Variables of a class can be assigned as null. Class can contain constructor/destructor.

A structure is a value type. In structure, memory is allocated on stack. Structures do not support inheritance. Structure members cannot have null values. Structure does not require constructor/destructor and members can be initialized automatically.
What are similarities between a class and a structure?
Access specifiers, such as public, private, and protected, are identically used in structures and classes to restrict the access of their data and methods outside their body. The access level for class members and struct members, including nested classes and structs, is private by default. Private nested types are not accessible from outside the containing type. Both can have constructors, methods, properties, fields, constants, enumerations, events, and event handlers. Both structures and classes can implement interfaces to use multiple-inheritance in code. Both structures and classes can have constructors with parameter. Both structures and classes can have delegates and events.
Explain the concept of constructor?
Constructor is a special method of a class, which is called automatically when the instance of a class is created. It is created with the same name as the class and initializes all class members, whenever you access the class.

The main features of a constructor are as follows:
- Constructors do not have any return type.
- Constructors can be overloaded.
- It is not mandatory to declare a constructor; it is invoked automatically by .NET Framework.
Does .NET support multiple inheritance?
No because in .NET, a class cannot inherit from more than one class. .NET supports multiple inheritance through interfaces.
What is the syntax to inherit from a class in C#?
class MyNewClass : MyBaseclass
State the features of an interface.
An interface is a template that contains only the signature of methods. The signature of a method consists of the numbers of parameters, the type of parameter (value, reference, or output), and the order of parameters.

An interface has no implementation on its own because it contains only the definition of methods without any method body. An interface is defined using the interface keyword. Moreover, you cannot instantiate an interface.

The various features of an interface are as follows:
- An interface is used to implement multiple inheritance in code. This feature of an interface is quite different from that of abstract classes because a class cannot derive the features of more than one class but can easily implement multiple interfaces.
- It defines a specific set of methods and their arguments.
- Variables in interface must be declared as public, static, and final while methods must be public and abstract.
- A class implementing an interface must implement all of its methods.
- An interface can derive from more than one interface.
Do events have return type?
What are abstract classes? 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.
What are the different ways a method can be overloaded?
The ways to overload a method are given as follows:
- By changing the number of parameters used;
- By changing the order of parameters;
- By using different data types for the parameters
Differentiate between an abstract class and an interface.
Abstract Class:
- A class can extend only one abstract class. The members of abstract class can be private as well as protected.
- Abstract classes should have subclasses. Any class can extend an abstract class.
- Methods in abstract class can be abstract as well as concrete.
- There can be a constructor for abstract class.
- The class extending the abstract class may or may not implement any of its method.
- An abstract class can implement methods.

- A class can implement several interfaces An interface can only have public members.
- Interfaces must have implementations by classes
- Only an interface can extend another interface.
- All methods in an interface should be abstract.
- Interface does not have constructor.
- All methods of interface need to be implemented by a class implementing that interface.
- Interfaces cannot contain body of any of its method.
Whenever an action takes place in a class, that class provides a notification to other classes or objects that are assigned to perform particular tasks. These notifications are called events. For example, when a button is clicked, the class generates an event called Click. An event can be declared with the help of the event keyword.
When do you really need to create an abstract class?
We define abstract classes when we define a template that needs to be followed by all the derived classes.
A meaningful name that is given to a data storage location in the computer memory that contains a value.
Similar to a variable except that the value, which you assign to a constant, cannot be changed, as in case of a variable. Constants must be initialized at the same time they are declared.
Data type
A data storage format that can contain a specific type or range of values. Whenever you declare variables, each variable must be assigned a specific data type. There are value types and reference types.
Value Type
Refers to the data type that contains the data. In other words, the exact value or the data is directly stored in this data type. It means that when you assign a value type variable to another variable, then it copies the value rather than copying the reference of that variable. When you create a value type variable, a single space in memory is allocated to store the value (stack memory). Primitive data types, such as int, float, and char are examples of value type variables.
Reference type
Refers to a data type that can access data by reference. Reference is a value or an address that accesses a particular data by address, which is stored elsewhere in memory (heap memory). You can say that reference is the physical address of data, where the data is stored in memory or in the storage device. Some built-in reference types variables in .Net are string, array, and object.
What is the difference between constants and read-only variables that are used in programs?
Constants are dealt with at compile-time. Constants supports value-type variables. Constants should be used when it is very unlikely that the value will ever change. Read-only variables are evaluated at run-time. Read-only variables can hold reference type variables. Read-only variables should be used when run-time calculation is required.
Names given to various entities uniquely identified in a program. The name of identifiers must differ in spelling or casing. For example, MyProg and myProg are two different identifiers.
Words that are reserved in the programming language to be used for a specific task. These words cannot be used as identifiers. You cannot use a keyword to define the name of a variable or method. Keywords are used in programs to use the features of object-oriented programming. For example, the abstract keyword is used to implement abstraction.
Briefly explain the characteristics of reference-type variables that are supported in the C# programming language.
The variables that are based on reference types store references to the actual data. The keywords that are used to declare reference types are class, interface, and delegate.
Refers to the primary building block for the programs, which is used to encapsulate variables and methods into a single unit.
Contains only the signatures of methods, properties, events, or indexers.
Refers to a reference type that is used to encapsulate a named or anonymous method.
What are the different types of literals?
A literal is a textual representation of a particular value of a type.The different types of literals in C# are Boolean literals, integer literals, real literals, character literals, string literals, and null literals.
Boolean literals
Refers to the True and False literals that map to the true and false states, respectively.
Integer literals
Refers to literals that are used to write values of types int, uint, long, and ulong.
Real literals
Refers to literals that are used to write values of types float, double, and decimal.
Character literals
Represents a single character that usually consists of a character in quotes, such as 'a'.