OOP Flashcards
What is OOP?
OOP is a programming methodology that considers a program as consisting of objects that interact with each other by means of actions
What is an object?
An object is an identifiable entity that has a set of attributes, behaviour and state
Name the basic units of an object-oriented system
Objects
Define attributes
Attributes are individual characteristics that differentiate one object from another
Define behaviour
Behaviour is defined by the set of actions an object can perform
Define state
State is defined by the set of values held by its attributes
What are member variables or instance variables?
While implementing a real-world object in software form, all its attributes are represented through data items known as member variables or instance variables
What are member methods?
While implementing a real-world object in software form, all its behaviors are represented through data items known as member methods
In a software object, sum up the attribute+behavior+state
Member variables that hold the attributes
The values of these variables define the state of obj
Member methods that define the behavior of obj
What is a class?
A class is a template or blueprint for multiple objects with similar features and may be regarded as a specification for creating similar objects
Which objects are grouped together in a class?
Objects that share the same attributes and behavior are grouped together in a class
Why is a class called an object factory?
A class is a blueprint for multiple objects with similar features and may be regarded as a specification for creating similar objects
What do you need to do to create objects belonging to a class?
Once a class has been defined, you can create many objects belonging to that class
What is known as an instance of a class?
An object belonging to a particular class is known as an instance of a class
State the differences between a class and an object
- The way attributes and methods are treated in the class and the object
- The class is just a specification of the object. The attributes and methods in the class are thus declarations that do not contain any values. However, an object is a concrete instance of a class with properly defined values of each attribute and behaves as per the methods of a class
In one line, summarize the specification difference between object and class
The class is a logical construct whereas the object has a physical reality.
Name the four fundamental principles of OOP
- Abstraction
- Encapsulation
- Inheritance
- Polymorphism
Define abstraction
Abstraction refers to the act of representing essential features without including the background details
Define encapsulation
Encapsulation is a mechanism that binds the data and code (methods) together into a single unit.
How does encapsulation promote protection of data?
Encapsulation keeps the data and methods safe from the outside world, preventing any unauthorized access or misuse. Access to the data and code inside a class is strictly controlled. Only the methods inside the class can access the data.
In a camera, where do abstraction and encapsulation come into play?
The Capture button shows the essential feature and not the details - abstraction
The encapsulation helping to hide all the internal details of taking a photograph - opening the aperture, streaming light through lens, etc
How do objects encapsulate state and behavior?
- An object stores its state in member variables and exposes its behavior through member methods.
- The member methods operate on member variables and serve as the primary mechanism to interact with the object.
- Only the member methods which are defined inside the class can access the data and change its state.
- Hence, the state and behavior are said to be encapsulated by the object, hiding the internal state and requiring all interactions to be performed through the methods of the object.
How can the data of an object be accessed and its state changed?
Only the member methods which are defined inside the class can access its data and change its state
Define inheritance
Inheritance is a powerful mechanism by which one class acquires the property of another class
What is child class?
The class that is derived from another class
Give other names for child class
Derived class or sub class
What is parent class?
The class from which another class is derived is called the parent class
Give other names for parent class
Base class or super class
What does the word Polymorphism mean?
It means many forms. It is the ability of a method or object to take on multiple forms
Where does the word Polymorphism come from?
From the Greek word poly (many) and morphos (form)
Define polymorphism in terms of OOP
Polymorphism is a mechanism by which you can send the same message to different objects, and each object can respond in a different way depending on its class
In the statement ‘Student sumit;’ how does class as a user-defined data type come in?
The Student class may be considered as a data type after being defined, and an object as a variable of that data type. The statement will create a student object sumit belonging to the Student class
When does a class behave like built-in data types?
WashingMachine samsung; Once the WashingMachine class has been defined, the statement will create a samsung object belonging to the WashingMachine class. Hence, class is a user-defined data type, which has its own functionality and behaves like built-in data types
Define message passing
Objects interact with each other by passing messages to each other. This interaction is known as message passing.
What is a message?
A message is a request to an object to perform an action (behavior)
- MusicPlayer Alexa
- Alexa.PlaySong()
Define the significant parts of both statements
- The Alexa object belonging to the class MusicPlayer
2. Request to the Alexa object to invoke its member method PlaySong() using the dot(.) operator
What is the use of dot operator in message passing?
The dot operator is used between an object and its member method to invoke the member method
What is the message sending and receiving object called?
Message sending object - sender
Message receiving object - receiver
Name two objects between which message passing will take place.
Person objJoseph;
WashingMachine objSamsung;
What might the message look like between instance objJospeh and instance objSamsung?
objSamsung.StartWash()
Give the syntax of a message
ObjectName.MemberMethod()
Which terms in OOP are often interchangeable?
Object and instance
How many abstractions of an entity can there be?
Multiple abstractions, depending on the perspective of the viewer
How can you make the entire data and code contained in an object a user-defined data type?
Using the concept of a class. The class may now be considered as a data type and an object as a variable of that data type.
Which approach does OOP mainly use?
Bottom-up programming approach
Which approach does POP mainly use?
Top-down programming approach
POP places emphasis on?
instructions/functions
What is the POP paradigm?
It is a programming paradigm where a complex programming problem is solved by dividing it into smaller problems using procedures. Any procedure can be called by the main program as well as the other procedures
Explain the OOP paradigm
OOP is a programming paradigm that revolves around the behavior of an object, and its interactions with other objects and classes. OOP follows the design principle of data abstraction, Encapsulation, Inheritance, and Polymorphism
What does inheritance allow a class to do?
To use the attributes and behaviors of its parent class
In polymorphism, what does the behaviour of an operation depend on?
An operation is allowed to exhibit different behavior in different instances. The behavior depends upon the type of data used in the operation
Give a software example of OOP
For two integers, the operation of addition will generate a sum. For two strings, it will produce a third string by concatenation
Write two major differences between POP and OOP
- In POP, the real world is represented by procedures operating on data but in OOP, the real world is represented by objects and the operations which can be performed on these objects
- Data and functions are separate in POP, but in OOP, the data and functions are encapsulated into a single entity
Why is an object called an instance of a class?
Every object created from the class gets its own instance of member variables and methods defined in the class. Multiple objects can be created from the class
Why is a class known as a composite data type?
A class is composed of member variables that are of different data types. Hence, a class can be viewed as a composite data type.
What does a class encapsulate?
A class encapsulates the member variables and member methods
Give the characteristics of OOP
- Emphasis on data items rather than functions
- Makes program simpler by dividing into a no. of
objects - Objects can be used as a bridge to have data flow
from one function to another - The concept of data hiding enhances security in
problems
What are the limitations of OOP?
- Object-oriented programs typically involve more lines
of code than procedural programs - Object-oriented programs are typically slower than
procedure - In certain scenarios, these programs can consume
a large amount of memory.
Give the characteristics of POP
- Emphasis is on functions/methods
- Functions share global data
- Data values can keep floating from one function to
another - It uses the top-down approach of programming
What are the limitations of POP?
- As data values are global to all functions, you may
require to make necessary changes in all the
functions due to any change in the data values - It is not suitable to solve complex problems in real-
life situations
Code and data held together in POP or OOP?
Code and data are held together in OOP, and held separately in POP