Batch 8
Batch 8
Basic OOP
*What is OOP?
OOPs refers to Object-Oriented Programming. It is the programming technique that is
defined using objects. Objects can be considered as real-world instances of entities like class,
that have some characteristics and behaviours.
1
rules, and has a definitive control flow, such as (if/then/else), (while and for), block structures,
and subroutines.
Nearly all programming paradigms include Structured programming, including the OOPs
model.
*What is Class?
A class is a prototype that consists of objects in different states and with different
behaviours. It has a number of methods that are common the objects present within that class.
*What is Object?
An object is an instance of a class. It has its own state, behaviour, and identity.
*Can you call the base class method without creating an instance?
Yes, you can call the base class without instantiating it if:
• It is a static method
• The base class is inherited by some other subclass
Inheritance
2
*What is Inheritance?
Inheritance is a concept where one class shares the structure and behaviour defined in
another class. If Inheritance applied to one class is called Single Inheritance, and if it depends
on multiple classes, then it is called multiple Inheritance.
Inheritance is a feature of OOPs which allows classes inherit common properties from
other classes. For example, if there is a class such as ‘vehicle’, other classes like ‘car’, ‘bike’,
etc can inherit common properties from the vehicle class. This property helps you get rid of
redundant code thereby reducing the overall size of the code.
3
Hierarchical inheritance refers to inheritance where one base class has more than one
subclasses. For example, the vehicle class can have ‘car’, ‘bike’, etc as its subclasses.
Yes, with more powers comes more complications. Inheritance is a very powerful feature
in OOPs, but it has some limitations too. Inheritance needs more time to process, as it needs to
navigate through multiple classes for its implementation. Also, the classes involved in
Inheritance - the base class and the child class, are very tightly coupled together. So, if one
needs to make some changes, they might need to do nested changes in both classes. Inheritance
might be complex for implementation, as well. So, if not correctly implemented, this might
lead to unexpected errors or incorrect outputs.
*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.
*What is composition?
Composition is one of the vital concepts in OOP. It describes a class that references one
or more objects of other classes in instance variables. It allows us to model a has-a association
between objects. We can find such relationships in the real world.
4
*What is the difference between Composition and Inheritance?
Inheritance means an object inheriting reusable properties of the base class.
Compositions mean that an object holds other objects. In Inheritance, there is only one object
in memory (derived object) whereas, in Composition, the parent object holds references of all
composed objects. From a design perspective, inheritance is "is a" relationship among objects
whereas Composition is "has a" relationship among objects.
Polymorphism
*What is polymorphism?
Polymorphism refers to the ability to exist in multiple forms. Multiple definitions can
be given to a single interface. For example, if you have a class named Vehicle, it can have a
method named speed, but you cannot define it because different vehicles have different speed.
This method will be defined in the subclasses with different definitions for different vehicles.
5
*What is operator overloading?
Operator overloading refers to implementing operators using user-defined types based
on the arguments passed along with it.
Overloading Overriding
Two or more methods having the same name Child class redefining methods present in the
but different parameters or signature in a base class with the same parameters/
same class. signature.
Resolved during compile-time. Resolved during runtime.
Encapsulation
*What is encapsulation?
Encapsulation refers to binding the data and the code that works on that together in a
single unit. For example, a class. Encapsulation also allows data-hiding as the data specified in
one class is hidden from other classes.
Encapsulation is an attribute of an object, and it contains all data which is hidden. That
hidden data can be restricted to the members of that class.
6
Levels are Public, Protected, Private, Internal, and Protected Internal.
Data Abstraction
*What is data abstraction?
Data abstraction is a very important feature of OOPs that allows displaying only the
important information and hiding the implementation details. For example, while riding a bike,
you know that if you raise the accelerator, the speed will increase, but you don’t know how it
actually happens. This is data abstraction as the implementation details are hidden from the
rider.
7
No. Instances of an abstract class cannot be created because it does not have a complete
implementation. However, instances of subclass inheriting the abstract class can be created.
*What is an interface?
It is a concept of OOPs that allows you to declare methods without defining them.
Interfaces, unlike classes, are not blueprints because they do not contain detailed instructions
or actions to be performed. Any class that implements an interface defines the methods of the
interfaces.
*What is a constructor?
A constructor is a special type of method that has the same name as the class and is
used to initialize objects of that class and has no return type.
*What is a destructor?
A destructor is a method that is automatically invoked when an object is destroyed. The
destructor also recovers the heap space that was allocated to the destroyed object, closes the
files and database connections of the object, etc.
8
*Types of constructors ?
Types of constructor is differed from language to language. However, all the possible
constructors are:
• Default constructor
• Parameterized constructor
• Copy constructor
• Static constructor
• Private constructor
9
*What are manipulators?
Manipulators are the functions which can be used in conjunction with the insertion (<<)
and extraction (>>) operators on an object. Examples are endl and setw.
Exception Handling
*What is an exception?
An exception is a kind of notification that interrupts the normal execution of a program.
Exceptions provide a pattern to the error and transfer the error to the exception handler to
resolve it. The state of the program is saved as soon as an exception is raised.
10