DronaBlog

Monday, September 3, 2018

Java Interview Questions and Answers - Part 4


Are you preparing for Java interview and looking for information for preparation? Are you also interested in knowing what kinds of questions about Object Oriented concepts are normally asked during Java interview? If so, then this article provides detailed questions and answers about Java, Object Oriented Concepts. This article also provides detailed information about Inheritance concepts. Good luck for your interview.

Q1: What is Object Oriented Programming?
Answer:

A programming style which is associated with the concepts like an object, a class, the inheritance, the polymorphism, an encapsulation and an abstraction is called as Object Oriented Programming.

Q2: What are the core concepts of Object Oriented Programming?
Answer: 

The core concepts of Object Oriented Programming are:

  1. Inheritance
  2. Encapsulation
  3. Abstraction
  4. Polymorphism

Q3: What is inheritance?
Answer:


  • Inheritance is nothing but inheriting properties (behaviour and state) of class into other class. 
  • The process by which one object acquires characteristics from one or more other objects is also termed as inheritance. e.g. children acquire characteristics from their parents.
  • The class from which properties are inherited is called parent class or base class or superclass. 
  • The class in which properties are inherited is called child or derived class or subclass. 
  • It helps to reuse the code and establish a relationship between different classes.
  • The common business logic can be moved from the derived class into the base class to improve maintainability of code.

Q4: What are types of inheritances?
Answer:

There are 4 types of inheritance
1. Single Level Inheritance
2. Multi Level Inheritance
3. Hierarchical Inheritance
4. Hybrid Inheritance

Q5: Explain types of inheritances?
Answer:

1. Single Level Inheritance:
a. In this type of inheritance, one class inherits the properties of the another class.
b. Properties such as behaviour and state from a single parent class is inherited in child class.
b. It helps to add new feature in existing code as as the code re-usability.

Here, Class Shape is parent class and Class Triangle is child class which inherits the properties and behaviour of the parent class.4

Class Shape
{ ... }
Class Triangle extends Shape 
{ ... }

2. Multilevel Inheritance:
a. If class has more than one parent then it is called Multilevel Inheritance.
b. In the multilevel inheritance, a class is derived from the parent class which is also derived from another parent class.

Here, The class Square is derived from it's parent class Rectangle which is derived from it's parent Shape. The class Square has two parents - Rectangle and Shape respectively. Hence it is a multilevel inheritance.

Class Shape 
{ ... }
Class Rectangle extends Shape
{ ... }
Class Square extends Rectangle
{ ... }

3. Hierarchical Inheritance:
a. In the hierarchical inheritance the parent class has more than one child classes.
b. We can also state that if more than one child classes have the same parent class then it is a hierarchical inheritance.

Here, the super class Shape has two sub classes - Triangle and Rectangle, hence it is a hierarchical inheritance.

Class Shape 
{ ... }
Class Triangle extends Shape 
{ ... }
Class Rectangle extends Shape 
{ ... }

4. Hybrid Inheritance:
a. It is a combination of multiple inheritance and multilevel inheritance.
b. The multiple inheritance is not supported in Java as it leads to ambiguity.
c. Java supports multiple interfaces inheritance.


Q6: What is Implementation inheritance?
Answer:

a. Implementation inheritance is also know as class inheritance.
b. We can extend an functionality by reusing functionality in the super class by inheriting all or some of the operations already implemented.
c. In Java, we can only inherit from one superclass.
d. It helps in code reusability.
e. Improper use of class inheritance can cause problem in making future changes.
f. The subclass becomes tightly coupled with the superclass.
g. We have to make sure that the subclasses depend only on the behavior of the superclass, not on the actual implementation.

Q7: What is Interface inheritance?
Answer:

a. Interface inheritance is also know as type inheritance or as subtyping.
b. Interface inheritance reduces the coupling or implementation dependencies between systems.
c. It promotes the design concept of program to interfaces not to implementations.
d. We can implement any number of interfaces.
e. As this type of implementation will not influence specific subclass implementations, hence it is more flexible than implementation inheritance.

                                   

No comments:

Post a Comment

Please do not enter any spam link in the comment box.

What is CRM system?

  In the digital age, where customer-centricity reigns supreme, businesses are increasingly turning to advanced technologies to manage and n...