Thursday, April 13, 2023

Java OOPs Interview Questions : 2023

Java is one of the most popular programming languages in the world, and object-oriented programming (OOP) is a key feature of Java. OOPs is a programming paradigm that focuses on objects and their interactions, and Java implements OOPs through classes and objects. If you are preparing for a Java OOPs interview, here are some questions you should be familiar with:

Java OOPs Interview Questions


Java OOPs Interview Questions

1.What  is OOPs?

  1. OOPs(Object-Oriented Programming) is a type of programming which is based on object rather then just functions and procedures.
  2. OOPs means Individual objects are grouped into classes.

2.What are the main features of OOPs?

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

3.What is an object?

  1. An object is a real world entity which is the basic unit of OOPs.
  2. ex. Mango, Apple.
  3. Different objects have different states or attributes, and behaviors.(Read More).

4.What is Class?

  1. A class can be defined as templet/blueprint that describes the behavior/state that the object of its type support.
  2. It is a Group of Objects.
  3. ex. Fruit(Read More).

5.What is Inheritance?

  1. Inheritance is the process in which child class acquires all properties of parent class. by using "extend" keyword.
  2. Main Advantage of Inheritance is Code Reusability.(Read More).

6.Types of Inheritance?

  1. Single Inheritance :- one child class extend one parent class.
  2. Multilevel Inheritance :- Class A extends Class B and Class B extends Class C.
  3. Hierarchical Inheritance :- Many child class extends one parent class.
  4. Hybrid Inheritance :- Combination of single and multilevel inheritance.(Read More).


7.Why Multiple Inheritance not supported in java?

If one Child class extends 2 Parent classes, and if both Parent classes also contain same methods(m1), then which method needs to be implemented in child will be problem (i.e. Ambiguity Problem/ Dimond Problem). Hence Multiple Inheritance not supported in Java.

Example :- 

Suppose you have two classes, ClassA and ClassB, both of which define a method called print(). Now suppose you have a third class, ClassC, which inherits from both ClassA and ClassB. When you call the print() method on an instance of ClassC, there is ambiguity about which version of print() to use, since both ClassA and ClassB have a print() method.

Here is an example of how the diamond problem can occur in Java:



class ClassA
public void print()
 System.out.println("This is ClassA's print() method."); 
 } 
class ClassB
public void print()
 System.out.println("This is ClassB's print() method."); 
 } 
// ClassC inherits from ClassA and ClassB 
class ClassC extends ClassA, ClassB
//... 
// calling print() on an instance of ClassC is ambiguous
ClassC obj = new ClassC(); obj.print();


In this example, when we create an instance of ClassC and call its print() method, we get an error because there is ambiguity about which version of print() to use.

To avoid the diamond problem, Java enforces the single inheritance rule, which means that a class can only inherit from one direct superclass. However, Java does allow a class to implement multiple interfaces, which can provide similar functionality to multiple inheritance.

8.What is Encapsulation?

  1. Encapsulation is the process of binding data(variables) and code(methods) into a single unit.
  2. Main Advantage of Encapsulation is Security.(Read More).
  3. Ex. Capsule.

9.How to achieve Encapsulation?

  1. Encapsulation can be achieved by :
  2. creating Private variables in class.
  3. providing Getter and Setter methods, to make variables accessible only through Methods.

10.What is Abstraction?

  1. Abstraction is the process of hiding the implementation and providing only the necessary data at high level to the users. (details).
  2. ex. Car 

11.How to achieve Abstraction in Java?

  1. Abstract Class (Partial Abstraction).
  2. Interface (Pure Abstraction).

12.What is Abstract Class?

  1. A class is made abstract when only partial implementation of class is known.
  2. It can have abstract and non-abstract methods, constructors and static methods.
  3. It can not be instantiated.

13.What is Interface?

  1. It is set of Contract between Client and Service provider.
  2. Interface Methods always public and abstract.(Since Java 8, it can have default and static methods also).
  3. Interface variables always public static final.
  4. Class can implement any number of interfaces using "implements" keyword.
  5. It is used when all methods of a class should be implemented. 


14.Difference Between Abstract Class and Interface.

Abstract Class

Interface

Abstract Class Contains both abstract and non-abstract methods. Interface Contains only abstract methods.
Since Java 8, it can have default and static methods also.
Abstract class doesn't support multiple inheritance. Interface supports multiple inheritance.
Abstract class can have final, non-final, static and non-static variables. Interface has only static and final variables.
The abstract keyword is used to declare abstract class. The interface keyword is used to declare interface.
A abstract class can have class members like private, protected, etc. Members of a interface are public by default.td>
Abstract class achieves partial abstraction (0 to 100%). Interface achieves fully abstraction (100%).
The abstract class have a constructor. The interface doesn't have a constructor.


15.Anyway we can't create object for abstract class and interface. But abstract class contains constructor, but interface don't?

The main purpose of constructor is to perform initialization of instance variables.
Abstract class can contain instance variables which are required for child object. To perform initialization of those instance variables, constructor is required for abstract class.
But every variable in interface is always public static final and no instance variables will be there in interface. so there is no constructor.

16.What is Polymorphism?

Polymorphism is the process in which one object used in many forms.
It occurs when we have many classes that are related to each other by inheritance.
Real Time Example :- Human.(details).

17.What are the types of Polymorphism?

  1. Static/Compile time Polymorphism.
  2. Dynamic/Run time Polymorphism.

18.What is Method Overloading?

  1. Method overloading is a feature that lets a class have multiple methods with the same name. Here, the method remains the same but the parameters are different.
  2. Method Overloading exists in the same class. There is no need of a superclass and subclass relationship
  3. Method name must be the same
  4. Parameters must be different, the count of parameters does not matter
Parameters must be changed in one of the following three ways:
  1. Types of parameters
  2. Number of parameters
  3. Order of parameters.

19.What is Method Overriding?

The process of implementing superclass method in subclass is called method overriding. As per java principles, classes are closed for modification, so if we want to modify a method in any class, changing the existing method is not a good idea. Instead of that, we should extend that class and override method in child class.


When you are overriding superclass methods in subclass you need to follow certain rules :

  1. The subclass method name must be the same as superclass method name
  2. Subclass method parameters must be the same as superclass method parameters [not even subclass as a parameter is allowed]
  3. Subclass method's return type must be the same as superclass method return type [sub type is allowed]
  4. Subclass method's access modifier must be the same or higher than the superclass method access modifier

superclassIn subclass, we can have access specifier
publicPublic
protectedProtected, Public
defaultDefault, Protected, Public
privateNot possible possible to Override

20.Difference between Runtime Polymorphism and Compile-time Polymorphism.


Runtime Polymorphism

Compile-time Polymorphism

Run time polymorphism where at run time we came to know which method is going to invoke. Compile time polymorphism means binding is occurring at compile time.
It can be achieved through dynamic binding. It can be achieved through static binding.
Inheritance is involved There is no inheritance involved.
Method overriding is an example of runtime polymorphism Method overloading is an example of compile time polymorphism

21.Difference between Method Overloading and Method Overriding.

Method Overloading

Method Overriding

Method overloading is used to increase the readability of the program. Method overriding is used to provide the specific implementation of the method that is already provided by its super class.
Method overloading is performed within class. Method overriding occurs in two classes that have IS-A (inheritance) relationship.
In case of method overloading, parameter must be different. In case of method overriding, parameter must be same.
Method overloading is the example of compile time polymorphism. Method overriding is the example of run time polymorphism.

22.Can we overload a static method in Java?

Yes, we can overload a static method in Java as they all are with different method signatures and no problem will occur.

23.Can we override the static method in Java?

  1. No, we cannot override a static method because static methods belong to a class and resolved at compile-time using the type of reference variable. 
  2. Only method hiding can be achieved as always class variable references will be available for static methods.

24.Can we override a private method in Java.

No, since the private method is only accessible and visible inside the class they are declared, it's not possible to override theme in subclasses.


25.What is the covariant method overriding in Java?

  1. In the covariant overriding, the overriding method can return the subclass of the object returned by the original or overridden method.
  2. ex. overriding clone() method, which is declared in java.lang.Object .


26.Rules for method overriding in Java?

  1. If parent class throws checked exceptions, then child class should throw that exception/or its subtype exception.
  2. But no restriction for unchecked exception. Both Classes are independent.

27.What is Composition?

  1. Without existing container object, if there is no chance of contained object, then container and contained object are strongly associated.
  2. This Strong association is called Composition.
  3. ex. Company(Container Obj), Project-Contained Obj.

28.What is Aggregation?

  1. When the Container are contained objects are weakly associated, (without existing container object , contained objects can be there).
  2. ex. Project(Container) with employees(Contained).
  3. Container objects stores only references of contained obj.

Conclusion:

Java OOP interview questions are essential for any Java developer, and understanding these concepts is crucial for building robust, scalable applications. By mastering these concepts, you will be able to create more efficient and effective code, leading to better overall performance. As such, it is important to study and practice these concepts regularly, to ensure that you are ready to answer any Java OOP interview question that comes your way.


No comments:

Post a Comment