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.
What is a class ?
A class 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.
what is an object ?
it’s an instance of the class, it’s the basic unit in the system. An object is an entity that has attributes, behavior, and identity. Attributes and behavior of an object are defined by the class definition.
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 a number of objects. An object is an instance of the class. For example, you have a class called Vehicle and Car is the object of that class. You can create any number of objects for the class named Vehicle, such as Van, Truck, and Auto.
The new operator is used to create an object of a class. When an object of a class is instantiated, the system allocates memory for every data member that is present in the class.
What are the basic concepts 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.
What is dynamic or run time polymorphism ?
It is also referred to as method overriding. Here, there can be two methods with same name and signature but different implementation.
The function here is resolved during run time rather than compile time.
What is Encapsulation ?
- It is a technique to hide the properties and behaviors of an object.
The access is provided only as required.
- It prevents other objects from altering or accessing the properties of an encapsulated object.
what is Inheritance ?
- It is the process which allows the objects of one class to acquire the properties of objects of another class.
- The class that inherits is called sub-class while the class from which the object is inherited is called super-class.
- Inheritance helps in re-using the code and polymorphism.
How can you prevent your class to be inherited further?
You can prevent a class from being inherited further by defining it with the sealed keyword.
Can you specify the accessibility modifier for methods inside the interface?
All the methods inside an interface are always public, by default. You cannot specify any other access modifier for them.
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 initialiazed automatically.
Why is the virtual keyword used in code?
he virtual keyword is used while defining a class to specify that the methods and the properties of that class can be overridden in derived classes.
Can you allow a class to be inherited, but prevent a method from being overridden in C#?
Yes. Just declare the class public and make the method sealed.
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.
Can you inherit private members of a class?
Does .NET support multiple inheritance?
NET does not support multiple inheritance directly because in .NET, a class cannot inherit from more than one class. .NET supports multiple inheritance through interfaces.
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 bepublic and abstract.
- A class implementing an interface must implement all of its methods.
- An interface can derive from more than one interface.
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.
Give a brief description of properties in C# and the advantages that are obtained by using them in programs.
Explain different types of inheritance.
Inheritance in OOP is of four types:
- Single inheritance – Contains one base class and one derived class
- Hierarchical inheritance – Contains one base class and multiple derived classes of the same base class
- Multilevel inheritance – Contains a class derived from a derived class
- Multiple inheritance – Contains several base classes and a derived class
All .NET languages supports single, hierarchical, and multilevel inheritance. They do not support multiple inheritance because in these languages, a derived class cannot have more than one base class. However, you can implement multiple inheritance in.NET through interfaces.
Explain the concept of destructor?
A destructor is a special method for a class and is invoked automatically when an object is finally destroyed. The name of the destructor is also same as that of the class but is followed by a prefix tilde (~).
A destructor is used to free the dynamic allocated memory and release the resources. You can, however, implement a custom method that allows you to control object destruction by calling the destructor.
The main features of a destructor are as follows:
- Destructors do not have any return type
- Similar to constructors, destructors are also always public
- Destructors cannot be overloaded.