Monday, October 24, 2016

Java OOPs Interview Questions And Answers

In this session we will learn Top OOPs concept interview Questions and answers.When you attend the interview for job tittle as java developer then you must have depth knowledge on the Object Oriented Programming(OOP) concept.This is key point in interview round,interviewer start their first question from OOPs concept.Therefore, I am writing this article for freshers and experienced developers and brush up these questions before attending interview and some other Basic java basic Questions also need to be prepare.


1.What are the principle concepts of OOPS?

Ans: There are four principle concepts upon which object oriented design and programming Test. They are
  • Abstraction
  • Polymorphism
  • Inheritance
  • Encapsulation
2. What is Abstraction?

Ans: Abstraction refers to the act of representing essential features without including the background details of explanations. It is a process of hiding the implementation details and showing only functionality to the user.We can say in another way, it shows only important things to the user and hides the internal details. For example, Sending SMS, you just type the text and send the message,but you don't know the internal processing about the message delivery.

ways to achieve abstraction:
There are two ways to achieve  abstraction in java
  1. abstract class
  2. interface
3. What is Encapsulation?

Ans: Encapsulation is a technique used for hiding the properties and behaviors of an object and allowing outside access only as appropriate. It prevents other objects from directly altering or accessing the properties or methods of the encapsulation object.

4. What is the difference between abstraction and encapsualtion?

Ans: 
  • Abstraction focuses on the outside view of an object that means the interface. But encapsulation(information hiding) prevents clients from seeing it's inside view,where the behavior of the abstraction is implemented.
  • Abstraction solves the problem in the design side while Encapsulation is the implementation.
5. What is inheritance?

Ans: Inheritance is the process by which objects of one class acquire the properties of objects of another class. A class that is inherited is called as Super class. The class that does the inheriting is called as subclass.
 Inheritance is done by using the keyword "extends".
The common reasons to use inheritance are:
  • to improve code reuse
  • to use polymorphism
 6. What is Polymorphism?

Ans: Polymorphism means one interface, many implementations.It is a characteristics of being able to assign a different meaning or usage to something in different contexts,specially to allow an entity such as a variable,a function.


7. How does java implement polymorphism?

Ans: java implement polymorphism in three ways:

Inheritance,Overloading and Overriding are used to achieve polymorphism in java.
 Overloding  means multiple methods have same name but different formal arguments lists.In other case,multiple methods have the same name,same return type and same formal argument list is called Overriding.


8.What is runtime polymorphism or dynamic method dispatch?

Ans: In Java, runtime polymorphism or dynamic method dispatch is a  process in which a call to an overridden method is resolved at runtime rather than at compile-time.In this process,an overridden method is called through the reference variable of a subclass. The determination of the method to be called is based on the object being referred to by the reference variable.

9.What is Dynamic Binding?

Ans: Binding refers to the linking of a procedure call to the code to be executed in response to the call.Dynamic binding also called as late binding. It means that the code associated with the given procedure call is not known until the time of the call at run-time.It is associated with polymorphism and inheritance.

10.What is method Overloading?

Ans: It means to have two or more methods with same name in the same class with different arguments. The benefit of method overloading is that it allows you implement methods that support the same semantic operation but differ by argument number or type.

Rules for Overloading:
  • Overloaded methods must change the argument list
  •  Overloaded methods  can change the return type
  • Overloaded methods can change the access modifier 
  • Overloaded methods can declare new checked exceptions
  • A method can be overloaded in the same class or in a subclass
11.What is method Overriding?

Ans: Overriding occurs when sub class declares a method that has the same type arguments as a method declared by one of its super class.The key benefit of overriding is the ability to define behavior that's specific to a particular subclass type.

Rules for Overriding:

  • The Overriding method can not have more restrictive access modifier than the method being overridden.
  • you can not override a method  marked final
  • you can not override a method marked static

12. How do you prevent a method from being Overridden?

Ans: To prevent a specific method from being Overridden in a sub class, use the final modifier on the method declaration,which means this is the final implementation of this method,the end of its inheritance .

13. What is Dependency?

Ans: Change in structure or behavior of a class affects the other related class,then there is a dependency between those two classes. It needs not be the same vice-versa. When one class contains the other class in this happens.

Example: Relationship between shape and circle is dependency.

14.How do you express an 'is-a' relationship and 'has-a' relationship?

Ans:  The 'IS-A' relationship is expressed with inheritance and 'HAS-A' relationship is expressed with composition. Inheritance is uni-dirctional.
Example: House is a Building.Building is not a house.
Inheritance uses extends keyword.
Composition is used when House has a Bathroom.Composition simply means using instance variables that refer to other objects.

15. What is Super?

Ans: Super is a Keyword which is used to access the method or member variables from the superclass. If a method hides one of the member variables in its superclass,the method can refer to the hidden variable through the use of super keyword. In the same way, if a method overrides one of the methods in its super class,the method can invoke the overridden method through the use of the super keyword

Note: if use constructor,if you use super(),it must be the very first code,and you can not access any this.xxx variables or methods to compute its parameters.


No comments:

Post a Comment

High Paying Jobs after Learning Python

Everyone knows Python is one of the most demand Programming Language. It is a computer programming language to build web applications and sc...