Oops Concept Interview Questions in Java
Interview Questions on oops Concept in Java
Here we cover all topics related to oops concept in java. Once again we discuss some oops concept in detail separately.
What are different oops concept in java ?
OOPs stands for Object Oriented Programming. The oops concepts are similar in any other programming languages. Because it is not programming concept. All oops concept in java are;
What is class ?
A class is a specification or blue print or template of an object.
What is object ?
Object is instance of class. It is dynamic memory allocation of class.
What are the Characteristics of Object ?
Characteristics of Object are;State Behavior and Identity.
Can a class extend more than one class in Java ?
No, a class can only extend another class because Java doesn't support multiple inheritances but yes, it can implement multiple interfaces.
What is the difference between abstraction and polymorphism in Java ?
Abstraction generalizes the concept and Polymorphism allow you to use different implementation without changing your code.
What is an Abstraction ?
Abstraction is a process to show only usful data and remaining hide from user.
Real Life Example of Abstraction ?
When you log into your email, compose and send a mail. Again there is a whole lot of background processing involved, verifying the recipient, sending request to the email server, sending your email. Here you are only interested in composing and clicking on the send button. What really happens when you click on the send button, is hidden from you.
Real life example of abstraction ?
Real life example of abstraction is ATM machine; here only show balance in your account but not display ATM pin code.
How to achieve data abstraction?
Data abstraction can be achieved through:
- Abstract class
- Abstract method
What is an abstract class?
An 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.
Differentiate between data abstraction and encapsulation.
|Solves the problem at the design level||Solves the problem at the implementation level|
|Allows showing important aspects while hiding implementation details||Binds code and data together into a single unit and hides it from the world|
What is Encapsulation ?
The process of binding data and methods into a single unit is known as Encapsulation. In other word The process of binding the data with related methods known as encapsulation.
Real Life Example of Encapsulation ?
Encapsulation is like enclosing in a capsule. Encapsulation is like your bag in which you can keep your pen, book etc.
What is inheritance ?
It is used in java for achieve concept of re-usability. As the name suggests , inheritance means to take something that already made.
What is polymorphism ?
It is process to represent any things in multiple forms.
Real life example of Polymorphism ?
A Person who knows more than two languages he can speak in a language which he knows. Here person is Object and speak is polymorphisam.
What is static polymorphism?
Static polymorphism (static binding) is a kind of polymorphism that occurs at compile time. An example of compile-time polymorphism is method overloading.
What is dynamic polymorphism?
Runtime polymorphism or dynamic polymorphism (dynamic binding) is a type of polymorphism which is resolved during runtime. An example of runtime polymorphism is method overriding.
What is method overloading in Java ?
When same method is available in any class with same name and different signature is called method overloading.
What is method overriding in Java ?
When same method available in base class and drive class with same name and same signature it is called method overriding.
What is the difference between OOP and SOP?
|Object-Oriented Programming||Structural Programming|
|Object-Oriented Programming is a type of programming which is based on objects rather than just functions and procedures||Provides logical structure to a program where programs are divided functions|
|It follow Bottom-up approach||It follow Top-down approach|
|it's Provides data hiding||It Does not provide data hiding|
|Can solve problems of any complexity||Can solve moderate problems|
|Code can be reused thereby reducing redundancy||Does not support code reusability|
What are the different types of inheritance?
- Single inheritance
- Multiple inheritance
- Multilevel inheritance
- Hierarchical inheritance
- Hybrid inheritance
What is a superclass?
A superclass or base class is a class that acts as a parent to some other class or classes. For example, the Vehicle class is a superclass of class Car.
What is a subclass?
A class that inherits from another class is called the subclass. For example, the class Car is a subclass or a derived of Vehicle class.
What is hybrid inheritance?
Hybrid inheritance is a combination of multiple and multi-level inheritance.
What is hierarchical inheritance?
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.
What are the limitations of inheritance?
- Increases the time and effort required to execute a program as it requires jumping back and forth between different classes
- The parent class and the child class get tightly coupled
- Any modifications to the program would require changes both in the parent as well as the child class
- Needs careful implementation else would lead to incorrect results
What is Aggregation ?
If a class have an entity reference, it is known as Aggregation. Aggregation represents HAS-A relationship..
What is Composition ?
Composition is the design technique to implement has-a relationship in classes. Composition in java is achieved by using instance variables that refers to other objects.
Can we declare abstract method as final ?
No. its not possible to declare abstract method with final keyword. Because abstract methods should be override by its sub classes.
Is it possible to declare abstract method as private ?
No. its not possible to declare abstract method with private. Because abstract methods should be override by its sub classes.
Differences between method Overloading and method Overriding ?
|Overloaded Method||Overridden Method|
|Arguments||Must be Change||Must not be change|
|Return Type||Can change||Can't change except for covariant returns|
|Exceptions||Can Change||Can reduce or eliminate. Must not throw new or broader checked exceptions|
|Access||Can change||Must not make more restrictive (can be less restrictive)|
|Invocation||Reference type determines which overloaded version is selected. Happens at compile time.||Object type determines which method is selected. Happens at runtime.|
Methods and Functions OOPs interview questions
What are virtual functions?
Virtual functions are functions that are present in the parent class and are overridden by the subclass. These functions are used to achieve runtime polymorphism.
What are pure virtual functions?
Pure virtual functions or abstract functions are functions that are only declared in the base class. This means that they do not contain any definition in the base class and need to be redefined in the subclass.
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.
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.
Types of constructors
Types of constructors differ from language to language. However, all the possible constructors are:
- Default constructor
- Parameterized constructor
- Copy constructor
What is a copy constructor?
A copy constructor creates objects by copying variables from another object of the same class. The main aim of a copy constructor is to create a new object from an existing one.
What is the use of 'finalize'?
Finalize as an object method used to free up unmanaged resources and cleanup before Garbage Collection(GC). It performs memory management tasks.
What is Garbage Collection(GC)?
Garbage Collection is an implementation of automatic memory management. The Garbage collector frees up space occupied by objects that are no longer in existence.
Differentiate between a class and a method.
|A class is basically a template that binds the code and data together into a single unit. Classes consist of methods, variables, etc||Callable set of instructions also called a procedure or function that are to be performed on the given data|
Differentiate between an abstract class and an interface?
|Basis for comparison||Abstract Class||Inferface|
|Methods||Can have abstract as well as other methods||Only abstract methods|
|Final Variables||May contain final and non-final variables||Variables declared are final by default|
|Accessibility of Data Members||Can be private, public, etc||Public by default|
|Implementation||Can provide the implementation of an interface||Cannot provide the implementation of an abstract class|
What is a final variable?
A variable whose value does not change. It always refers to the same object by the property of non-transversity.
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.
What is exception handling?
Exception handling in Object-Oriented Programming is a very important concept that is used to manage errors. An exception handler allows errors to be thrown and caught and implements a centralized mechanism to resolve them.
What is a 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.
What is a finally block?
A finally block consists of code that is used to execute important code such as closing a connection, etc. This block executes when the try block exits. It also makes sure that finally block executes even in case some unexpected exception is encountered.