Basic OOP Questions that Interviewers Ask Junior Developers

Basic OOP Questions

These are the basic OOP questions interviewers commonly asks the newbies and even experts are sometimes asked about these terms.

Question 1. What is the concept of IS-A and HAS-A relation in OOP?

Answer. IS-A relationship is used for inheritance, consider an example

Circle IS-A Shape

class Shape //Parent Class or Super Class
class Circle // Child Class
class Rectangle //Child Class

Circle IS-A Shape

class Circle extends Shape //in Java
class Circle : Shape            //in C#

Rectangle IS-A Shape

class Rectangle extends Shape //in Java
class Rectangle : Shape            //in C#

Another Example:

David is Son of Charles
David IS-A Child

class Father
class Child
class Child extends Father // in java
class Child : Father            // in C#

now consider another class Habit

when class Child has an instance of class Habit
this instance will show that Child Has-A Habit

IS-A is used in the concept of inheritance.
HAS-A is used in the concept of keeping an instance.

 

Question 2. What are the core concepts of Object Oriented Programming?

Answer. The 4 core OOP concepts i.e. Abstraction, Encapsulation, Inheritance and Polymorphism.

1. Abstraction:

Abstraction is used to set the appropriate visibility of the class. It deals with which the contents of the class. Which content should be displayed and which should not be displayed or accessible outside the class. Abstraction also refers to the terms access specifying, access modifying, hiding data and encapsulation.

2. Encapsulation:

To encapsulate fields, we use access specifiers, i.e. public, protected, private and default. If no access specifier is defined, it is default. default is accessible in the package in which declared. public is accessible on the whole project. protected limits the scope of the child class as well as the parent class. private limits the scope to the class scope in which declared.

3. Inheritance:

Inheritance is the same concept as it is used in real life. Inheritance means the parent’s properties are transferred to the child. Similarly, all the content in the parent class which is not private is available in the child class. To access any member of the parent class, super keyword is used. To access any member of the same class, this keyword is used.

class Parent
class Child

class Child extends Parent //java
class Child : Parent //C#

4. Polymorphism:

Polymorphism means same message is interpreted differently. This can be explained through an example of an animal. It is further categorized as method overloading and method overriding.

a. Method Overriding:

Method Overriding refers to the methods with same names and same methods but differentiated through the different classes inherited from the same class.

class Animal
class Dog extends Animal -> run( ){ “Dog is Running” }
class Cat extends Animal -> run( ){ “Cat is Running” }

both the classes (Dog and Cat) have separate methods, run()
there are instance of both classes in another

class Shed
Animal dog=new Dog( );
Animal cat=new Cat( );

When we call dog.run( );
It will display “Dog is Running”

When we call cat.run( );
It will display “Cat is Running”

run( ); method is interpreted differently through different instances.b. Method Overloading:

b. Method Overloading:

Method overloading refers to the methods of the same class and same names but they are differentiated through the method signatures. Most commonly for beginners, it is used in the overloading of the constructor.

-before moving toward method overloading, I will describe constructor. Constructor is a method with the same name as the name of the class, this is the first method which is called when instantiating or when creating the object to access the members of any class. Mostly constructor is used to initialize (to give initial values) variables/members of the class.  In Java and C# new keyword is used.

<Class name> <object/instance name> = new <class name>();

If we have a class SomeClass and its instance is someInstance

 class SomeClass
Its instance/object will be like
SomeClass someInstance=new SomeClass ();

Coming back to method overloading I will consider an example of Constructor overloading

As we considered a class SomeClass
And now we have a variable SomeVariable in the class

 

and we have another class OtherClass

Read More about overloading and overriding.

Question 3. What is the difference between abstract class and interface?

Answer.
Abstract class:
Abstract class contains abstract methods as well as concrete methods. This type of class is declared using abstract keyword. A class must extend the abstract class to implement its abstract methods.

Interface:
The interface only contains abstract methods. It can be declared using interface keyword. As all the methods in the interface are abstract so there should be a class to implement all its methods. Implements keyword is used with the class to implement the interface methods.

Question 4. What is the difference between extends and implements?

Answer.
extends:
When a subclass extends a super class for reusability purposes and to override the super class methods extends keyword is used. extends keyword can only extend one class at a time. see Question 2. Inheritance.

implements:
When a class gives the implementation of all the methods of any interface. implements keyword is used with the class name to implements the interface. implements keyword can implement more than one interfaces.

           

Learn more about the interface and abstract class.

 

Question 5. What is the difference between method overloading and overriding?Answer.

Method Overloading:
Two or more methods with same names but different parameters and different or same return types are called method overloading.

Method Overriding:
Two or more methods with same signatures (name, return type, parameters) but from different inherited classes is known as method overriding.

See question 2. Polymorphism.

Read more about method overloading and method overriding.

Question 6. What is the concept of multiple inheritances?

Answer.
Multiple Inheritance:
Multiple Inheritance means when two or more classes are the parent of a single class. Multiple inheritances are not supported in Java as when using the super keyword to differentiate between overridden methods compiler confuses between the parent classes.

Leave a Reply

Your email address will not be published. Required fields are marked *