Object Oriented Programming Interview Question

  1. What is Object Oriented Programming  in java? What are the main principal of Object Oriented Programming?
  2. What is Encapsulation is java? How we will achieve it?
  3. What is Inheritance in java? What is benefit of inheritance?
  4. What are the types of polymorphism? What is Polymorphism in java?
  5. What is Dynamic Dispatch in java?
  6. What is Abstraction in java? How we achieve abstraction in java?
  7. What is difference between Abstract class and Interface in java ?
  8. What are different between method overriding and method overloading in java?

What is Object Oriented Programming  in java? What are the main principal of Object Oriented Programming?

Object Oriented Programming is a acronym for Object Oriented Program. Object Oriented Programming is a programming methodology that views a program as similarly consisting of objects that interact with each other by means of actions.The world around us is made up of objects, such as people,automobiles, buildings, streets, adding machines, papers, and so forth. Each of these objects has the ability to perform certain actions, and each of these actions has some effect on some of the other objects.

Following are the main features of Object Oriented Programming –

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

What is Encapsulation is java? How we will achieve it?

Encapsulation is a process of binding the data(instance variable) and methods into a single unit. Now question arises how you will achieve encapsulation.Simply by making instance variable as private and providing corresponding getter and setter method(i.e JavaBeans Naming conventions).

Let’s see simple example of encapsulation –

Employee.java –

Test.java –

NOTE:-
Here class Test can access the data of class Employee only going through getter and setter.
Benefit of encapsulation:–
The ability to make changes in your code without breaking the code of others who use your code is a big benefit of encapsulation.

What is Inheritance in java? What is benefit of inheritance?

Inheritance is one of the OOPs concept by which one class acquires all the properties of other class(i.e super class).Inheritance achieved by extends keyword. Inheritance is every where in java,even you define a single class then it follows Inheritance principle.Inheritance follows IS-A relationship. Let’s see an example –

Example 1 :-

Here inheritance happening because Animal class is extending Object class.

Example 2 :-

Note –

String animalName is defied within Animal class but we can access within Horse class. Similarly m1() method is defined within Animal class but we are accessing within Horse class. So no need to define String animalName and method m1() in horse class, just extend Animal class and use variables and method of Animal class. This is code re usability.

Benefit of Inheritance :-

  1. Here we are doing e1.equals(e2) and it works fine(i.e it will return false).As we can see equals() method is not defined within Employee class, but we are using because of it has already defined within Object class.
  2. Inheritance is also useful to achieve polymorphism.

What is Polymorphism in java?  What are the types of polymorphism?

Polymorphism means more than one form. when one form (i.e methods and reference variable) behaving differently in different case its called polymorphism. There are two types of polymorphism.

  1. Compile time polymorphism –  Compile time polymorphism is also called static binding.It’s achieved with the help of method Overloading.
  2. Run Time Polymorphism – RunTime polymorphism also called dynamic binding. It’s achieved with the help of method overriding.

Example of Compile time polymorphism :-

Example of Run time polymorphism :-

output will be  – This is m1 method in class Tiger.

What is Dynamic Dispatch in java?

Dynamic Dispatch is process of assigning subclass object to super class reference variable.

Example of dynamic dispatch –

Here we can see that ” a ” is object type of Tiger and reference type of Animal.

 

What is Abstraction in java? How we achieve abstraction in java? 

Abstraction is process of hiding internal details.It allows user to manage and use to difference application without worrying ,how it has internally implemented.we can take a simple example .when we login to any website use username and password.Here we no need to worry what code has been written. Now we take a real time scenario.suppose we are driving a car, we never worry about how engine is working,just we drive.

In java we can achieve abstraction in two ways :-

  1. By making classes abstract.
  2. By using interface.

Some Points related to Abstract Class :-

  1. we can declare any class abstract with help of abstract keywords.
  2. abstract class can contain abstract as well as non abstract method.
  3. abstract class cannot be instantiated only reference can be created.
  4. if a class is going to extend abstract class then it must override all abstract method or if sub class doesn’t override all the abstract method then declare subclass as a abstract.

Some Points related to Interface :-

  1. Interface is one of Object Oriented principal which is contract for what a class can do, they say nothing about by which way class must do it.
  2. One class can implement one or more than one interface.
  3. Interface cannot implement another interface.
  4. Interface can extends one or more interface.
  5. Interface cannot be instantiated with new keyword, reference can be created.
  6. we can only define constant within interface i.e by default fields define within interface is “public static final”.
  7. Interface can have only abstract method i.e by default methods of interface is “public abstract”.
  8. if any class is implementing interface then that class must override all the method of interface or if class is not overriding all the methods of interface then it must declare as abstract.

What is difference between Abstract class and Interface in java ?

Let’s see difference between abstract and interface –

Abstract classInterface
1.An abstract class can contain both abstract as well non abstract method.1.Interface contain only abstract method.By default method of interface is public abstract.
2.Abstract class can have instance variable2.within interface we can only define constants.
3.Abstract class can have constructor.3.Interface doesn't have constructor.
4.Abstract class extended by other class with help of extend keyword.4.Interface is implemented by other class with help of implement keyword
5.Any class can extends only one abstract class.5.But in case of interface class can implement then one interface.
6.suppose there is scenario where IS-A relationship happening then go for abstract class.6.If there is no any IS-A relationship in our requirement then go for interface.
7.when we define abstract class then its our duty to provide sub class for it.7.In case of interface we can leave it for third party vendor.

What are different between method overriding and method overloading in java?

Let’s see difference between overriding and overloading –

Method OverloadingMethod Overriding
1.In case of method overloading argument must be change1.in case of method overriding argument must be same.
2.Return type of overloaded method can be change.2.Return type of Overridden method can not be change except covariant return type
3.It happens at compile time.3.it happens at runtime.
4.In case of method overloading exception can be change.4.In case of method overriding subclass overridden method can declare same, subclass or no exception.
5.There is no restriction for access modifier in case of overloading.5.In case of method overriding subclass overridden method must have same or less restrictive access modifier.

 

 

Top