Java OOPs interview questions and answers

It is very important to know the concepts of Object Oriented Programming(OOP) before you go for an interview for any of the OOPs languages like Java, C++ or Python.  Here I would be listing the mostly asked OOPs interview questions. And would also explain various OOPs concepts.

 

1. What is Object Oriented Programming?

Object Oriented programming is a style of programming that is based on the concept of objects. Objects advertise the type of data that it will store and the types of operations that it allows to manipulate the data.

Object oriented programming

Object oriented programming

2. What are the core concepts of OOPS?

The different OOPS concepts are

 

3. What is Abstraction?

Abstraction is a the concept that denotes the extracting of essential details about an item or a group of items while ignoring the inessential details. In Java abstraction can be achieved by defining an Abstract class and extending that class or by defining an interface.

public interface Vehcile {
  public String getModel();
  public String getType();
}
abstraction

abstraction

4. What is Encapsulation?

Encapsulation is grouping of all closely related data into a Class. The class contains all the data and methods. The attributes are not exposed to the outside world by defining them private and the methods are exposed by defining them as public. Essentially it hides the details of how things work and only making the behaviour public. Here is a typical java bean that hides the attributes but exposes the get and set method on the attributes.

package com.programtalk.learn.encapsulation;

public class Car {
	private String model;
	private String type;
	
	public String getModel() {
		return model;
	}
	public void setModel(String model) {
		this.model = model;
	}
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
}

5. What is Inheritance?

Inheritance is a mechanism by which one class is derived from another class. In Java, classes can inherit the properties and methods of other classes. The class that is derived from another class is called a subclass and the class that is inherited is called a superclass. Here is nice example of  inheritance. A Student is a subclass of Person. So the student can use the properties and methods of Person and doesn’t need to define them again.

package com.programtalk.learn.inheritance;

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;
	}
}
package com.programtalk.learn.inheritance;
public class Student extends Person{
	private String grade;

	public String getGrade() {
		return grade;
	}

	public void setGrade(String grade) {
		this.grade = grade;
	}
}

Inheritance

Inheritance

6. What is Polymorphism?

Polymorphism is a concept that more than one type of objects share a common interface but have a different functionality. Example is that of various shapes.  Calling the draw method on any of shapes draws the respective shape.

ploymorphism

ploymorphism

All Java classes inherit class Object and hence all java objects are polymorphic. Concepts that demonstrate polymorphism in java:

Method Overloading:

In Java same method name can be given to more than one methods but the arguments or parameters have to be different Here is a simple example. A class Operations defines three methods with same name add but with different argument list.

package com.programtalk.learn.methodoverloading;

public class Operations{
	
	private int add(int i, int j) {
		return i + j;
	}
	private long add(long i, long j) {
		return i + j;
	}
	private float add(float i, float j) {
		return i + j;
	}
}

Method Overriding:

In Method overriding a subclass method changes the behavior of the SuperClass method by redefining the method and overriding its functionality. Here is a nice example of method overriding.

  • the example calls draw() on shape first and it prints “I am a shape”
  • then it calls draw() on circle and it prints “I am a shape”
  • then it creates an instance of Circle but assigns it to Shape. This is possible because Circle is also a type of Shape as it extends Shape. Now when draw() is invoked here. It prints “I am a Circle”.  This behavior is referred to as virtual method invocation. An overridden method is invoked at run time, no matter what data type the reference is that was used in the source code at compile time.
package com.programtalk.learn.methodoverriding;

class Shape {
	
	public void draw(){
		System.out.println("I am a shape");
	}
}

class Circle extends Shape {
	
	public void draw(){
		System.out.println("I am a circle");
	}
}

public class TestOverriding{
	
	public static void main(String[] args) {
		Circle circle = new Circle();
		circle.draw();
		
		Shape shape = new Shape();
		shape.draw();
		// this is the interesting case
		Shape circleShape = new Circle();
		circleShape.draw();
		
	}
}

Output

I am a circle
I am a shape
I am a circle

You can also find more details here

7. What is Association?

It represents a relationship between two or more objects where all objects have their own lifecycle and there is no owner. Let us take the example of Doctor and a Patient. Both have their own lifecycle. A doctor can see multiple patients and a patient can visit multiple doctors.

Association

Association

8. What is Aggregation?

Aggregation is a form of association with all objects having there own lifecycle but there is an ownership. An example here would be like a car belongs to a Person but if the Person object is deleted the car object would not be deleted as it has its own lifecycle.

Aggregation

Aggregation

9. What is Composition?

Composition is a special form of Aggregation. If the owner is destroyed then the owned object is also destroyed. The child object doesn’t have its own lifecycle

Composition

Composition

10. What is Dependency?

Dependency is a a form of relationship in which one object depends on another object. The other may or may not depend on the first object.  An Order object depends on Customer object. An Order Object cannot be created without the Customer object.

Dependency

Dependency

11. What is multiple inheritance? Does Java support multiple inheritance?

Multiple inheritance is a feature in which an object can inherit attributes and methods of the parent object. Let’s look at various types of multiple inheritance and which of them is supported by Java.

Multiple Inheritance of State: This means ability to inherit fields from multiple classes. Java does not permit to extend more than once class. So this type of multiple inheritance is not supported in Java.

Multiple inheritance of implementation: This means the ability to inherit method definitions from multiple classes. Java didn’t support this type of multiple inheritance untile Java 8. In Java 8 Default methods were introduced. A class can implement more than one interface. And these interfaces can have default methods with same signature. The Java compiler provides some rules to determine which default method a particular class uses.

Multiple inheritance of type:  Ability of a class to implement more than one interface. Java supports this type of multiple inheritance.

 

12. Is Java a pure Object Oriented Programming language?

Java is not a pure Object Oriented language.  Java does have support for encapsulation, Abstraction, Inheritance, Polymorphism. But Java fails with a key requirement for pure OOP language which is that every predefined type must be Object. But java has primitive types like int,long, char, float.  In pure OOP language all the operations have to be via Object methods but Java allows arithmetic operations like String a= "hello" + " world!"

 

You may also be interested in:

Like this post? Don’t forget to share it!

Leave a Reply

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