Object Oriented Programming (OOP) Flashcards
What is Inheritance?
- It allows us to define a class in terms of another class, which makes it easier to create and maintain an application. This also provides an opportunity to reuse the code functionality and speeds up implementation time.
- When creating a class, instead of writing completely new data members and member functions, the programmer can designate that the new class should inherit the members of an existing class. This existing class is called the base class, and the new class is referred to as the derived class.
- The idea of inheritance implements the IS-A relationship. For example, mammal IS A animal, dog IS-A mammal hence dog IS-A animal as well, and so on.
What 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++.
What is Polymorphism?
The word polymorphism means having many forms. In object-oriented programming paradigm, polymorphism is often expressed as one interface, multiple functions.
What is Encapsulation?
Encapsulation is defined as the process of enclosing one or more items within a physical or logical package. Encapsulation, in object-oriented programming methodology, prevents access to implementation details.
Can you inherit private members of a class?
No, you cannot inherit private members of a class because private members are accessible only to that class and not outside that class.
Explain the concept of a 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.
What is an Object?
Objects are instances of classes. It is a basic unit of a 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 blueprint 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 is the difference between a class and a structure?
Class:
- A class is a reference type.
- While instantiating a class, CLR allocates memory for its instance in the heap.
- Classes support inheritance.
- Variables of a class can be assigned as null.
- Class can contain constructor/destructor.
Structure:
- A structure is a value type.
- In structure, memory is allocated on the stack.
- Structures do not support inheritance.
- Structure members cannot have null values.
- Structure does not require a constructor/destructor and members can be initialized automatically.
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 upon 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 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 blueprint of objects. It is a template of an object.
A class 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.
A class contains the 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 the flight, speed, and the number of passengers. A class inherits the data members and behaviors of other classes by extending from them.
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 code and data in a single unit called an 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.
Why is the virtual keyword used in code?
The 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.