0% found this document useful (0 votes)
13 views

OOPS

Uploaded by

suvasom024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

OOPS

Uploaded by

suvasom024
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

OOPS - OOPS is a programming concept that works on the principles of abstraction,

encapsulation, inheritance, and polymorphism. It allows users to create objects they want
and create methods to handle those objects.

The main aim of OOP is to bind together the data and the functions that operate on them so
that no other part of the code can access this data except that function.

For example, if we consider a car, then based on the OOPs model:

 Class = Vehicle
 Object = A specific car model, such as Audi A4, BMW I8, Maruti Suzuki Vitara
Brezza, etc.
 Characteristics = What is the color of your car? What is the Chassis number of
your car? etc
 Behavior = How to start the car? How to change the gear of the car? etc.

Object - An object is an instance of a class. It has its own state, behavior, and
identity. For example, a chair, pen, table. An object contains an address and takes
up some space in memory. Objects can communicate without knowing the details of
each other's data or code.

Class - A class is simply a representation of a type of object. It is a logical entity.


It is the blueprint/plan/template that create and describes the details of an object.
Class doesn't consume any space.

Inheritence - When one object acquires all the properties and behaviors of a
parent object, it is known as inheritance. It provides code reusability.

When creating a class, instead of writing completely new data members and member functions, the
programmer can designate that the new class should inherit the members of an existing class. This
existing class is called the base class, and the new class is referred to as the derived class.

The idea of inheritance implements the IS-A relationship. For example, mammal IS A animal, dog IS-A
mammal hence dog IS-A animal as well, and so on.
If Inheritance applied to one class is called Single Inheritance, and if it depends on
multiple classes, then it is called multiple Inheritance. It is used to achieve runtime
polymorphism.

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.

example of polymorphism: A person at the same time can have different characteristic. Like a man
at the same time is a father, a husband, an employee.
Compile time polymorphism, also known as Static Polymorphism, refers to the type of Polymorphism
that happens at compile time. What it means is the feature by which an object is linked with the
respective function or operator based on the values during the compile time. Static or Compile time
Polymorphism can be achieved through Method overloading or operator overloading.

Runtime polymorphism, also known as Dynamic Polymorphism, refers to the type of Polymorphism
in OOPs, by which the actual implementation of the function is decided during the runtime or
execution. The dynamic or runtime polymorphism can be achieved with the help of method
overriding.

Abstraction – Abstraction is a useful feature of OOPS, and it shows only the


necessary details to the client of an object. Meaning, it shows only required details
for an object, not the inner constructors, of an object . 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. we use abstract class and interface to achieve abstraction.

Encapsulation – Binding (or wrapping) code and data together into a single
unit are known as encapsulation. & also it is the process of hiding unwanted information,
such as restricting access to any member of an object. A car is having multiple parts..like
steering,wheels,engine...etc..which binds together to form a single object that is car. So, Here
multiple parts of cars encapsulates itself together to form a single object that is Car.

A java class is the example of encapsulation. Java bean is the fully encapsulated
class because all the data members are private here.

Constructor – Constructors are special methods whose name is the same as


the class name. The constructors serve the special purpose of initializing the objects .
Rules for constructor are:

 Constructor can be overloaded.


 A constructor do not have any return type.

Default constructor: The default constructor is the constructor which doesn’t take
any argument. It has no parameters.

Parameterized constructor: The constructors that take some arguments are known
as parameterized constructors.

Copy constructor: A copy constructor is a member function that initializes an object


using another object of the same class. What it means is that a copy constructor will clone an
object and its values, into another object, is provided that both the objects are of the same class.

Destructor – A destructor is a method which is automatically called when the


object is made of scope or destroyed. Destructor name is also same as class name
but with the tilde symbol before the name.

3) What is the diff b/w procedural and OOPS?


Procedural programming is based upon the modular approach in which the larger programs
are broken into procedures. Each procedure is a set of instructions that are executed one after
another. On the other hand, OOP is based upon objects. An object consists of various
elements, such as methods and variables.

Access modifiers are not used in procedural programming, which implies that the entire data
can be accessed freely anywhere in the program. In OOP, you can specify the scope of a
particular data by using access modifiers - public, private, internal, protected, and protected
internal.

16) What is an abstract class?

An abstract class is a class that cannot be instantiated and is always used as a base class.. The
significance of abstract class is that the abstract methods inside it are not
implemented and only declared. So as a result, when a subclass inherits the abstract
class and needs to use its abstract methods, they need to define and implement
them.

21) What is method overriding and method overloading?

Method overriding is a feature that allows a subclass to provide the


implementation of a method that overrides in the main class. It will
override the implementation in the superclass by providing the
same method name, same parameter, and same return type.

Method Overloading 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 child).

25) What is the main difference between overloading and


overriding?

Overloading is a compile-time polymorphism feature in which an entity has multiple


implementations with the same name. For example, Method overloading and
Operator overloading.

Whereas Overriding is a runtime polymorphism feature in which an entity has the


same name, but its implementation changes during execution. For example, Method
overriding.

22) 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.

Java uses Interface to implement multiple inheritances.

23) What is exception handling?


No one wants its software to fail or crash. Exceptions are the major reason for
software failure. The exceptions can be handled in the program beforehand and
prevent the execution from stopping. This is known as exception handling.
So exception handling is the mechanism for identifying the undesirable states that
the program can reach and specifying the desirable outcomes of such states.
Try-catch is the most common method used for handling exceptions in the program.

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.

26) What is the main difference between a class and an


object?

A class acts as a blue-print that defines the properties, states, and behaviors that are common
to a number of objects. An object is an instance of the class. For example, you have a class
called Vehicle and Car is the object of that class. You can create any number of objects for
the class named Vehicle, such as Van, Truck, and Auto.

The new operator is used to create an object of a class. When an object of a class is
instantiated, the system allocates memory for every data member that is present in the class.

A class can have sub-classes, while an object doesn’t have sub-


objects.

35) What is the difference between structure and a class?

Class:

 A class is a reference type. Such a variable does not contain an actual object, but rather a
reference to the class instance, or object, the variable refers to. Because using a class name
as a type declares a reference to an object.
 While instantiating a class, CLR(vm that execute a program) allocates memory for its
instance in heap(is a memory to store global variables).
 Classes support inheritance.
 Variables of a class can be assigned as null.
 Class can contain constructor/destructor.

Structure:

 A structure is a value type means When a value-type instance is created, a single space in
memory is allocated to store the value .
 In structure, memory is allocated on stack.
 Structures do not support inheritance.
 Structure members cannot have null values.
 Structure does not require constructor/destructor and members can be initialiazed
automatically.
28) What are the access modifiers?

Access specifiers are a special type of keywords, which are used to control or specify
the accessibility of entities like classes, methods, etc. These access specifiers also
play a very vital role in achieving Encapsulation - one of the major features of OOPs .
There are five types of access modifiers, and they are as follows:

 Private
 Protected
 Public
 Friend
 Protected Friend

Inline function – An inline function is a technique used by the


compilers and instructs to insert complete body of the function
wherever that function is used in the program source code

Virtual function – A virtual function is a member function of a


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.

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.

A friend can be declared anywhere in the class declaration, and it


cannot be affected by access control keywords like private, public,
or protected.

14) What is function overloading?

Function overloading is a regular function, but it can perform


different tasks. It allows the creation of several methods with the
same name which differ from each other by the type of input and
output of the function.

Example

1 void add(int& a, int& b);


2
3 void add(double& a, double& b);
4
5 void add(struct bob& a, struct bob& b);

15) What is operator overloading?

Operator overloading is a function where different operators are


applied and depends on the arguments. Operator,-,* can be used to
pass through the function, and it has its own precedence to execute

17) What is a ternary operator?

The ternary operator is said to be an operator which takes three


arguments. Arguments and results are of different data types, and it
depends on the function. The ternary operator is also called a
conditional operator.

18) What is the use of finalize method?

Finalize method helps to perform cleanup operations on the


resources which are not currently used. Finalize method is
protected, and it is accessible only through this class or by a derived
class.

19) What are the different types of arguments?

A parameter is a variable used during the declaration of the function


or subroutine, and arguments are passed to the function body, and
it should match with the parameter defined. There are two types of
Arguments.

 Call by Value – Value passed will get modified only inside the
function, and it returns the same value whatever it is passed
into the function.
 Call by Reference – Value passed will get modified in both
inside and outside the functions and it returns the same or
different value.

20) What is the super keyword?

The super keyword is used to invoke the overridden method, which


overrides one of its superclass methods. This keyword allows to
access overridden methods and also to access hidden members of
the superclass.

It also forwards a call from a constructor, to a constructor in the


superclass.
24) What are tokens?

A compiler recognizes a token, and it cannot be broken down into


component elements. Keywords, identifiers, constants, string
literals, and operators are examples of tokens.

Even punctuation characters are also considered as tokens.


Example: Brackets, Commas, Braces, and Parentheses.

30) How can we call the base method without creating an


instance?

Yes, it is possible to call the base method without creating an


instance. And that method should be “Static method.”

Doing Inheritance from that class.-Use Base Keyword from a derived


class.

31) What is the difference between new and override?

The new modifier instructs the compiler to use the new


implementation instead of the base class function. Whereas,
Override modifier helps to override the base class function.

33) What is early and late Binding?

Early binding refers to the assignment of values to variables during


design time, whereas late Binding refers to the assignment of values
to variables during run time.

34) What is ‘this’ pointer?

THIS pointer refers to the current object of a class. THIS keyword is


used as a pointer which differentiates between the current object
with the global object. It refers to the current object.
38) What are all the operators that cannot be overloaded?

Following are the operators that cannot be overloaded -.

1. Scope Resolution (::)


2. Member Selection (.)
3. Member selection through a pointer to function (.*)

39) What is dynamic or run time polymorphism?

Dynamic or Run time polymorphism is also known as method


overriding in which call to an overridden function is resolved during
run time, not at the compile time. It means having two or more
methods with the same name, same signature but with different
implementation.

41) What is a copy constructor?

This is a special constructor for creating a new object as a copy of


an existing object. There will always be only one copy constructor
that can be either defined by the user or the system.

44) What are a base class, subclass, and superclass?

The base class is the most generalized class, and it is said to be a


root class.

A Subclass is a class that inherits from one or more base classes.

The superclass is the parent class from which another class inherits.

45) What is static and dynamic Binding?

Binding is nothing but the association of a name with the class.


Static Binding is a binding in which name can be associated with the
class during compilation time, and it is also called as early Binding.

Dynamic Binding is a binding in which name can be associated with


the class during execution time, and it is also called as Late Binding.

46) How many instances can be created for an abstract


class?

Zero instances will be created for an abstract class. In other words,


you cannot create an instance of an Abstract Class.

46) Why the virtual keyword is used in code?


The virtual keyword is used while defining a class to specify that the methods and the
properties of that class can be overridden in derived classes

46) Can you specify the accessibility modifier for methods


inside interface?

All the methods inside an interface are always public, by default. You cannot specify any other
access modifier for them.

46) Constructors can inherited or not?

No, a class cannot inherit the constructor. Because Constructors are special and have same name as
class name. So if constructors were inherited in child class then child class would contain a parent
class constructor which is against the constraint that constructor should have same name as class
name.

Garbage collection – Garbage collection refers to this mechanism of


handling the memory in the program. Through garbage collection, the unwanted
memory is freed up by removing the objects that are no longer needed.

When should I use a struct instead of a class?


Do not define a structure unless the type has all of the following characteristics:

 It logically represents a single value, similar to primitive types (integer, double, and
so on).
 It has an instance size smaller than 16 bytes.
 It is immutable.
 It will not have to be boxed frequently.

Cohesion - In object oriented design, cohesion refers all about how a single class is
designed. Cohesion is the Object Oriented principle most closely associated with making sure
that a class is designed with a single, well-focused purpose.

Coupling - in object oriented design, Coupling refers to the degree of direct knowledge
that one element has of another.

Local Variable - Local variables are those variables present within a block,
function, or constructor and can be accessed only inside them. The
utilization of the variable is restricted to the block scope. Whenever a local
variable is declared inside a method, the other class methods don’t have
any knowledge about the local variable.
Global variable - Instance variables are those variables that are accessible
by all the methods in the class. They are declared outside the methods and
inside the class. These variables describe the properties of an object and
remain bound to it at any cost.

Try and catch block - The try statement allows you to define a block of code to be
tested for errors while it is being executed. The catch statement allows you to define
a block of code to be executed, if an error occurs in the try block.
Static keyword - the keyword static indicates that the particular member belongs to a
type itself, rather than to an instance of that type. This means that only one instance of
that static member is created which is shared across all instances of the class.

You might also like