Top Java Architect Interview Questions (2021) | CodeUsingJava
















Most frequently asked Java Architect Interview Questions


  1. What is SOLID?
  2. What do you mean by single Responsibility Principle?
  3. What is open Close Principle (OCP)?
  4. What is liskov Substitution Principle?
  5. Explain Interface Segregation Principle?
  6. What is dependency Inversion Principle?
  7. Why should we use SOLID principles?
  8. How Design Principles are different from Design Patterns?
  9. What are the limitations of Inversion of Control (IoC)
  10. Is the open/closed principle violated by enums in Java?


What is SOLID principle?

SOLID principles are an object-oriented approach that enables us to manage most of the software design problems.SOLID stands for five design principles that aim to make software designs more understandable, flexible, and maintainable.Robert C. Martin is the one who came up with the idea of SOLID principle.These five principles have advanced the concept of object-oriented programming, as well as the way software is written.
The word SOLID acronym for:
  • Single Responsibility Principle (SRP)
  • Open-Closed Principle (OCP)
  • Liskov Substitution Principle (LSP)
  • Interface Segregation Principle (ISP)
  • Dependency Inversion Principle (DIP)


What do you mean by single Responsibility Principle?

according to this theory,"a class should have just one reason to change,"This means that each Java class must do a single functionnality.Every module or class should be in charge of a single functionality by software, and that responsibility should be completely wrapped by the class.It specifies the code and makes it simple to maintain.

public class Employee  
{  
public void show();  
{  
//functionality of the method  
}  
pubic void salary();  
{  
//functionality of the method  
}  
public void designation();  
{  
//functionality of the method  
}  
}  
The single responsibility principle is violated in the above code . To meet the principle's purpose, we should create a distinct class that only performs a single functionality. Employee.java
public class Employee  
{  
public void designation();  
{  
//functionality of the method  
}  
}  
show.java
public class show  
{  
public void show();  
{  
//functionality of the method  
}  
} 

salary.java

public class salary 
{  
public void salary();  
{  
//functionality of the method  
}  
}  

What is open Close Principle (OCP)?

It states that "Software entitles should be open for execution but closed for modification," it says.That is, the code should be designed and written in such a way that new functionality may be introduced with little changes to the existing code. The design should be made in such a way that new functionality can be added as new classes while maintaining as much of the present code as possible.
Example: Suppose, student is a class and it has the method score() that returns the score of the student. animal.java

public class animalinfo   
{  
public int legs(animal al)   
{  
if (al instanceof dog)   
{  
return al.getNumber();  
if (al instanceof snake)   
{  
return st.getNumber();  
}  
}  
If we want to add another subclass named flamingoes then:
animalinfo.java
public class animalInfo   
{  
public int legs()   
{  
//functionality   
}  
}  
public class dog extends animalinfo   
{  
public int legs()   
{  
return this.getValue();  
}  
public class dog extends flamingoes  
{  
public int legs()   
{  
return this.getValue();  
}  


What is liskov Substitution Principle?

It states that a program should be replaceable with instances of their subtypes without affecting the program's correctness.If A is a subtype of B, B instances can be replaced with A instances without affecting programme correctness.If a programme module uses a base class, the reference to the base class can be swapped with a reference to the drive class without impacting the programme module's functionality.So derived classes must be completely substitutable for their base classes.
  public class rectangle 
{  
private double length;  
private double breadth;  
public void setLength(double l)   
{   
length=l;  
}  
public void setBreadth(double b)   
{   
breadth= b;   
}  
... 
}  
public class rect extends rectangle  
{  
public void setLength(double l)   
  
{  
super.setLength(l);  
super.setBreadth(b);  
}  
public void setBreadth(double b)   
{  
super.setLength(l);  
super.setBreadth(b);  
}  
}
The above classes violated the Liskov substitution principle as rect class has extra constraints i.e. length and breadth that should be the same. Therefore, the rectagle class cannot be replaced by rect class. Hence this may result unexpected behavior.

Explain Interface Segregation Principle?

It states that a variety of client-specific interfaces is preferable than a single general-purpose interface.We should not compel clients to implement interfaces that they do not utilise, nor should we create a single large interface that may be broken down into smaller interfaces.Or in other words it means that creating a separate interface and allowing your classes to implement several interfaces is preferable.

What is dependency Inversion Principle?

According to this, one should rely on abstraction rather than concretions. abstraction should not depend on the details where as the detail should depend on abstractions.High level module should not depend on low level module. Example

public class car 
{  
//functionality   
}  
We must have wheels and engine to run a car so we create a constructor of the class and add the instances of the wheels and engine.
public class car 
{  
public final wheel;  
public final engine;  
public car()  
{  
wheel= new wheel();  
engine = new engine(); 
}  
Here by using the new keyword ,have tightly coupled the three classes together so we decouple the car from the wheel by using the wheel interface and this keyword.
public interface Wheel   
{   
//functionality  
}  
car.java

public class car
{  
private final Wheel wheel;  
private final Engine engine;  
public car(Wheel wheel, Engine engine)   
{  
this.wheel = wheel;  
this.engine= engine;  
}  
}  


Why should we use SOLID principles?

  • It minimises dependencies, allowing a code block to be updated without affecting other code blocks.
  • The principles were created with the goal of making design more accessible and understood. The system is maintainable, testable, scalable, and reusable by using the principles.
  • It avoids the software's poor design.

How Design Principles are different from Design Patterns?

Design Principles Design Principles are the fundamental principles that must be followed while developing any software system on any platform and in any programming language.
Design Principles Examples:
  • Abstraction, not concrete classes, as a source of dependency
  • Interfaces, not implementations, are the focus of this programme.
Design Patterns Design Patterns are answers to commonly occurring generic problems; nevertheless, they are not exact programmes that can be used to fix your problem; instead, you must alter them to match your needs. Design Patterns are pre-existing solutions that have been thoroughly evaluated and proven to be safe to employ.
Design Patterns Examples:
  • When you only want one instance of a class, use the single design pattern.
  • To separate different layers of application(Business Repository, Data Repository), use the repository design pattern


What are the limitations of Inversion of Control (IoC) ?

Inversion of Control (IoC) is a design principle that iss used in object-oriented design to achieve loose coupling by inverting various types of controls. Controls refer to any responsibilities that a class has in addition to its core responsibility. this includesControlling the flow of an application, as well as the flow of object creation or dependent object creation and binding.

Is the open/closed principle violated by enums in Java?

OCP does not (and cannot) apply to Enums, so the answer is no. Enums should be complete (i.e., they should contain all possible values) and static (i.e., they should be final and non-mutable). They can be thought of as a small, finite collection of Value Objects.
You can always use a class or create your own "type-safe enumeration" that can be extended if you want something that can be extended.