Oops Concept Interview Questions in Java

Advertisements

Java Interview Questions Constructor Interview Question

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;

  • Class
  • Object
  • Polymorphism
  • Inheritance
  • Abstraction
  • Encapsulation
  • Aggreagation
  • Composition
  • Association

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.

Data abstractionEncapsulation
Solves the problem at the design levelSolves the problem at the implementation level
Allows showing important aspects while hiding implementation detailsBinds 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 ProgrammingStructural Programming
Object-Oriented Programming is a type of programming which is based on objects rather than just functions and proceduresProvides logical structure to a program where programs are divided functions
It follow Bottom-up approachIt follow Top-down approach
it's Provides data hidingIt Does not provide data hiding
Can solve problems of any complexityCan solve moderate problems
Code can be reused thereby reducing redundancyDoes 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 MethodOverridden Method
ArgumentsMust be ChangeMust not be change
Return TypeCan changeCan't change except for covariant returns
ExceptionsCan ChangeCan reduce or eliminate. Must not throw new or broader checked exceptions
AccessCan changeMust not make more restrictive (can be less restrictive)
InvocationReference 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.

ClassMethod
A class is basically a template that binds the code and data together into a single unit. Classes consist of methods, variables, etcCallable 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 comparisonAbstract ClassInferface
MethodsCan have abstract as well as other methodsOnly abstract methods
Final VariablesMay contain final and non-final variablesVariables declared are final by default
Accessibility of Data MembersCan be private, public, etcPublic by default
ImplementationCan provide the implementation of an interfaceCannot 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.


Java Interview Questions Constructor Interview Question

Advertisements

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

Download Projects

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements