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

OOPs Interview Question

The document provides an overview of Object-Oriented Programming (OOP) concepts including classes, objects, inheritance, polymorphism, abstraction, encapsulation, access modifiers, constructors, and exception handling. It explains key principles such as code reusability, method overloading, and method overriding, along with the differences between abstraction and encapsulation. Additionally, it discusses the use of keywords like 'this', 'super', and 'final' in Java, as well as the importance of exception handling through try/catch blocks.

Uploaded by

terrabox144
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

OOPs Interview Question

The document provides an overview of Object-Oriented Programming (OOP) concepts including classes, objects, inheritance, polymorphism, abstraction, encapsulation, access modifiers, constructors, and exception handling. It explains key principles such as code reusability, method overloading, and method overriding, along with the differences between abstraction and encapsulation. Additionally, it discusses the use of keywords like 'this', 'super', and 'final' in Java, as well as the importance of exception handling through try/catch blocks.

Uploaded by

terrabox144
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

OOPs (Object Oriented Programming) Interview Questions:

OOPs mainly focuses on the objects that are required to be manipulated instead
of logic. This approach is ideal for the programs large and complex codes and
needs to be actively updated or maintained.
Object: Object is a instance of a class. A class can be defined that describe
behaviour of the object.
Class: A class is a group of objects which have common properties. it is a
template or blueprint from which objects are created.
Inheritance:
Inheritance is inheriting the properties of the parent class into child class. And
Inheritance represents the IS-A relationship which is also known as a parent-
child relationship.
For exp: Dog IS-A Animal, Surgeons IS-A Doctor
Advantage:
 Code Reusability
 It promotes runtime polymorphism by allowing overriding
Disadvantage:
 Using inheritance, the two classes (parents and child class) tightly
coupled.
Types of Inheritance:
1) Single Inheritance: In single inheritance one class A extends another
class B.
2) Multilevel Inheritance: In Multilevel Inheritance, one class can be
inherit from a derived class. For exp: Class A inherit class B and class B
inherit class C.
3) Hierarchical Inheritance: In hieratical Inheritance One class is inherited
by many sub class. For Exp: class A inherit Class A and Class C.
4) Multiple Inheritance: In multiple Inheritance One class extending more
than one class. But java does not Support multiple inheritance. because it
leads to deadly diamond problem, Java supports multiple inheritance
through interfaces only. A class can implement any number of interfaces
but can extend only one class.
5) Hybrid Inheritance: Hybrid inheritance is a combination of any two
inheritances. Java does not support hybrid inheritance.
Polymorphism:
In Polymorphism we can perform a single action in different ways. and also,
polymorphism means many forms. There are two types of polymorphism
compile-time polymorphism and runtime polymorphism. We can perform
polymorphism by method overloading and method overriding.
For exp: -
1) One person has different behaviour. For example, a person acts as an
employee in the office, a customer in the shopping mall, a passenger in
bus/train, a student in school, and a son at home.
Method Overloading (Compile-time polymorphism-):
In Method Overloading, a class has more than one method of the same name
and their parameter are different. Its also known a static polymorphism.
Method Overriding (Run-time polymorphism):
In method Overriding the child class has the same method with the same name
and exactly the same number of parameter and same return type. It’s also
known as dynamic polymorphism. In run-time polymorphism we used
inheritance.
Can you override a private or static method in Java?
You cannot override a private or static method. If you create a similar method
with the same return type and same method arguments in child class then it will
hide the superclass method this is known as method hiding.
Automatic Promotion:
One type is promoted to another implicitly if no matching datatype is found.
Abstraction:
Abstraction is the methodology of hiding the implementation details from the
user and only providing functionality to the user.
For Exp: -
1) when we ride a bike, we only know about how to ride bikes but cannot
know about how it works? And also, we do not know the internal
functionality of a bike.
2) 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.
How to achieve data abstraction?
Data abstraction can be achieved Abstract class and interface.
What is an abstract class?
abstract class is a class that consists of abstract methods. These methods are
basically declared but not defined. If these methods are to be used in some
subclass, they need to be exclusively defined in the subclass.
Can you create an instance of an abstract class?
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.
Interface:
Interface are similar to abstraction class but having all methods of abstract type.
or interface are the blueprint of the class it specifies what a class must do and
how. And it is used to achieve abstraction also support multiple inheritance.
Difference between Abstraction & Encapsulation:
1. Abstraction is details hiding 1. Encapsulation is data hiding
(Implementation hiding) (Information hiding)
2. Data abstraction deals with 2. Encapsulation groups together
Exposing the interface to the data and methods that act upon
User and hiding the details of the data.
Implementation
Encapsulation:
wrapping the data (variable) and code acting on the data (methods) together as a
single unit. It is to make that sensitive data hidden from user.
For exp: -
1) The common example of encapsulation is capsule. In capsule all
medicine is encapsulated in side capsule.
2) School bag is one of the most real examples of Encapsulation. School bag
can keep our books, pens, etc.
Steps to achieve encapsulation:
1. Declare the variable of a class as private.
2. Provide public setter and getter method to modify and view the variable
values.
Access modifiers:
Access modifiers set the accessibility of classes, methods, and other members.
Access modifiers are a specific part of programming language of encapsulation.
Four type of access modifiers used:
1. Public: It can access within class, outside the class, within the package
and outside the package.
2. Private: code is accessible within the declaration of class. it cannot be
access from outside the class.
3. Protected: The code is accessible in the same package and child class in
any outside package.
4. Default: The code is only accessible in the same package.
Constructor:
Constructor used to initialize an object. It must have the same name as that of
the class. It has no return type and it is automatically called when an object is
created.
Different type of constructor used:
1. Default: Default constructor are the no argument constructor. Its main
purpose to initialize the instance variable with the default value.
2. Parametrized: Parametrized constructor initialize the instance variable
with the provided values. And Its take arguments are called parametrized
constructor.
3. Copy Constructor: In java no copy constructor but we can copy the value
one object to other in c++ by copy constructor.
destructor?
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.
This keyword:
This keyword is the reference variable that refers to the current object.
Usage of Java this keyword
 this can be used to refer current class instance variable.
 this can be used to invoke current class method (implicitly)
 this() can be used to invoke current class constructor.
 this can be passed as an argument in the method call.
 this can be passed as argument in the constructor call.
 this can be used to return the current class instance from the method.
Super Keyword:
Super keyword is a reference variable which is used to refer immediate parent
class object.
Usage of Java super Keyword
 super can be used to refer immediate parent class instance variable.
 super can be used to invoke immediate parent class method.
 super() can be used to invoke immediate parent class constructor.

Final Keyword:
final keyword is used in different contexts.
1. Final variable: if we create final variable, it become constant we cannot
change the value of final variable.
2. Final method: if we create method, we cannot override it.
3. Final class: if we create any final class, we cannot extend it or inherit it.

Exception:
Exception disrupts the normal flow of the program. It is an object which is
thrown at runtime.
Exception Handling?
Exception Handling is a mechanism to handle runtime errors such as
ClassNotFoundException, IOException, AirthmaticExceptio,
ArrayOfBoundException, SQLException, RemoteException, etc.
try/ catch block?
A try/ catch block is used to handle exceptions. The try block defines a set of
statements that may lead to an error. The catch block basically catches the
exception.

You might also like