Link Search Menu Expand Document

OOPS Concept Java Interview Questions

Q1. What are the five types of inheritance?

In Java, inheritance refers to the ability to inherit attributes from one class to another.

The five types of inheritance in Java are:

  • Single-level inheritance
  • Multi-level inheritance
  • Multiple Inheritance
  • Hierarchical Inheritance
  • Hybrid Inheritance

Java does not support multiple inheritances through classes.

Q2. What is aggregation in Java?

The relationship between two objects or two classes, or where the aggregated class shows a reference to the class that owns it, is known as aggregation. In Java, aggregation is mostly seen in the ‘has - a’ relationship, and the class will show a reference to the class in aggregation.

Q3. In Java, what is composition?

When a class holds a reference to another class, it is known as composition. Also, upon an object containing another object, if the stored object does not exist without the presence of the object holding it, it is known as composition. In a way, the composition is specialized aggregation showcasing a strong object-object or class-class relationship.

Q4. How do aggregation and composition differ?

While the composition is similar to aggregation in a way, composition highlights a strong relationship between the container classes or objects and the contained classes and objects. On the other hand, aggregation showcases only a weak connection.

Q5. What is object cloning?

Cloning refers to creating exact copies of the subject in question. Hence, object cloning is the process where a truly precise duplicate of the object is created. The copy is made in the Object class by using the clone() method. Also, it is essential for the class whose object is to be cloned to implement the java.lang.Cloneable interface.

Q6. What is method overloading?

When you create multiple methods with the same name but different signatures using the polymorphism techniques, it is called method overloading. It can be done by either changing the number of arguments or changing the return type.

Q7. What is method overriding?

When a subclass implements the same method that is already implemented by the parent class, it is called method overriding. It can be achieved by naming the method the same as the parent class method, assigning the same signature, and also implementing an is-a relationship between them.

Q8. Why is the static method overriding not doable?

Static methods are class methods and obtain their memory from the class area. Hence, they are tied with the class. On the other hand, instance methods can implement overriding because they are object bound and obtain their memory from the heap memory.

Q9. Differences between method overloading and overriding.

While method overriding aims at increasing the specific implementation of the method provided already by the superclass, overloading helps in expanding the readability of the program. Method overriding always occurs in two classes (with is-a relationship), but overloading ever happens only within a single class.

Q10. Is it possible to override private methods?

Private methods are always limited only in the class and, hence, are not usable outside the class. Thus, private methods cannot undergo overriding.

Other useful articles:


Back to top

© , Java Interviews — All Rights Reserved - Terms of Use - Privacy Policy