Object Oriented Programming Flashcards

1
Q

What is Object Oriented Programming?

A

Object-oriented programming (OOP) is a programming paradigm that uses “objects” and their interactions to design applications and computer programs. Programming techniques may include features such as encapsulation, modularity, polymorphism, and inheritance. It was not commonly used in mainstream software application development until the early 1990s. Many modern programming languages now support OOP. Languages that support OOP are typically referred to as high level languages (C++, C#, Java) where languages that do not support OOP can be called low-level languages (Assembly, C).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a Class?

A

A class describes all the attributes of objects, as well as the methods that implement the behavior of object members. A class is a comprehensive data type, which represents a blue print of an object.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is an Object?

A

An object is an instance of classes. It’s a basic unit of a system. An object is an entity that has attributes, behavior, and identity. The attributes and behavior of an object are defined by the class definition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the relation between Classes and Objects?

A

A class is a definition, while an object is an instance of the class. A class is a blue print while objects are actual things existing in real world. For example, we have class named Car which has attributes like Make, Model, and Year and methods like Accelerate and Brake. The Car Class is just a prototype, but when we actually create the car (the object) it’s attributes are now readable and it’s methods are executable. When we instantiate the Car, we can ask it how old it is and we can tell it to speed up! This is the difference between a Class and an Object, one is a template for a thing and the other is the actual thing itself.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain the General Concepts in building Object-oriented systems?

A

Abstraction
Abstraction allows complex real world concepts to be represented in simplified manner. For example, a color is abstracted to RGB. By just making a combination of these three colors we can achieve any color in world. It’s a simplified model of real world or concept (making colors).

Encapsulation
Encapsulation is the process of hiding all the internal details of an object from the outside world.

Communication using messages
When an application wants to achieve a certain task, it needs to use a combination of objects. A single object can’t do the entire task on it’s own. For example, if we want to make an order-processing form we will need to use a Customer object, an Order object, a Product object, and a Payment object to achieve this the desired end result, which is an order form. The objects should communicate with each other and they do this by sending messages to one another.

Object lifetime
All objects have lifetime. Objects are created, and initialized, objects are used and then the object is destroyed. Every object has its own state and identity, which differ from instance to instance.

Class hierarchies (Inheritance and aggregation)
Alternate: - What's the difference between Association, Aggregation, and Inheritance relationships? The 3 Types of relationships between objects are:

Association This is the simplest relationship between objects. For example, a every recipe has ingredients and every ingredient has recipes. This relationship is known as an association.

Aggregation This is also called a composition model. For example, in order to make an Accounts class, you have to have a Voucher class, a Journal class, and a Cash class. The accounts class is an aggregation of these three objects.

Inheritance An inheritance hierarchy is used to define more specialized classes based on a preexisting classes. For example, a Toyota class may share a common ancestry with a Honda or Ford class in that they share many of the same properties and methods. They all have makes and models and they all can drive and brake. That shared functionality might reside in a base class called Car. However, each of them may have additional attributes and may be able to do things that only it can do. Even though a Viper has all the attributes and features of a Car, it can also drive in turbo-charge mode and it may have a spoiler however, it is still a car. The Viper inherits from the Car and the Car might inherit from a vehicle.

Polymorphism
In simple terms, polymorphism is the ability of one type, A, to appear as and be used like another type, B. In strongly typed languages, this usually means that type A somehow derives from type B, or type A implements an interface that represents type B.

In non-strongly typed languages (dynamically typed languages) types are implicitly polymorphic to the extent they have similar features (fields, methods, operators). In fact, this is one of the principal benefits (and pitfalls) of dynamic typing.

The primary usage of polymorphism in industry (object-oriented programming theory) is the ability of objects belonging to different types to respond to method, field, or property calls of the same name, each one according to an appropriate type-specific behavior. The programmer (and the program) does not have to know the exact type of the object in advance, and so the exact behavior is determined at run time (this is called late binding or dynamic binding).

The different objects involved only need to present a compatible interface to the clients (the calling routines). That is, there must be public or internal methods, fields, events, and properties with the same name and the same parameter sets in all the Superclasses, Subclasses, and potentially Interfaces. In principle, the object types may be unrelated, but since they share a common interface, they are often implemented as Subclasses of the same Superclass. Though it is not required, it is understood that the different methods will also produce similar results (for example, returning values of the same type).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are abstract classes?

A
  • You cannot instantiate an Abstract class directly.
  • Abstract classes are designed to act as a base class (to be inherited by other classes).
  • Abstract classes are similar to interfaces. After declaring an abstract class, it cannot be instantiated on its own, it must be inherited.
  • In VB.NET, abstract classes are created using the MustInherit keyword. In C# we use the Abstract keyword.
  • Abstract classes can have concrete or abstract methods, which should be implemented in the child class.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is a Interface?

A

Interface generally refers to an abstraction that an entity provides of itself to the outside. This separates the methods of external communication from internal operation, and allows it to be internally modified without affecting the way outside entities interact with it, as well as provide multiple abstractions of itself. It may also provide a means of translation between entities which do not speak the same language, such as between a human and a computer.

  • A single class can implement multiple interfaces.
  • If a class implements a interface then it has to implement all its methods as well.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What’s the difference between abstract classes and interfaces?

A

Abstract classes can have concrete methods while interfaces have no methods implemented.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is a delegate?

A

A Delegate is a class that can hold a reference to a method or a function. Delegate classes have a signature and can only reference those methods whose signature is compliant with the class. Delegates are type-safe functions pointers or callbacks.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are Events?

A

As compared to delegates, events work with the source and listener methodology. Listeners subscribe to the source in order to receive events. One source can have multiple listeners. The source generates an event and the listeners receive the event.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Do events have return type?

A

No

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Can events have access modifiers?

A

By default, events are public but you can use the private and protected modifiers with events as well.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can we have shared/static events?

A

Yes, events can be shared/static but only shared/static methods can raise shared/static events

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What is shadowing?

A

When two elements in a program have the same name, one of them can hide and shadow the other one. In these cases the element, which shadowed the main element, is referenced.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the difference between Shadowing and Overriding?

A
  • Overriding redefines only the implementation while shadowing redefines the whole element.
  • Overrided derived classes are referred by the parent with the “ME” or “this” keyword, but shadowed classes are accessed by using the “MYBASE” keyword.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the difference between delegate and events?

A
  • Events use delegates but they add an extra layer on the delegates, which forms the publisher and subscriber model.
  • As delegates are functions to pointers, they can move across clients. Any of the clients can add or remove events.
17
Q

If we inherit a class do the private variables also get inherited?

A

Yes, but they still cannot be accessed publicly.

18
Q

What is the different accessibility levels defined in .NET?

A
  • Private: Only members of the class have access.
  • Protected:- All members in the class and all derived classes have access.
  • Internal (Friend in VB):- Only members in the current project have access.
  • Protected Internal (Protected friend in VB):- All members in the current project and all members in derived classes have access.
  • Public: - All members in all classes and projects have access.
19
Q

Can you prevent a class from overriding?

A

Mark it sealed (C#) or NotInheritable (VB)

20
Q

Do interfaces have accessibility modifier?

A

No, All elements in an Interface are public.

21
Q

What are some similarities between Classes and structures?

A
  • Both can have constructors, methods, properties, fields, constants, enumerations, events, and event handlers.
  • Structures and classes can implement interface.
  • Both of them can have constructors with and without parameter.
  • Both can have delegates and events.
22
Q

What are some differences between Classes and structures?

A
  • Structures are value types and classes are reference types. So structures use stack and classes use heap.
  • Struct members cannot be declared as protected, but class members can. You cannot use inheritance in structures.
  • Structures do not require constructors while classes do.
  • Objects created from classes are terminated using the Garbage collector. Structures are not destroyed by the GC.
23
Q

What does virtual keyword mean?

A

The element is overrideable.

24
Q

What are shared (VB.NET)/Static(C#) variables?

A

Static elements are elements that need to be shared between objects without requiring an explicit reference to the object.

Below are some properties of static (sometimes called helper) classes:

  • Static/Shared classes cannot be instantiated.
  • Static/Shared classes cannot be inherited.
  • Static/Shared classes can have only static members.
  • Static/Shared classes can have only static constructors.
25
Q

What is Dispose method in .NET?

A

As covered several times throughout this wiki, Dispose originates by implementing the IDisposable interface. The Dispose method executes all the cleanup routines needed to release un-used object references from memory.

26
Q

Explain the use of “Overrides” and “Overridable” keywords?

A

Overridable is used in parent class to indicate that a method can be overridden. Overrides is used in the child class to indicate that you are overriding a method.

27
Q

Where are all .NET Collection classes located?

A

System.Collections

28
Q

What are queues and stacks?

A

A Queue is for first-in, first-out (FIFO) structures. A Stack is for last in, first-out (LIFO) structures.

29
Q

What is ENUM?

A

Used for defining logical structures for constants.

30
Q

What is nested Classes?

A

A class within a class.

31
Q

What is Operator overloading in .NET?

A

It provides a way to define and use operators such as +, -, and / for user-defined classes or structs. It allows us to define/redefine the way operators work with our classes and structs. This allows programmers to make their custom types look and feel like simple types such as int and string.

32
Q

What is the significance of Finalize method in .NET?

A

The finalize method provides a way for us to wrap classes using un-managed resources and clean up their memory usage. Finalize does not call GC.Collect()

33
Q

How can we suppress a finalize method?

A

GC.SuppressFinalize();

34
Q

Explain the use of a DISPOSE method?

A

Again, Dispose implements the IDisposable interface and provides a way to tell the runtime environment that we’re done using an object and it’s safe to release its reference from memory.

35
Q

How do I force the Dispose method to be called automatically?

A

Wrap logic in with the using keyword so when the calling routine is done, the entity is marked for cleanup.

36
Q

In what instances you will declare a constructor to be private?

A

Private constructors are used when you don’t want clients to be able to create instances of classes. For example, in static or helper classes you don’t want the user to be able to create instances of the class.

37
Q

If we write a return statement in try and catch block will the finally block execute?

A

if you use a return statement in a try block the code in the finally block will be executed prior to the return logic.

38
Q

Can two catch blocks be executed?

A

No. Once the correct catch block is executed control of execution is handed to the Finally block. You can define multiple catches but only the first one will execute

39
Q

What is the difference between System.String and System.StringBuilder classes?

A

System.String is immutable; which means that any changes require a re-allocation of memory. System.StringBuilder types are mutable and provide various functions for manipulating strings in a memory efficient manner.