Oops PDF
Oops PDF
Oops PDF
The programming languages that use and follow the Object-Oriented Programming
paradigm or OOPs, are known as Object-Oriented Programming languages. Some of
the major Object-Oriented Programming languages include:
Java
C++
Javascript
Python
PHP
And many more.
9. What is a class?
A class can be understood as a template or a blueprint, which contains some values,
known as member data or member, and some set of rules, known as behaviors or
functions. So when an object is created, it automatically takes the data and functions
that are defined in the class.
Therefore the class is basically a template or blueprint for objects. Also one can
create as many objects as they want based on a class.
For example, first, a car’s template is created. Then multiple units of car are created
based on that template.
10. What is an object?
An object refers to the instance of the class, which contains the instance of the
members and behaviors defined in the class template. In the real world, an object is
an actual entity to which a user interacts, whereas class is just the blueprint for that
object. So the objects consume space and have some characteristic behavior.
For example, a specific car.
11. What is encapsulation?
One can visualize Encapsulation as the method of putting everything that is required
to do the job, inside a capsule and presenting that capsule to the user. What it means
is that by Encapsulation, all the necessary data and methods are bind together and
all the unnecessary details are hidden to the normal user. So Encapsulation is the
process of binding data members and methods of a program together to do a specific
job, without revealing unnecessary details.
2) Data binding: Encapsulation is the process of binding the data members and the
methods together as a whole, as a class.
12. What is Polymorphism?
Polymorphism is composed of two words - “poly” which means “many”, and “morph”
which means “shapes”. Therefore Polymorphism refers to something that has many
shapes.
In OOPs, Polymorphism refers to the process by which some code, data, method, or
object behaves differently under different circumstances or contexts. Compile-time
polymorphism and Run time polymorphism are the two types of polymorphisms in
OOPs languages.
// In this program, we will see how multiple functions are created with the same name,
// but the compiler decides which function to call easily at the compile time itself.
class CompileTimePolymorphism{
// 1st method with name add
public int add(int x, int y){
return x+y;
}
// 2nd method with name add
public int add(int x, int y, int z){
return x+y+z;
}
// 3rd method with name add
public int add(double x, int y){
return (int)x+y;
}
// 4th method with name add
public int add(int x, double y){
return x+(int)y;
}
}
class Test{
public static void main(String[] args){
CompileTimePolymorphism demo=new CompileTimePolymorphism();
// In the below statement, the Compiler looks at the argument types and decides to c
System.out.println(demo.add(2,3));
// Similarly, in the below statement, the compiler calls method 2
System.out.println(demo.add(2,3,4));
// Similarly, in the below statement, the compiler calls method 4
System.out.println(demo.add(2,3.4));
// Similarly, in the below statement, the compiler calls method 3
System.out.println(demo.add(2.5,3));
}
}
In the above example, there are four versions of add methods. The first method takes
two parameters while the second one takes three. For the third and fourth methods,
there is a change of order of parameters. The compiler looks at the method signature
and decides which method to invoke for a particular method call at compile time.
class AnyVehicle{
public void move(){
System.out.println(“Any vehicle should move!!”);
}
}
class Bike extends AnyVehicle{
public void move(){
System.out.println(“Bike can move too!!”);
}
}
class Test{
public static void main(String[] args){
AnyVehicle vehicle = new Bike();
// In the above statement, as you can see, the object vehicle is of type AnyVehicle
// But the output of the below statement will be “Bike can move too!!”,
// because the actual implementation of object ‘vehicle’ is decided during runtime v
vehicle = new AnyVehicle();
// Now, the output of the below statement will be “Any vehicle should move!!”,
vehicle.move();
}
}
As the method to call is determined at runtime, as shown in the above code, this is
called runtime polymorphism.
16. What is Abstraction?
If you are a user, and you have a problem statement, you don't want to know how the
components of the so ware work, or how it's made. You only want to know how the
so ware solves your problem. Abstraction is the method of hiding unnecessary
details from the necessary ones. It is one of the main features of OOPs.
For example, consider a car. You only need to know how to run a car, and not how the
wires are connected inside it. This is obtained using Abstraction.
19. What is a constructor?
Constructors are special methods whose name is the same as the class name. The
constructors serve the special purpose of initializing the objects.
For example, suppose there is a class with the name “MyClass”, then when you
instantiate this class, you pass the syntax:
MyClass myClassObject = new MyClass();
Now here, the method called a er “new” keyword - MyClass(), is the constructor of
this class. This will help to instantiate the member data and methods and assign
them to the object myClassObject.
class ABC
{
int x;
ABC()
{
x = 0;
}
}
Parameterized constructor: The constructors that take some arguments are known
as parameterized constructors.
class ABC
{
int x;
ABC(int y)
{
x = y;
}
}
class ABC
{
int x;
ABC(int y)
{
x = y;
}
// Copy constructor
ABC(ABC abc)
{
x = abc.x;
}
}
22. What is a destructor?
Contrary to constructors, which initialize objects and specify space for them,
Destructors are also special methods. But destructors free up the resources and
memory occupied by an object. Destructors are automatically called when an object
is being destroyed.
27. What is a subclass?
The subclass is a part of Inheritance. The subclass is an entity, which inherits from
another class. It is also known as the child class.
28. Define a superclass?
Superclass is also a part of Inheritance. The superclass is an entity, which allows
subclasses or child classes to inherit from itself.
29. What is an interface?
An interface refers to a special type of class, which contains methods, but not their
definition. Only the declaration of methods is allowed inside an interface. To use an
interface, you cannot create objects. Instead, you need to implement that interface
and define the methods for their implementation.
Access specifiers, as the name suggests, are a special type of keywords, which are
used to control or specify the accessibility of entities like classes, methods, etc. Some
of the access specifiers or access modifiers include “private”, “public”, etc. These
access specifiers also play a very vital role in achieving Encapsulation - one of the
major features of OOPs.
37. What is an exception?
An exception can be considered as a special event, which is raised during the
execution of a program at runtime, that brings the execution to a halt. The reason for
the exception is mainly due to a position in the program, where the user wants to do
something for which the program is not specified, like undesirable input.
However, on the other hand, C++ can be implemented without OOPs, as it also
supports the C-like structural programming model.
#include<iostream>
class BaseClass2 {
public:
BaseClass2()
{ cout << "BaseClass2 constructor called" << endl; }
};
int main()
{
DerivedClass derived_class;
return 0;
}
Output:
Reason:
The above program demonstrates Multiple inheritances. So when the Derived class’s
constructor is called, it automatically calls the Base class's constructors from le to
right order of inheritance.
class Scaler
{
static int i;
static
{
System.out.println(“a”);
i = 100;
}
}
System.out.println(Scaler.i);
}
}
Output:
b
c
a
100
Reason:
Firstly the static block inside the main-method calling class will be implemented.
Hence ‘b’ will be printed first. Then the main method is called, and now the sequence
is kept as expected.
#include<iostream>
using namespace std;
class ClassA {
public:
ClassA(int ii = 0) : i(ii) {}
void show() { cout << "i = " << i << endl;}
private:
int i;
};
class ClassB {
public:
ClassB(int xx) : x(xx) {}
operator ClassA() const { return ClassA(x); }
private:
int x;
};
void g(ClassA a)
{ a.show(); }
int main() {
ClassB b(10);
g(b);
g(20);
getchar();
return 0;
}
Output:
i = 10
i = 20
Reason:
ClassA contains a conversion constructor. Due to this, the objects of ClassA can have
integer values. So the statement g(20) works. Also, ClassB has a conversion operator
overloaded. So the statement g(b) also works.
Output:
Main1
Reason:
Here the main() method is overloaded. But JVM only understands the main method
which has a String[] argument in its definition. Hence Main1 is printed and the
overloaded main method is ignored.
#include<iostream>
using namespace std;
class BaseClass{
int arr[10];
};
int main(void)
{
cout<<sizeof(DerivedClass);
return 0;
}
Output:
If the size of the integer is 4 bytes, then the output will be 80.
Reason:
Since DerivedBaseClass1 and DerivedBaseClass1 both inherit from class BaseClass,
DerivedClass contains two copies of BaseClass. Hence it results in wastage of space
and a large size output. It can be reduced with the help of a virtual base class.
#include<iostream>
class B : public A {
public:
void print()
{ cout <<" Inside B"; }
};
class C: public B {
};
int main(void)
{
C c;
c.print();
return 0;
}
Output:
Inside B
Reason:
The above program implements a Multi-level hierarchy. So the program is linearly
searched up until a matching function is found. Here, it is present in both classes A
and B. So class B’s print() method is called.
Useful Resource
Features of OOPS