Object-oriented programming is one of most important part of interviews.This post covers almost all the basic definitions that are asked in interviews.All the key terms with their definitions are as follows-
Object-Oriented Programming:
OOP is a programming technique for developing software solutions wherein the real world is represented in terms of objects. It is a real-world entity, in which data and function are hidden from outside the world the only object of the class can access the data and functionality of the class.
e.g: If the animal is the class then the dog is the object if a human is a class then man is the object. A dog has legs and eyes, then eyes are the variable in the technical concept, this is the property and the dog may run or may walk, these are methods, and the same concept we used in OOPS concept.
Advantage of object-oriented programming:
- Faster development: Reuse enables faster development.
- Improved software-development productivity
- Improved software maintainability
- Lower cost of development: The reuse of software also lowers the cost of development.
Disadvantages of object-oriented programming:
- Larger program size: Object-oriented programs typically involve more lines of code than procedural programs.
- Slower programs: Object-oriented programs are typically slower than procedure-based programs, as they typically require more instructions to be executed.
Characteristics of object-oriented programming:
Abstraction :
Abstraction is the concept of exposing only the required essential characteristics and behavior with respect to a context.
e.g. -Abstraction shows only important things to the user and hides the internal details, for example, when we ride a bike, we only know about how to ride bikes but can not know how it works? And also we do not know the internal functionality of a bike.
Abstraction is ATM Machine-
All are performing operations on the ATM machine like cash withdrawal, money transfer, retrieve mini-statement…etc. but we can't know internal details about ATM.
Encapsulation:
Data Hiding + Abstraction.
Prevents the data from unwanted access by binding of code and data in a single unit called object.
e.g. - Looking at the example of a power steering mechanism of a car. Power steering of a car is a complex system, which internally has lots of components tightly coupled together, they work synchronously to turn the car in the desired direction. It even controls the power delivered by the engine to the steering wheel. But to the external world, there is only one interface is available and rest of the complexity is hidden. Moreover, the steering unit in itself is complete and independent. It does not affect the functioning of any other mechanism.
Inheritance:
Reusability of the existing object. It is the process by which one object acquires the properties of another object.
Exceptions in inheritance-
- Constructors and Destructors are never inherited and hence never overrode.
- Assignment operator = is never inherited. It can be overloaded but can't be inherited by subclass.
e.g. Father gives his property to child, father got that properties from child’s grandfather, so child is the taker and father is giver, hence father works as a base class and child as derived class,
you can explain is that the child couldn’t give its property to father so Inheritance is one sided
Polymorphism:
The process of representing one form in multiple forms is known as Polymorphism.
e.g.
1. In an exam paper, each student writes a different answer to the same question(assuming question requires a descriptive answer).Here there is only one interface, a question in the exam paper. It has different behavior (answers) due to different objects (students) of similar/related type (human).
2. Suppose if you are in a classroom that time you behave like a student when you are in the market at that time you behave like a
customer, when you at your home at that time you behave like a son or daughter, Here one person present in different-different behaviors.
Types of Polymorphism:
Compile time Polymorphism:
Function overloading, operator overloading
Run Time Polymorphism:
Function overriding, virtual function
Summary:
OOPs have following features:
1. Object - Instance of Class
2. Class - Blueprint of Object
3. Encapsulation - Protecting our Data
4. Polymorphism - Different behaviors at different instances
5. Abstraction - Hiding our irrelevant Data
6. Inheritance - One property of object is acquiring to another property of object
Abstraction Vs encapsulation
- Abstraction is design oriented while encapsulation is implementation oriented.
- The focus of abstraction is on the interface i.e. the outside view of the object while encapsulation prevents other objects or methods from looking into the properties and behavior of that object.
Abstract Class:
Abstract Class is a class which contains at least one Pure Virtual function in it. Abstract classes are used to provide an Interface for its subclasses.
- An abstract class cannot be instantiated, but pointers and references of Abstract class type can be created.
- An abstract class can have normal functions and variables along with a pure virtual function.
- Abstract classes are mainly used for Upcasting so that its derived classes can use its interface.
- Classes inheriting an Abstract Class must implement all pure virtual functions, or else they will become Abstract too.
Virtual Function:
A virtual function is a member function of class and its functionality can be overridden in its derived class. This function can be implemented by using a keyword called virtual, and it can be given during function declaration.
A virtual function can be achieved in C++, and it can be achieved in C Language by using function pointers or pointers to function.
Pure Virtual Function:
A pure virtual function is a function which can be overridden in the derived class but cannot be defined. A virtual function can be declared as Pure by using the operator =0.
e.g.
Virtual void function1() // Virtual, Not pure
Virtual void function2() = 0 //Pure virtual
Friend Function:
A friend function is a friend of a class that is allowed to access to Public, private or protected data in that same class.
If the function is defined outside the class cannot access such information.Friend can be declared anywhere in the class declaration, and it cannot be affected by access control keywords like private, public or protected.
Overloading Vs Overriding:
Overriding involves the creation of two or more methods with the same name and same signature in different classes (one of them should be parent class and other should be a child).
Overloading is a concept of using a method at different places with the same name and different signatures within the same class.
Exception Handling:
Exception handling is a feature of OOP, to handle unresolved exceptions or errors produced at runtime.
e.g. try{
// code
}
catch(Exception e){}
Upcasting:
Upcasting is using the Super class's reference or pointer to refer to a Sub class's object.
e.g. Base *base = new Derived();
Constructor:
Constructors are special class functions which perform initialization of every object. The Compiler calls the Constructor whenever an object is created. Constructors initialize values to object members after storage is allocated to the object. Constructors are of three types :
- Default Constructor
- Parametrized Constructor
- Copy COnstructor
Destructor:
A destructor is a special class function which destroys the object as soon as the scope of an object ends. The destructor is called automatically by the compiler when the object goes out of scope.
Virtual Destructors:
Destructors in the Base class can be Virtual. Whenever Upcasting is done, Destructors of the Base class must be made virtual for proper destruction of the object when the program exits.
NOTE: Constructors are never Virtual, only Destructors can be Virtual.
Operator Overloading:
It is a type of polymorphism in which an operator is overloaded to give user defined meaning to it. Overloaded operator is used to perform the operation on user-defined data type.
Almost any operator can be overloaded in C++. However, there are few operators which can not be overloaded. The operators that are not overloaded are following:
- scope operator - ::
- sizeof
- member selector - .
- member pointer selector - *
- ternary operator - ?:
The disadvantage of Multiple Inheritance:
The most obvious problem is with function overriding.
Let's say have two classes A and B, both of which define a method "doSomething". Now you define a third class C, which inherits from both A and B, but you don't override the "doSomething" method.
When the compiler sees this code...
C c = new C();
c.doSomething();
which implementation of the method should it use? Without any further clarification, it's impossible for the compiler to resolve the ambiguity.
Very nice explanation regarding interview questions. Very helpful. Good job man!!
ReplyDeletegreat job.. well explained..!!!!!!!
ReplyDelete