In this post, we will cover some of the commonly asked interview questions on Object Oriented Programming(OOP) concepts. These are frequently asked in developer and architect level java interviews alike.
1. What is Object Oriented Programming?
Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which may contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods.[Wiki]
Objects communicate with other objects by sending messages to them. When an object receives a message, it responds by executing one of its methods, which may modify its state.
A class is a basic unit of programming in OOP that contains the structural blueprint of objects. An object is known as an instance of a class.
For example, if you want to represent real world persons in OOP, you can represent by creating a class Person as follows :
public class Person { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } }
The Person class above contains :
Instance variables “name” and “age” to store corresponding data for a particular Person.
Methods getName() and getAge() that can help obtain name and age of a Person.
2. What are the core concepts of OOP?
The core concepts of object-oriented paradigm are :
- Abstraction : This is the process of exposing the essential details of an entity, while ignoring the irrelevant details.
- Encapsulation : This is the process of bundling data and operations on the data together in an entity.
- Inheritance : Inheritance is used to derive a new type from an existing type, thereby establishing a parent-child relationship.
- Polymorphism : This an entity take on different meanings in different contexts.
3. What is Abstraction ?
Abstraction is considering details that are necessary to view the problem in the way that is appropriate in a particular context while ignoring the inessential details.
For example, in a Calculator, you have different buttons to perform various numerical functions. But how the numerical operations are performed is abstracted away from the user.
In Java, abstraction can be achieved by defining an Abstract class and extending that class or by defining an interface.
4. Explain Encapsulation in Java
Encapsulation is simply the bundling of items together into one entity.
Java supports encapsulation by letting us bundle data and methods that operate on the data in an entity called a class. Similarly, we can bundle one or more logically related classes in a package.
In the above example , the Person class is an encapsulation of the data elements “name” and “age”, and the methods “getName()” and “getAge()”.
A related concept, Information hiding is concerned with how an item is hidden. The Person class in above example, uses Information hiding by marking the properties name and age with “private” access specifier that hides direct access to these variables from outside world.
5. What is the difference between Abstraction and Encapsulation in Java ?
Abstraction is a process where you show only “relevant” data and “hide” unnecessary details of an object from the user.
For example, in your phone or tablet, you just need to know what app icons to touch to take a picture or play a game etc, but internal details of what really happens when you touch an icon is abstracted away from the user.
Encapsulation is the process of combining data and functions into a single unit called class.
In Encapsulation, the data is encapsulated along with methods inside a class. A related concept Information hiding is concerned with hiding the data from outside.
6. What is Inheritance ?
Inheritance is a mechanism by which we can create a new class by reusing code from an existing class. The new class is called a subclass and the existing class is called the superclass.
In the below example, Employee class is a subclass of Person. The inheritance relationship is established using the extends keyword.
Here, an Employee instance can use the properties and methods of Person and doesn’t need to define them again.
public class Person { private String name; private String age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } }
public class Employee extends Person{ private String company; public String getCompany() { return company; } public void setCompany(String company) { this.company= company; } }
The relationship that must exist between the superclass and the subclass in order for inheritance to be effective is called an “is-a” relationship.
In the above example, an Employee is also a Person. So, Employee class can inherit from Person class.
Read more on inheritance here :
7. Does every class in Java inherit from a SuperClass ?
The Object class is the default superclass of all classes in Java.
If a class does not specify a superclass using the keyword extends in its class declaration, it inherits from the java.lang.Object class.
The Object class, defined in the java.lang package, defines and implements behavior common to all classes—including the ones that you write. In the Java platform, many classes derive directly from Object, other classes derive from some of those classes, and so on, forming a hierarchy of classes.
Image ref : https://docs.oracle.com
8. What is Polymorphism ?
Polymorphism is the ability of an entity (e.g. variable, class, method, object, code, parameter, etc.) to take on different meanings in different contexts.
Polmorphism can be categorized into Compile time polymorphism(Overloading) and Runtime polymorphism(Overriding).
9. What is Method Overloading or Compile time polymorphism ?
Overloading (also known as compile time polymorphism) is where more than one methods share the same name with different method signatures.
For example, the below Calculator class defines two methods with same name add but with different signatures :
public class Calculator{ private int add(int i, int j) { return i + j; } private int add(int i, int j, int k) { return i + j + k; } }
Read more on method overloading here :
10. What is Method Overriding or Run-time polymorphism ?
When a subclass method has the same name and type signature as its superclass method, then the method is subclass is said to override the corresponding method in the superclass.
When a overridden method is called from subclass, it always refers to the subclass version of the method… the superclass version of the method will be hidden.
Here is an example of the same:
public class ClassA { public void show(){ System.out.println("ClassA.show() called"); } } public class ClassB extends ClassA{ public void show(){ System.out.println("ClassB.show() called"); } } public class InheritanceExample { public static void main(String[] args) { ClassB obj = new ClassB(); obj.show(); } }
In the above example, both the classes ClassA and ClassB define the method show(). The method has the same name and type signature. So, when the method is invoked using the child class instance, the subclass’s show() method will be called.
This will print :
ClassB.show() called
Method overriding occurs only when the names and type signatures of the two methods are identical. If they are not, the methods are overloaded, but not overridden.
Read more on this here :
Method Overriding and Runtime Polymorphism in Java
11. What is difference between Static and Dynamic polymorphism in Java?
In Static polymorphism, more than one method share the same name with different method signatures. The same method name is overloaded with different number or type of parameters.
In Java, static polymorphism is implemented using method overloading.
In Dynamic polymorphism, the same method is overridden in different classes and the method name and type/number of parameters remains same.
In Java, Dynamic polymorphism is implemented using method overriding.
12. Does Java support multiple inheritance with classes ? How about multiple inheritance using interfaces
Java doesn’t support multiple inheritance with classes. So, a subclass cannot extend more than one super classes. However, multiple inheritance can be achieved by implementing more than one interface.
The following is not allowed in Java :
class SubClass extends SuperClass1, SuperClass2 {
…
…
}
Although, you can create a hierarchy of inheritance in which a subclass can become superclass of another class.
However, in java, one class can implement more than one interface.
Read more here :
Multiple Inheritance using Interfaces
13. When should you use abstract classes or interfaces in Java ?
From Oracle documentation :
Consider using abstract classes if any of these statements apply to your situation:
- You want to share code among several closely related classes.
- You expect that classes that extend your abstract class have many common methods or fields, or require access modifiers other than public (such as protected and private).
- You want to declare non-static or non-final fields. This enables you to define methods that can access and modify the state of the object to which they belong.
Consider using interfaces if any of these statements apply to your situation:
- You expect that unrelated classes would implement your interface. For example, the interfaces Comparable and Cloneable are implemented by many unrelated classes.
- You want to specify the behavior of a particular data type, but not concerned about who implements its behavior.
- You want to take advantage of multiple inheritance of type.
© 2017 – 2018, https:. All rights reserved. On republishing this post, you must provide link to original post