Object Oriented Programming Flashcards
What is Object Oriented Programming?
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).
What is a Class?
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.
What is an Object?
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.
What is the relation between Classes and Objects?
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.
Explain the General Concepts in building Object-oriented systems?
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).
What are abstract classes?
- 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.
What is a Interface?
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.
What’s the difference between abstract classes and interfaces?
Abstract classes can have concrete methods while interfaces have no methods implemented.
What is a delegate?
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.
What are Events?
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.
Do events have return type?
No
Can events have access modifiers?
By default, events are public but you can use the private and protected modifiers with events as well.
Can we have shared/static events?
Yes, events can be shared/static but only shared/static methods can raise shared/static events
What is shadowing?
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.
What is the difference between Shadowing and Overriding?
- 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.