0% found this document useful (0 votes)
5 views12 pages

Java OOPs

The document explains the core concepts of Object-Oriented Programming (OOP), highlighting its four main pillars: Encapsulation, Inheritance, Polymorphism, and Abstraction. It emphasizes the importance of these concepts in creating flexible, maintainable, and secure code by organizing it into objects and classes. Additionally, it details how encapsulation is implemented in Java through access modifiers and the use of getter and setter methods.

Uploaded by

Shruti Chauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views12 pages

Java OOPs

The document explains the core concepts of Object-Oriented Programming (OOP), highlighting its four main pillars: Encapsulation, Inheritance, Polymorphism, and Abstraction. It emphasizes the importance of these concepts in creating flexible, maintainable, and secure code by organizing it into objects and classes. Additionally, it details how encapsulation is implemented in Java through access modifiers and the use of getter and setter methods.

Uploaded by

Shruti Chauhan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

‭(runtime‬ ‭polymorphism)‬ ‭or‬ ‭method‬ ‭overloading‬ ‭(compile-time‬

‭Java OOPs‬ ‭ olymorphism).‬


p
‭I. Core OOP Concepts‬ ‭ ‬ ‭Why‬ ‭it‬ ‭matters‬‭:‬ ‭It‬ ‭makes‬ ‭the‬ ‭code‬ ‭more‬ ‭flexible‬ ‭and‬ ‭extensible,‬

‭ .‬ ‭What‬‭is‬‭Object-Oriented‬‭Programming‬‭(OOP)?‬‭Explain‬‭its‬‭four‬
1 ‭allowing‬ ‭the‬ ‭same‬ ‭method‬ ‭to‬ ‭work‬ ‭differently‬ ‭depending‬ ‭on‬ ‭the‬
‭main pillars.‬ ‭object it’s acting on.‬
‭Ans:‬ ‭It‬ ‭is‬ ‭a‬ ‭programming‬ ‭approach‬ ‭that‬ ‭organizes‬‭code‬‭into‬‭objects‬ ‭●‬ ‭Example‬‭:‬ ‭A‬‭makeSound()‬‭method‬‭can‬‭be‬‭called‬‭on‬‭both‬‭Dog‬‭and‬
‭and‬ ‭classes‬ ‭and‬ ‭makes‬ ‭it‬ ‭more‬ ‭structured‬ ‭and‬ ‭easier‬ ‭to‬ ‭manage.‬ ‭A‬ ‭Cat‬ ‭objects,‬‭but‬‭the‬‭actual‬‭sound‬‭made‬‭will‬‭depend‬‭on‬‭the‬‭type‬‭of‬
‭class‬ ‭is‬ ‭a‬ ‭blueprint‬ ‭that‬ ‭defines‬ ‭properties‬ ‭and‬ ‭behaviors,‬ ‭while‬ ‭an‬ ‭object.‬
‭object is an instance of a class representing real-world entities.‬ ‭4. Abstraction‬
‭The Four Main Pillars of OOP:‬ ‭●‬ ‭Definition‬‭:‬ ‭Abstraction‬ ‭involves‬ ‭hiding‬ ‭the‬ ‭implementation‬ ‭details‬
‭of‬ ‭an‬ ‭object‬ ‭and‬ ‭exposing‬ ‭only‬ ‭the‬‭essential‬‭features.‬‭This‬‭allows‬
‭1. Encapsulation‬ ‭the‬ ‭user‬ ‭to‬ ‭interact‬ ‭with‬ ‭the‬ ‭object‬ ‭without‬ ‭worrying‬ ‭about‬ ‭its‬
‭●‬ ‭Definition‬‭:‬ ‭Encapsulation‬ ‭is‬ ‭the‬ ‭concept‬ ‭of‬ ‭bundling‬ ‭the‬ ‭data‬ ‭internal workings.‬
‭(attributes)‬‭and‬‭methods‬‭(functions)‬‭that‬‭operate‬‭on‬‭that‬‭data‬‭into‬ ‭●‬ ‭Why‬ ‭it‬ ‭matters‬‭:‬ ‭It‬ ‭simplifies‬ ‭interaction‬ ‭with‬ ‭objects‬ ‭and‬ ‭helps‬ ‭in‬
‭a‬ ‭single‬ ‭unit‬ ‭(class).‬ ‭It‬ ‭also‬ ‭involves‬ ‭restricting‬ ‭direct‬ ‭access‬ ‭to‬ ‭managing‬ ‭complexity‬ ‭by‬ ‭focusing‬ ‭on‬ ‭what‬ ‭an‬ ‭object‬ ‭does‬ ‭rather‬
‭some of the object's internal details, by using access specifiers.‬ ‭than how it does it.‬
‭●‬ ‭Why‬ ‭it‬ ‭matters‬‭:‬ ‭It‬ ‭helps‬ ‭to‬ ‭protect‬ ‭the‬ ‭internal‬ ‭state‬‭of‬‭an‬‭object,‬ ‭●‬ ‭Example‬‭:‬ ‭A‬ ‭Car‬ ‭class‬ ‭may‬ ‭have‬ ‭a‬ ‭drive()‬ ‭method‬ ‭that‬ ‭abstracts‬
‭ensuring that data is only modified in controlled ways.‬ ‭away the details of how the car’s engine works.‬
‭●‬ ‭Example‬‭:‬‭In‬‭a‬‭BankAccount‬‭class,‬‭the‬‭balance‬‭is‬‭private,‬‭and‬‭you‬
‭can‬‭only‬‭modify‬‭it‬‭through‬‭methods‬‭like‬‭deposit()‬‭or‬‭withdraw(),‬‭not‬
‭directly.‬ ‭Key Takeaways:‬
‭2. Inheritance‬ ‭●‬ ‭Encapsulation‬‭:‬ ‭Keep‬ ‭data‬ ‭private‬ ‭and‬ ‭expose‬ ‭only‬ ‭necessary‬
‭●‬ ‭Definition‬‭:‬ ‭Inheritance‬ ‭allows‬ ‭one‬ ‭class‬ ‭(child‬ ‭class)‬ ‭to‬ ‭inherit‬ ‭functionality.‬
‭properties‬ ‭and‬ ‭methods‬ ‭from‬ ‭another‬ ‭class‬ ‭(parent‬ ‭class).‬ ‭This‬ ‭●‬ ‭Inheritance‬‭:‬ ‭Promote‬ ‭reuse‬ ‭through‬ ‭parent-child‬ ‭class‬
‭supports the‬‭is-a‬‭relationship and promotes code reuse.‬ ‭relationships.‬
‭●‬ ‭Why‬ ‭it‬ ‭matters‬‭:‬ ‭It‬ ‭minimizes‬ ‭code‬‭duplication‬‭by‬‭allowing‬‭a‬‭class‬ ‭●‬ ‭Polymorphism‬‭:‬ ‭Write‬ ‭flexible‬ ‭code‬ ‭that‬ ‭can‬ ‭work‬ ‭with‬ ‭different‬
‭to reuse and extend the functionality of an existing class.‬ ‭objects via the same interface.‬
‭●‬ ‭Example‬‭:‬ ‭A‬‭Dog‬‭class‬‭can‬‭inherit‬‭from‬‭an‬‭Animal‬‭class,‬‭inheriting‬ ‭●‬ ‭Abstraction‬‭: Hide complexity and expose only essential details.‬
‭common‬ ‭behaviors‬ ‭like‬ ‭eat(),‬ ‭and‬ ‭adding‬ ‭specific‬ ‭behaviors‬ ‭like‬
‭bark().‬ ‭ .‬
2 ‭Explain‬‭Encapsulation‬‭in‬‭detail.‬‭What‬‭are‬‭its‬‭benefits‬‭and‬
‭3. Polymorphism‬ ‭how is it implemented in Java?‬
‭●‬ ‭Definition‬‭:‬ ‭Polymorphism‬ ‭allows‬ ‭an‬ ‭object‬ ‭to‬ ‭take‬ ‭many‬‭forms.‬‭It‬ ‭Ans:‬ ‭Encapsulation‬ ‭is‬ ‭all‬ ‭about‬ ‭data‬ ‭hiding‬ ‭and‬ ‭information‬
‭enables‬ ‭one‬ ‭interface‬ ‭(method)‬ ‭to‬ ‭be‬ ‭used‬ ‭for‬ ‭objects‬‭of‬‭different‬ ‭hiding‬‭.‬ ‭In‬ ‭simple‬‭terms,‬‭it‬‭involves‬‭bundling‬‭the‬‭data‬‭(fields)‬‭and‬‭the‬
‭classes.‬ ‭This‬ ‭can‬ ‭be‬ ‭achieved‬ ‭through‬ ‭method‬ ‭overriding‬ ‭methods‬ ‭(behaviors)‬ ‭that‬ ‭operate‬ ‭on‬ ‭the‬ ‭data‬ ‭into‬ ‭a‬ ‭single‬ ‭unit‬ ‭or‬
‭class,‬ ‭while‬ ‭restricting‬ ‭access‬ ‭to‬ ‭some‬ ‭of‬ ‭the‬ ‭object's‬ ‭components.‬
‭This‬ ‭ensures‬ ‭that‬ ‭the‬ ‭internal‬ ‭workings‬ ‭of‬ ‭an‬ ‭object‬ ‭are‬ ‭protected‬
f‭rom‬ ‭unintended‬ ‭or‬ ‭harmful‬ ‭modification,‬ ‭and‬ ‭it‬‭provides‬‭a‬‭controlled‬ be empty"‬
‭ );‬

‭way for other code to interact with the object.‬ }‬

}‬

‭ ow Encapsulation is Implemented in Java:‬
H
‭In‬ ‭Java,‬ ‭encapsulation‬ ‭is‬ ‭typically‬ ‭achieved‬ ‭using‬ ‭access‬‭modifiers‬ ‭/ Public getter for age‬
/
‭to control the visibility of fields and methods:‬ public‬‭
‭ int‬‭
getAge‬
() {‬

‭●‬ ‭Private‬ ‭Fields‬‭:‬ ‭The‬ ‭fields‬ ‭(variables)‬‭of‬‭a‬‭class‬‭are‬‭made‬‭private,‬ return‬‭
‭ age;‬
‭which‬ ‭means‬ ‭they‬ ‭cannot‬ ‭be‬ ‭accessed‬ ‭directly‬ ‭from‬ ‭outside‬ ‭the‬
}‬

‭class.‬
‭●‬ ‭Public‬ ‭Getters‬ ‭and‬ ‭Setters‬‭:‬ ‭To‬ ‭provide‬ ‭controlled‬ ‭access‬ ‭to‬ ‭the‬
‭/ Public setter for age with validation‬
/
‭fields,‬ ‭public‬ ‭getter‬ ‭and‬ ‭setter‬ ‭methods‬ ‭are‬ ‭created.‬ ‭The‬ ‭getter‬
public‬‭
‭ void‬‭ setAge‬
(‬
‭ int‬‭
‭ age) {‬
‭allows‬ ‭external‬ ‭code‬ ‭to‬‭retrieve‬‭the‬‭value‬‭of‬‭a‬‭field,‬‭and‬‭the‬‭setter‬
if‬‭
‭ (age >=‬‭18‬) {‬

‭allows external code to update the value.‬
this‬
‭ .age = age;‬

‭ his‬‭is‬‭the‬‭most‬‭common‬‭way‬‭of‬‭implementing‬‭encapsulation,‬‭though‬
T }‬‭
‭ else‬‭
{‬
‭other‬‭methods‬‭can‬‭also‬‭be‬‭used‬‭to‬‭provide‬‭controlled‬‭access,‬‭such‬‭as‬ throw‬ ‭
‭ new‬ ‭
IllegalArgumentException(‬
"Age‬ ‭
‭ must‬ ‭
be‬
‭validation‬‭in setters or‬‭read-only fields‬‭.‬ at least 18"‬
‭ );‬

}‬

‭Example:‬ }‬

‭public class Employee {‬ }‬

‭/‬ ‭
/ Private‬ ‭
fields,‬ ‭
cannot‬ ‭
be‬ ‭
accessed‬ ‭
directly‬ ‭
outside‬ ‭ enefits of Encapsulation:‬
B
the class‬
‭ ‭1.‬ ‭Data Protection & Security‬‭:‬
private‬‭
‭ String name;‬ ‭Encapsulation‬‭helps‬‭in‬‭protecting‬‭the‬‭data‬‭.‬‭By‬‭making‬‭the‬‭fields‬
private‬‭
‭ int‬‭
age;‬ ‭private‬‭,‬ ‭we‬ ‭prevent‬ ‭external‬ ‭classes‬ ‭from‬ ‭directly‬ ‭modifying‬ ‭the‬
‭internal‬ ‭state‬ ‭of‬ ‭an‬ ‭object.‬ ‭Any‬ ‭modification‬ ‭can‬ ‭only‬ ‭happen‬ ‭via‬
‭/ Public getter for name‬
/ ‭the‬ ‭setter‬ ‭method,‬ ‭which‬ ‭can‬ ‭contain‬ ‭validation‬ ‭logic.‬ ‭This‬
public‬‭
‭ String‬‭
getName‬
() {‬
‭ ‭prevents invalid states‬‭and promotes data integrity.‬
return‬‭
‭ name;‬ ‭○‬ ‭For‬ ‭example,‬ ‭the‬ ‭setter‬ ‭for‬ ‭age‬ ‭in‬ ‭the‬ ‭above‬ ‭example‬
}‬
‭ ‭ensures‬ ‭that‬ ‭only‬ ‭valid‬ ‭ages‬ ‭(>=‬ ‭18)‬ ‭can‬ ‭be‬ ‭set.‬ ‭This‬
‭prevents the object from entering an invalid state.‬
‭/ Public setter for name with validation‬
/ ‭2.‬ ‭Improved Maintainability‬‭:‬
public‬‭
‭ void‬‭ setName‬
(String name) {‬
‭ ‭With‬‭encapsulation,‬‭the‬‭internal‬‭structure‬‭of‬‭the‬‭class‬‭can‬‭change‬
if‬‭
‭ (name !=‬‭ null‬‭
&& !name.isEmpty()) {‬ ‭without‬ ‭affecting‬ ‭external‬ ‭code.‬ ‭For‬ ‭example,‬ ‭you‬ ‭could‬‭decide‬‭to‬
this‬
‭ .name = name;‬
‭ ‭store‬ ‭the‬ ‭employee’s‬ ‭age‬ ‭as‬ ‭a‬ ‭Date‬ ‭object‬ ‭instead‬ ‭of‬ ‭an‬ ‭int,‬ ‭and‬
}‬‭
‭ else‬‭
{‬ ‭external‬ ‭code‬ ‭that‬ ‭uses‬ ‭the‬ ‭Employee‬ ‭class‬ ‭would‬ ‭not‬ ‭need‬ ‭to‬
throw‬ ‭
‭ new‬ ‭
IllegalArgumentException(‬
"Name‬ ‭
‭ cannot‬
‭ hange‬ ‭because‬ ‭they’re‬ ‭interacting‬ ‭with‬ ‭the‬ ‭public‬ ‭getter‬ ‭and‬
c private‬‭
‭ int‬‭
age;‬
‭setter methods, not the internal field.‬
‭3.‬ ‭Flexibility‬‭:‬ ‭ublic‬‭
p int‬‭
getAge‬
() {‬

‭Encapsulation‬ ‭allows‬ ‭you‬ ‭to‬ ‭change‬ ‭the‬ ‭implementation‬ ‭details‬ return‬‭
‭ age;‬
‭without‬ ‭affecting‬ ‭other‬ ‭parts‬ ‭of‬ ‭your‬ ‭program.‬ ‭For‬ ‭example,‬ ‭you‬ }‬

‭might‬‭later‬‭decide‬‭to‬‭add‬‭logging‬‭or‬‭caching‬‭to‬‭the‬‭getter‬‭methods,‬
‭and‬ ‭you‬ ‭can‬ ‭do‬‭this‬‭without‬‭changing‬‭the‬‭code‬‭that‬‭calls‬‭getAge()‬ ‭ublic‬‭
p void‬‭
setAge‬
(‬
‭ int‬‭
‭ age) {‬
‭or getName().‬
this‬
‭ .age = age;‬

‭4.‬ ‭Security‬‭:‬
}‬

‭Encapsulation‬ ‭enables‬ ‭the‬ ‭creation‬ ‭of‬ ‭read-only‬ ‭fields‬ ‭(by‬ ‭only‬
}‬

‭providing‬ ‭a‬ ‭getter‬ ‭method,‬ ‭and‬ ‭no‬ ‭setter)‬ ‭or‬ ‭write-only‬ ‭fields‬ ‭(by‬
‭only‬ ‭providing‬ ‭a‬ ‭setter‬ ‭method,‬ ‭and‬ ‭no‬ ‭getter).‬ ‭You‬ ‭can‬ ‭also‬ ‭After Change:‬
‭provide‬ ‭restricted‬ ‭access‬ ‭to‬ ‭certain‬ ‭fields‬ ‭to‬ ‭specific‬ ‭classes‬ ‭or‬ import‬‭
‭ java.util.Date;‬
‭packages using‬‭protected‬‭or‬‭default‬‭access modifiers.‬
‭5.‬ ‭Ease of Debugging‬‭:‬ public‬‭
‭ class‬‭
Employee‬‭
{‬
‭Encapsulation‬ ‭helps‬ ‭with‬‭debugging‬‭because‬‭you‬‭can‬‭control‬‭the‬ private‬‭
‭ Date birthDate;‬
‭behavior‬‭of‬‭a‬‭class‬‭through‬‭its‬‭public‬‭methods.‬‭For‬‭example,‬‭if‬‭you‬
‭detect‬‭an‬‭issue‬‭with‬‭how‬‭the‬‭age‬‭field‬‭is‬‭being‬‭set,‬‭you‬‭can‬‭debug‬ ‭ublic‬‭
p Date‬‭
getBirthDate‬
() {‬

‭and‬ ‭modify‬ ‭just‬ ‭the‬ ‭setter‬ ‭method‬ ‭without‬ ‭affecting‬ ‭other‬ ‭parts‬ ‭of‬ return‬‭
‭ birthDate;‬
‭the code.‬ }‬

‭6.‬ ‭Loose Coupling‬‭:‬
‭Encapsulation‬ ‭promotes‬ ‭loose‬ ‭coupling‬ ‭between‬ ‭components.‬ ‭ublic‬‭
p void‬‭
setBirthDate‬
(Date birthDate) {‬

‭External‬ ‭classes‬ ‭don't‬ ‭need‬ ‭to‬ ‭know‬ ‭how‬ ‭an‬ ‭object‬ ‭works‬ this‬
‭ .birthDate = birthDate;‬

‭internally,‬ ‭they‬ ‭just‬ ‭interact‬ ‭with‬ ‭its‬ ‭public‬ ‭methods.‬ ‭This‬ ‭allows‬ }‬

‭changes‬ ‭to‬ ‭the‬ ‭internal‬ ‭implementation‬ ‭without‬ ‭breaking‬ ‭external‬
‭code that relies on the public interface.‬ ‭/ Calculate age from birth date (example)‬
/
public‬‭
‭ int‬‭
getAge‬
() {‬

‭ xample‬ ‭of‬ ‭Changing‬ ‭Internal‬ ‭Implementation‬ ‭Without‬‭Breaking‬
E // Logic to calculate age from birthDate‬

‭External Code:‬ }‬

‭Let's‬‭say‬‭we‬‭change‬‭the‬‭Employee‬‭class‬‭to‬‭store‬‭the‬‭employee’s‬‭age‬ }‬

‭as‬ ‭a‬ ‭Date‬ ‭object‬ ‭(to‬ ‭record‬ ‭the‬ ‭date‬ ‭of‬ ‭birth).‬ ‭Because‬ ‭of‬ I‭n‬ ‭this‬ ‭case,‬ ‭all‬ ‭external‬ ‭code‬ ‭remains‬ ‭unaffected‬ ‭because‬ ‭they‬ ‭are‬
‭encapsulation,‬ ‭any‬ ‭external‬ ‭class‬ ‭that‬ ‭interacts‬ ‭with‬ ‭Employee‬ ‭only‬ ‭only‬ ‭calling‬ ‭getAge()‬ ‭and‬ ‭setAge(),‬ ‭which‬ ‭are‬ ‭still‬ ‭available‬ ‭(just‬
‭uses‬ ‭the‬ ‭getter‬ ‭and‬ ‭setter‬ ‭methods.‬ ‭The‬ ‭code‬ ‭that‬ ‭uses‬ ‭the‬ ‭implemented differently).‬
‭Employee class doesn’t need to change.‬ ‭In Summary:‬
‭Before Change:‬ ‭●‬ ‭Encapsulation‬ ‭helps‬ ‭protect‬ ‭data,‬ ‭promotes‬ ‭data‬ ‭integrity,‬ ‭and‬
‭public class Employee {‬ ‭allows for flexible and maintainable code.‬
‭●‬ I‭t‬ ‭hides‬ ‭the‬ ‭internal‬ ‭details‬ ‭of‬ ‭a‬ ‭class‬ ‭and‬ ‭exposes‬ ‭only‬ ‭the‬
‭necessary‬‭functionality‬‭via‬‭controlled‬‭access‬‭methods‬‭(getters‬‭and‬
‭setters).‬ ‭ he‬ ‭super‬ ‭Keyword:‬ ‭The‬ ‭super‬ ‭keyword‬ ‭is‬ ‭used‬‭to‬‭refer‬‭to‬‭the‬‭parent‬
T
‭●‬ ‭This‬ ‭is‬ ‭key‬ ‭for‬ ‭separation‬ ‭of‬ ‭concerns‬‭,‬ ‭maintainability‬‭,‬ ‭and‬ ‭class from the child class. Here’s how you can use it:‬
‭security‬‭in large-scale systems.‬ ‭●‬ ‭Access‬ ‭parent‬ ‭class‬ ‭methods‬ ‭(if‬ ‭they’re‬ ‭overridden‬ ‭in‬ ‭the‬ ‭child‬
‭class):‬
super‬
‭ .methodName();‬

‭‬ C
● ‭ all the parent class constructor‬‭: super();‬
‭ .‬
3 ‭Describe‬‭Inheritance‬‭in‬‭Java.‬‭What‬‭are‬‭the‬‭different‬‭types‬
‭●‬ ‭Access‬ ‭parent‬ ‭class‬ ‭variables‬ ‭(if‬ ‭there's‬ ‭a‬ ‭conflict‬ ‭between‬
‭of‬ ‭inheritance‬ ‭supported,‬ ‭and‬ ‭how‬ ‭is‬ ‭the‬ ‭super‬ ‭keyword‬ ‭used?‬
‭parent and child):‬
‭What are its limitations?‬
‭Ans:‬ ‭In‬ ‭Java,‬ ‭inheritance‬ ‭allows‬ ‭a‬ ‭class‬ ‭(child‬ ‭class)‬ ‭to‬ ‭inherit‬ super‬
‭ .variableName;‬

‭properties‬‭and‬‭behaviors‬‭(methods)‬‭from‬‭another‬‭class‬‭(parent‬‭class).‬
‭It's‬ ‭mainly‬ ‭used‬ ‭to‬ ‭promote‬ ‭code‬ ‭reusability‬ ‭and‬ ‭to‬ ‭represent‬ ‭an‬
‭"is-a"‬ ‭relationship.‬ ‭For‬ ‭example,‬ ‭if‬ ‭you‬ ‭have‬ ‭a‬ ‭Dog‬ ‭class‬ ‭and‬ ‭an‬ ‭Limitations of Inheritance:‬
‭Animal class, Dog is a type of Animal.‬ ‭1.‬ ‭Tight‬ ‭Coupling‬‭:‬ ‭Child‬ ‭classes‬ ‭depend‬ ‭heavily‬ ‭on‬‭parent‬‭classes.‬
‭If‬‭the‬‭parent‬‭class‬‭changes,‬‭all‬‭child‬‭classes‬‭may‬‭need‬‭to‬‭change,‬
‭Types of Inheritance in Java:‬ ‭which can break things or require a lot of refactoring.‬
‭1.‬ ‭Single Inheritance‬‭: A class can inherit from only one parent class.‬ ‭2.‬ ‭Harder‬ ‭to‬ ‭Refactor‬‭:‬ ‭Since‬ ‭inheritance‬ ‭tightly‬ ‭couples‬ ‭classes,‬
‭changing‬ ‭the‬ ‭parent‬ ‭class‬ ‭can‬ ‭lead‬ ‭to‬ ‭a‬ ‭lot‬‭of‬‭ripple‬‭effects‬‭in‬‭the‬
‭lass‬‭
c Animal‬‭
{ }‬ ‭child classes.‬
class‬‭
‭ Dog‬‭
extends‬‭
Animal‬‭
{ }‬ ‭3.‬ ‭No‬‭Multiple‬‭Inheritance‬‭with‬‭Classes‬‭:‬‭Java‬‭doesn't‬‭allow‬‭a‬‭class‬
‭2.‬ ‭Multilevel‬ ‭Inheritance‬‭:‬ ‭A‬ ‭class‬ ‭can‬ ‭inherit‬ ‭from‬ ‭another‬ ‭class,‬ ‭to‬‭inherit‬‭from‬‭more‬‭than‬‭one‬‭class,‬‭which‬‭avoids‬‭complexity‬‭(e.g.,‬
‭which in turn inherits from another class.‬ ‭the‬ ‭diamond‬ ‭problem).‬ ‭But‬ ‭you‬ ‭can‬ ‭still‬ ‭achieve‬ ‭multiple‬
‭inheritance via‬‭interfaces‬‭.‬
‭lass‬‭
c Animal‬‭{ }‬
class‬‭
‭ Dog‬‭
extends‬‭Animal‬‭
{ }‬
‭ .‬
4 ‭Explain‬ ‭Abstraction‬ ‭in‬ ‭Java.‬ ‭What‬ ‭are‬ ‭the‬ ‭key‬ ‭differences‬
class‬‭
‭ Puppy‬‭
extends‬‭Dog‬‭
{ }‬
‭between‬‭Abstract‬‭Classes‬‭and‬‭Interfaces?‬‭When‬‭would‬‭you‬‭choose‬
‭3.‬ ‭Hierarchical‬ ‭Inheritance‬‭:‬ ‭Multiple‬ ‭classes‬ ‭can‬ ‭inherit‬ ‭from‬ ‭a‬ ‭one over the other?‬
‭single parent class.‬
‭ ns:‬ ‭Abstraction‬ ‭in‬ ‭Java‬ ‭is‬ ‭about‬ ‭simplifying‬ ‭complexity‬ ‭by‬
A
‭lass‬‭
c Animal‬‭
{ }‬ ‭exposing‬‭only‬‭relevant‬‭details‬‭and‬‭hiding‬‭the‬‭internal‬‭logic.‬‭It's‬‭one‬
class‬‭
‭ Dog‬‭
extends‬‭
Animal‬‭
{ }‬ ‭of the core OOP principles and helps keep code modular and clean.‬
class‬‭
‭ Cat‬‭
extends‬‭
Animal‬‭
{ }‬
‭ ava‬ ‭supports‬ ‭abstraction‬ ‭through‬ ‭both‬ ‭abstract‬ ‭classes‬ ‭and‬
J
‭ ote‬‭:‬‭Java‬‭does‬‭not‬‭support‬‭multiple‬‭inheritance‬‭with‬‭classes‬‭to‬‭avoid‬
N ‭interfaces‬‭.‬ ‭While‬ ‭both‬ ‭let‬‭you‬‭define‬‭methods‬‭without‬‭implementations,‬
‭issues‬ ‭like‬ ‭the‬ ‭diamond‬ ‭problem‬ ‭(ambiguity‬ ‭when‬ ‭inheriting‬ ‭from‬ ‭there are key differences in how and when they’re used.‬
‭multiple classes). But you can achieve similar behavior using‬‭interfaces‬‭.‬
‭●‬ A ‭ bstract‬ ‭classes‬ ‭can‬ ‭have‬ ‭both‬ ‭abstract‬ ‭and‬ ‭concrete‬ ‭methods,‬ ‭ ohesion‬ ‭is‬ ‭about‬ ‭how‬ ‭focused‬ ‭a‬ ‭class‬ ‭is‬ ‭on‬ ‭a‬ ‭single‬ ‭responsibility.‬ ‭If‬
C
‭instance‬ ‭variables,‬ ‭and‬ ‭constructors.‬ ‭They're‬ ‭typically‬ ‭used‬ ‭when‬ ‭UserService‬ ‭is‬ ‭doing‬ ‭everything‬ ‭—‬ ‭from‬ ‭creating‬ ‭users,‬ ‭to‬ ‭validating‬
‭multiple‬ ‭related‬ ‭classes‬ ‭share‬ ‭common‬ ‭functionality.‬ ‭Since‬ ‭Java‬ ‭passwords,‬ ‭to‬ ‭sending‬ ‭emails‬ ‭and‬ ‭generating‬ ‭reports‬ ‭—‬ ‭it‬ ‭has‬ ‭low‬
‭allows‬‭only‬‭single‬‭inheritance‬‭for‬‭classes,‬‭abstract‬‭classes‬‭are‬‭best‬ ‭cohesion‬‭. It becomes harder to manage, test, and extend.‬
‭when there's a clear inheritance hierarchy‬
‭●‬ ‭Interfaces‬‭,‬ ‭especially‬ ‭after‬ ‭Java‬ ‭8,‬ ‭are‬ ‭more‬ ‭about‬ ‭defining‬ ‭a‬ ‭In a better design, we would split this into:‬
‭contract.‬ ‭Earlier‬ ‭they‬ ‭only‬ ‭allowed‬ ‭abstract‬ ‭methods‬ ‭and‬ ‭●‬ ‭UserService – handles user-related business logic‬
‭constants,‬ ‭but‬ ‭now‬‭they‬‭support‬‭default‬‭,‬‭static‬‭,‬‭and‬‭even‬‭private‬ ‭●‬ ‭AuthService – handles authentication‬
‭methods.‬‭Interfaces‬‭are‬‭better‬‭suited‬‭when‬‭unrelated‬‭classes‬‭need‬ ‭●‬ ‭EmailService – handles email notifications‬
‭to‬ ‭follow‬ ‭the‬ ‭same‬ ‭behavior‬ ‭—‬ ‭like‬ ‭Comparable,‬ ‭Serializable,‬ ‭or‬ ‭●‬ ‭AuditService – handles audit logging‬
‭domain-specific contracts like Auditable.‬ ‭ his‬ ‭creates‬ ‭high‬ ‭cohesion‬‭,‬ ‭because‬ ‭each‬ ‭class‬ ‭now‬ ‭has‬ ‭a‬ ‭clear‬‭and‬
T
‭ ne‬ ‭big‬ ‭difference‬ ‭is‬ ‭inheritance‬ ‭—‬ ‭a‬ ‭class‬ ‭can‬ ‭extend‬ ‭only‬ ‭one‬
O ‭focused responsibility.‬
‭abstract‬ ‭class‬ ‭but‬ ‭can‬ ‭implement‬ ‭multiple‬ ‭interfaces‬‭,‬ ‭making‬ I‭n‬ ‭summary‬‭:‬ ‭Loose‬ ‭coupling‬ ‭allows‬ ‭components‬ ‭to‬ ‭evolve‬
‭interfaces more flexible when designing loosely coupled systems.‬ ‭independently,‬ ‭and‬ ‭high‬ ‭cohesion‬ ‭ensures‬ ‭each‬ ‭class‬ ‭is‬ ‭focused‬ ‭and‬
I‭n‬‭short,‬‭use‬‭an‬‭abstract‬‭class‬‭when‬‭you‬‭have‬‭shared‬‭state‬‭or‬‭logic‬‭,‬ ‭easier‬‭to‬‭maintain.‬‭Both‬‭are‬‭critical‬‭in‬‭keeping‬‭even‬‭a‬‭simple‬‭system‬‭like‬
‭and‬ ‭use‬ ‭interfaces‬ ‭when‬ ‭you're‬ ‭defining‬ ‭capabilities‬ ‭that‬ ‭can‬ ‭cut‬ ‭user management clean, testable, and scalable.‬
‭across‬ ‭unrelated‬ ‭classes‬ ‭—‬ ‭especially‬ ‭when‬ ‭you‬ ‭need‬ ‭multiple‬
‭inheritance of behavior‬ ‭ onc‬ ‭Bad Example in UMS‬ ‭Good Example in UMS‬
C
‭ept‬
‭II. Advanced OOP Concepts & Design Principles‬
‭ oupl‬ U
C ‭ serService‬‭creates‬‭its‬ U
‭ serService‬ ‭depends‬ ‭on‬
‭ing‬ ‭own DB connection‬ ‭UserRepository interface‬
‭ .‬‭What‬‭are‬‭Coupling‬‭and‬‭Cohesion‬‭in‬‭the‬‭context‬‭of‬‭OOP?‬‭Why‬‭are‬
6
‭ ohe‬ U
C ‭ serService‬ ‭handles‬ R‭ esponsibilities‬ ‭split‬ ‭across‬
‭they important‬
‭sion‬ ‭auth, email, and audit‬ ‭UserService,‬ ‭AuthService,‬
‭Ans:‬ ‭Coupling‬ ‭is‬‭about‬‭how‬‭much‬‭one‬‭class‬‭depends‬‭on‬‭another.‬‭Let’s‬
‭EmailService‬
‭say‬ ‭the‬ ‭UserService‬ ‭class‬ ‭directly‬ ‭creates‬ ‭and‬ ‭manages‬ ‭a‬
‭DatabaseConnection‬ ‭object.‬ ‭That’s‬ ‭an‬ ‭example‬ ‭of‬ ‭tight‬ ‭coupling‬‭,‬
‭because‬ ‭UserService‬ ‭is‬ ‭now‬ ‭directly‬ ‭dependent‬ ‭on‬ ‭the‬ ‭database‬
‭implementation.‬ ‭If‬ ‭we‬ ‭later‬ ‭decide‬ ‭to‬ ‭switch‬ ‭from‬ ‭MySQL‬ ‭to‬ ‭MongoDB,‬
‭we’d have to modify UserService.‬ ‭ .‬ ‭Differentiate‬ ‭between‬ ‭Association,‬ ‭Aggregation,‬ ‭and‬
7
‭Composition‬‭in‬‭Java‬‭with‬‭examples.‬‭Keywords:‬‭Ans:‬‭In‬‭Association‬‭,‬
I‭nstead,‬ ‭if‬ ‭UserService‬ ‭depends‬‭on‬‭a‬‭UserRepository‬‭interface‬‭and‬‭that‬ ‭two‬‭classes‬‭are‬‭just‬‭related‬‭—‬‭like‬‭a‬‭User‬‭and‬‭a‬‭Role.‬‭They’re‬‭aware‬‭of‬
‭is‬ ‭injected‬ ‭via‬ ‭a‬ ‭framework‬ ‭like‬ ‭Spring,‬ ‭the‬ ‭service‬ ‭becomes‬ ‭loosely‬ ‭each‬ ‭other,‬ ‭but‬ ‭neither‬ ‭owns‬ ‭the‬ ‭other.‬ ‭It’s‬ ‭the‬ ‭most‬ ‭general‬ ‭form,‬ ‭and‬
‭coupled‬‭.‬ ‭The‬ ‭underlying‬ ‭data‬ ‭source‬ ‭can‬ ‭change‬ ‭without‬ ‭affecting‬ ‭the‬ ‭both can live independently.‬
‭service logic — making the code more flexible and testable.‬
‭In‬ ‭Aggregation‬‭,‬ ‭there's‬ ‭a‬ ‭whole-part‬ ‭relationship,‬ ‭but‬ ‭with‬ ‭shared‬ ‭ oint‬ ‭to‬ ‭a‬ ‭different‬ ‭object,‬ ‭but‬ ‭the‬‭object‬‭itself‬‭can‬‭still‬‭be‬‭modified‬‭if‬‭it’s‬
p
‭ wnership.‬‭For‬‭example,‬‭a‬‭Department‬‭has‬‭Students,‬‭but‬‭those‬‭students‬
o ‭mutable.‬
‭still‬‭exist‬‭if‬‭the‬‭department‬‭is‬‭deleted.‬‭So,‬‭aggregation‬‭adds‬‭structure‬‭but‬ ‭Final‬‭method:‬‭Cannot‬‭be‬‭overridden‬‭by‬‭subclasses.‬‭This‬‭is‬‭useful‬‭when‬
‭still keeps the objects loosely coupled.‬ ‭you‬‭want‬‭to‬‭lock‬‭down‬‭certain‬‭behavior‬‭to‬‭prevent‬‭accidental‬‭changes‬‭in‬
‭child classes.‬
‭ omposition‬ ‭is‬ ‭tighter‬ ‭—‬ ‭it’s‬ ‭an‬ ‭exclusive‬ ‭ownership.‬ ‭A‬ ‭User‬ ‭might‬
C ‭Final‬ ‭class:‬ ‭Cannot‬ ‭be‬ ‭subclassed.‬ ‭This‬ ‭is‬ ‭often‬ ‭done‬ ‭to‬ ‭prevent‬
‭have‬‭a‬‭UserProfile,‬‭and‬‭that‬‭profile‬‭doesn’t‬‭make‬‭sense‬‭without‬‭the‬‭user.‬ ‭inheritance‬ ‭altogether,‬ ‭ensuring‬ ‭the‬ ‭class’s‬ ‭design‬ ‭remains‬ ‭intact.‬
‭If‬ ‭the‬ ‭user‬ ‭is‬ ‭deleted,‬ ‭so‬ ‭is‬ ‭the‬ ‭profile.‬ ‭That’s‬ ‭a‬ ‭strong‬ ‭lifecycle‬ ‭Examples‬ ‭include‬ ‭String‬ ‭and‬ ‭wrapper‬ ‭classes‬ ‭like‬ ‭Integer.‬
‭dependency.‬

‭ o‬ ‭in‬ ‭short:‬ ‭all‬ a


S ‭ re‬ ‭associations,‬ ‭but‬ ‭aggregation‬ i‭mplies‬ ‭shared‬ ‭ 0.‬ ‭What‬ ‭are‬ ‭Design‬ ‭Patterns‬ ‭in‬ ‭OOP?‬ ‭Name‬ ‭and‬ ‭briefly‬ ‭explain‬ ‭a‬
1
‭ownership,‬ ‭while‬ ‭composition‬ ‭means‬ ‭full‬ ‭ownership‬ ‭and‬ ‭dependent‬ ‭few commonly used ones.‬
‭lifecycle‬
‭Ans:‬‭Some common design patterns include:‬
‭●‬ ‭Singleton‬‭:‬ ‭Ensures‬ ‭only‬ ‭one‬ ‭instance‬ ‭of‬ ‭a‬ ‭class‬ ‭exists‬ ‭and‬
‭ . Explain the usage and implications of the static keyword in Java‬
8 ‭provides‬ ‭a‬ ‭global‬ ‭access‬ ‭point.‬ ‭Useful‬ ‭for‬ ‭managing‬ ‭shared‬
‭Ans:‬ ‭The‬ ‭static‬ ‭keyword‬ ‭in‬ ‭Java‬ ‭is‬ ‭used‬ ‭to‬ ‭define‬ ‭members‬ ‭that‬ ‭resources like configurations or connection pools.‬
‭belong to the class rather than any particular instance.‬ ‭●‬ ‭Factory‬ ‭Method:‬ ‭Defines‬ ‭an‬ ‭interface‬ ‭for‬‭creating‬‭objects‬‭but‬‭lets‬
‭●‬ ‭Static‬ ‭variables‬ ‭are‬ ‭shared‬ ‭across‬ ‭all‬ ‭instances‬ ‭of‬ ‭a‬ ‭class.‬ ‭subclasses‬ ‭decide‬ ‭which‬ ‭class‬‭to‬‭instantiate.‬‭This‬‭promotes‬‭loose‬
‭There’s‬ ‭only‬ ‭one‬ ‭copy,‬ ‭stored‬ ‭in‬ ‭the‬ ‭method‬ ‭area,‬ ‭which‬ ‭helps‬ ‭coupling by hiding the object creation logic.‬
‭save memory when the same data applies to all objects.‬ ‭●‬ ‭Strategy‬‭:‬ ‭Encapsulates‬ ‭a‬ ‭family‬ ‭of‬ ‭algorithms,‬ ‭making‬ ‭them‬
‭●‬ ‭Static‬ ‭methods‬ ‭can‬ ‭be‬ ‭called‬ ‭without‬ ‭creating‬ ‭an‬ ‭object.‬ ‭They‬ ‭interchangeable‬ ‭at‬ ‭runtime.‬ ‭Useful‬ ‭when‬ ‭you‬ ‭want‬ ‭to‬ ‭select‬ ‭an‬
‭can’t‬ ‭directly‬ ‭access‬ ‭non-static‬ ‭(instance)‬ ‭variables‬ ‭or‬ ‭methods‬ ‭algorithm dynamically without changing the client code.‬
‭because they don’t have a reference to a specific object.‬ ‭●‬ ‭Observer‬‭:‬ ‭Establishes‬ ‭a‬ ‭one-to-many‬ ‭relationship‬ ‭where‬ ‭multiple‬
‭●‬ ‭Static‬ ‭blocks‬ ‭are‬ ‭executed‬ ‭once‬ ‭when‬ ‭the‬ ‭class‬ ‭is‬ ‭loaded,‬ ‭observers‬ ‭are‬ ‭notified‬ ‭automatically‬ ‭when‬ ‭the‬ ‭subject’s‬ ‭state‬
‭typically used for initializing static variables.‬ ‭changes. Commonly used in event-driven systems‬
‭●‬ ‭Static‬ ‭nested‬ ‭classes‬ ‭are‬‭inner‬‭classes‬‭declared‬‭static;‬‭they‬‭can‬
‭access‬ ‭the‬ ‭outer‬ ‭class’s‬ ‭static‬ ‭members‬ ‭without‬ ‭needing‬ ‭an‬ ‭ 1.‬ ‭Briefly‬ ‭explain‬ ‭the‬ ‭SOLID‬ ‭principles‬ ‭of‬ ‭OOP.‬ ‭Why‬ ‭are‬ ‭they‬
1
‭instance.‬ ‭important?‬
‭ enefits‬‭are‬‭memory‬‭efficiency‬‭and‬‭easy‬‭global‬‭access‬‭to‬‭utility‬‭methods‬
B ‭Ans:‬ ‭The‬ ‭SOLID‬ ‭principles‬‭are‬‭a‬‭set‬‭of‬‭design‬‭guidelines‬‭in‬‭OOP‬‭that‬
‭or‬ ‭constants.‬ ‭However,‬ ‭overusing‬ ‭static‬ ‭can‬ ‭cause‬ ‭tight‬ ‭coupling‬ ‭and‬ ‭help‬ ‭in‬ ‭writing‬ ‭maintainable,‬‭scalable,‬‭and‬‭testable‬‭code.‬‭Each‬‭principle‬
‭make‬ ‭unit‬ ‭testing‬ ‭harder‬ ‭because‬ ‭static‬ ‭members‬ ‭are‬ ‭hard‬ ‭to‬ ‭mock‬ ‭or‬ ‭targets‬ ‭a‬ ‭specific‬ ‭area‬ ‭of‬ ‭code‬ ‭design‬ ‭to‬ ‭reduce‬ ‭coupling‬ ‭and‬ ‭increase‬
‭override‬ ‭flexibility.‬
‭1.‬ ‭S – Single Responsibility Principle (SRP):‬
‭ .‬ ‭Discuss‬ ‭the‬ ‭final‬ ‭keyword‬ ‭in‬ ‭Java‬ ‭as‬ ‭applied‬ ‭to‬ ‭variables,‬
9 ‭A class should have only one reason to change.‬
‭methods, and classes.‬ 👉
‭ ‬ ‭It‬ ‭helps‬ ‭keep‬ ‭classes‬ ‭focused‬ ‭and‬ ‭easier‬ ‭to‬ ‭maintain.‬ ‭For‬
‭Ans:‬ ‭Final‬ ‭variable:‬ ‭Once‬ ‭initialized,‬ ‭its‬ ‭value‬ ‭can’t‬ ‭be‬ ‭changed.‬ ‭For‬ ‭example,‬‭in‬‭a‬‭User‬‭Management‬‭module,‬‭user‬‭validation‬‭and‬‭user‬
‭primitives,‬ ‭it‬ ‭means‬ ‭a‬ ‭constant‬ ‭value.‬ ‭For‬ ‭objects,‬ ‭the‬ ‭reference‬ ‭can’t‬ ‭persistence should be handled in separate classes.‬
‭2.‬ ‭O – Open/Closed Principle (OCP):‬ public‬‭
‭ final‬‭
class‬‭
User‬‭
{‬
‭Classes should be open for extension but closed for modification.‬ private‬‭
‭ final‬‭
String name;‬
👉
‭ ‬‭You‬‭can‬‭add‬‭new‬‭behavior‬‭without‬‭changing‬‭existing‬‭code.‬‭For‬ private‬‭
‭ final‬‭
List<String> roles;‬
‭instance,‬ ‭adding‬ ‭a‬ ‭new‬ ‭type‬ ‭of‬ ‭notification‬ ‭(like‬ ‭SMS)‬ ‭without‬
‭altering the EmailNotification class.‬ ‭ublic‬‭
p User‬
(String name, List<String> roles) {‬

‭3.‬ ‭L – Liskov Substitution Principle (LSP):‬ this‬
‭ .name = name;‬

‭Subtypes‬ ‭should‬ ‭be‬ ‭usable‬ ‭in‬ ‭place‬ ‭of‬ ‭their‬ ‭parent‬ ‭type‬ ‭without‬ this‬
‭ .roles‬ ‭
‭ =‬ ‭
new‬ ‭
ArrayList<>(roles);‬ ‭
//‬ ‭
Defensive‬
‭breaking the program.‬
copy‬

👉
‭ ‬‭It‬‭ensures‬‭that‬‭inheritance‬‭is‬‭correctly‬‭applied.‬‭For‬‭example,‬‭if‬
}‬

‭AdminUser‬ ‭extends‬ ‭User,‬ ‭it‬ ‭should‬ ‭behave‬ ‭correctly‬ ‭anywhere‬ ‭a‬
‭User is expected.‬
‭ublic‬‭
p String‬‭
getName‬
() {‬

‭4.‬ ‭I – Interface Segregation Principle (ISP):‬
return‬‭
‭ name;‬
‭Don't force a class to implement methods it doesn’t need.‬
}‬

👉
‭ ‬‭Keep‬‭interfaces‬‭lean‬‭and‬‭focused.‬‭For‬‭instance,‬‭instead‬‭of‬‭one‬
‭big‬ ‭UserOperations‬ ‭interface,‬ ‭break‬ ‭it‬ ‭down‬ ‭into‬ ‭ReadableUser,‬
‭ublic‬‭
p List<String>‬‭
getRoles‬
() {‬

‭EditableUser, etc., based on role or use.‬
return‬ ‭
‭ new‬ ‭
ArrayList<>(roles);‬ ‭
//‬ ‭
Defensive‬ ‭
copy‬
‭5.‬ ‭D – Dependency Inversion Principle (DIP):‬
again‬

‭High-level‬‭modules‬‭should‬‭not‬‭depend‬‭on‬‭low-level‬‭modules.‬‭Both‬
}‬

‭should depend on abstractions.‬
}‬

👉
‭ ‬‭Improves‬‭flexibility.‬‭Instead‬‭of‬‭a‬‭service‬‭tightly‬‭depending‬‭on‬‭a‬
‭concrete‬ ‭database‬ ‭class,‬ ‭it‬ ‭should‬ ‭depend‬ ‭on‬ ‭an‬ ‭interface‬ ‭like‬
‭💡 Why immutability matters:‬
‭UserRepository,‬ ‭which‬ ‭can‬ ‭be‬ ‭easily‬ ‭swapped‬ ‭(e.g.,‬ ‭for‬ ‭testing‬‭or‬
‭●‬ ‭Thread safety by default‬‭– no need for synchronization.‬
‭switching databases).‬
‭●‬ ‭Safe to cache‬‭– state never changes, so it can be reused.‬
‭●‬ ‭Stable keys in collections‬‭– ideal for HashMap or Set.‬
‭ 2.‬ ‭What‬‭is‬‭immutability‬‭in‬‭Java?‬‭How‬‭do‬‭you‬‭create‬‭an‬‭immutable‬
1
‭●‬ ‭Fewer‬‭side‬‭effects‬‭–‬‭makes‬‭debugging‬‭and‬‭reasoning‬‭about‬‭code‬
‭object, and why is it beneficial?‬
‭easier.‬
‭Ans:‬ ‭Immutability‬ ‭means‬ ‭that‬ ‭once‬ ‭an‬ ‭object‬ ‭is‬ ‭created,‬ ‭its‬ ‭state‬
‭cannot‬ ‭be‬ ‭changed.‬ ‭In‬ ‭Java,‬ ‭immutable‬ ‭objects‬‭are‬‭commonly‬‭used‬‭for‬
‭safer,‬ ‭more‬ ‭predictable‬ ‭behavior‬ ‭—‬ ‭especially‬ ‭in‬ ‭multi-threaded‬ ‭‬ ‭Overusing‬ ‭immutability‬ ‭in‬ ‭performance-sensitive‬ ‭areas‬ ‭(like‬
o
‭environments.‬ ‭large lists) can create extra garbage.‬
‭✅ How to create an immutable class:‬ o‬ ‭Serialization‬‭can break immutability if deserialized incorrectly.‬

‭1.‬ ‭Mark the class final‬‭– so it can’t be extended.‬ o‬ ‭Avoid exposing mutable internals via getters.‬

‭2.‬ ‭Make all fields private and final‬‭– to prevent reassignment.‬
‭3.‬ ‭No setters‬‭– expose only what’s absolutely necessary via getters.‬
‭4.‬ ‭Defensive‬‭copying‬‭–‬‭if‬‭any‬‭field‬‭is‬‭a‬‭mutable‬‭object‬‭(like‬‭a‬‭List‬‭or‬
‭13. How do Generics enhance OOP in Java? Explain Type Erasure.‬
‭Date),‬ ‭clone‬ ‭it‬ ‭during‬ ‭construction‬ ‭and‬ ‭when‬ ‭returning‬ ‭it‬ ‭from‬
‭getters.‬
‭ nswer:‬‭Generics‬ ‭mainly‬ ‭improve‬ ‭type‬ ‭safety‬ ‭and‬ ‭reduce‬ ‭boilerplate‬ ‭in‬
A ‭ 4.‬ ‭What‬ ‭is‬ ‭Java‬ ‭Reflection?‬ ‭When‬ ‭is‬ ‭it‬ ‭useful,‬ ‭and‬ ‭what‬ ‭are‬ ‭its‬
1
‭Java.‬ ‭In‬ ‭object-oriented‬ ‭design,‬ ‭we‬ ‭often‬ ‭work‬ ‭with‬ ‭data‬‭structures‬‭that‬ ‭potential downsides?‬
‭operate‬ ‭on‬ ‭different‬ ‭types‬ ‭—‬ ‭like‬ ‭lists,‬ ‭maps,‬ ‭etc.‬ ‭Generics‬ ‭let‬ ‭us‬ ‭write‬ ‭Expectation:‬
‭that‬ ‭logic‬ ‭once‬ ‭and‬ ‭reuse‬ ‭it‬ ‭with‬ ‭any‬ ‭object‬ ‭type,‬ ‭while‬ ‭still‬ ‭catching‬ ‭●‬ ‭Definition:‬ ‭Reflection‬ ‭is‬ ‭the‬ ‭ability‬ ‭of‬ ‭a‬ ‭program‬ ‭to‬ ‭examine‬ ‭or‬
‭type-related bugs at compile time.‬ ‭modify its own structure (classes, methods, fields) at runtime.‬
‭●‬ ‭Use Cases:‬
‭ or‬ ‭example,‬ ‭without‬ ‭generics,‬ ‭I’d‬ ‭have‬ ‭to‬ ‭cast‬ ‭objects‬ ‭manually‬ ‭when‬
F ‭○‬ ‭Developing IDEs, debuggers, or testing tools.‬
‭working‬ ‭with‬ ‭collections,‬ ‭which‬ ‭is‬ ‭error-prone.‬ ‭With‬ ‭generics,‬ ‭I‬ ‭get‬ ‭○‬ ‭Frameworks‬ ‭(e.g.,‬ ‭Spring‬ ‭for‬ ‭dependency‬ ‭injection,‬ ‭ORMs‬
‭compile-time checks and don’t need casting:‬ ‭for mapping database rows to objects).‬
‭ist<String> names =‬‭
L new‬‭
ArrayList<>();‬ ‭○‬ ‭Dynamic proxy objects.‬
String name = names.get(‬
‭ 0‭
‭)‬;‬‭
// No cast needed‬ ‭○‬ ‭Serialization/Deserialization libraries.‬
‭●‬ ‭Downsides:‬
‭○‬ ‭Performance‬ ‭Overhead:‬ ‭Reflection‬ ‭is‬ ‭slower‬ ‭than‬ ‭direct‬
🔹
‭ Type Erasure (and why it matters):‬ ‭calls.‬
‭What‬ ‭many‬ ‭people‬ ‭don't‬ ‭realize‬ ‭is‬ ‭that‬ ‭generics‬ ‭only‬ ‭exist‬ ‭at‬ ‭compile‬ ‭○‬ ‭Security‬ ‭Vulnerabilities:‬ ‭Can‬ ‭bypass‬ ‭access‬ ‭modifiers,‬
‭time.‬‭Once‬‭the‬‭code‬‭is‬‭compiled,‬‭the‬‭type‬‭info‬‭is‬‭removed‬‭—‬‭that's‬‭called‬ ‭potentially exposing private members.‬
‭type erasure‬‭.‬ ‭○‬ ‭Increased‬‭Complexity:‬‭Code‬‭using‬‭reflection‬‭can‬‭be‬‭harder‬
‭to read, understand, and maintain.‬
‭ o‬ ‭List<String>‬ ‭and‬ ‭List<Integer>‬ ‭both‬ ‭become‬ ‭just‬ ‭List‬ ‭at‬ ‭runtime.‬
S
‭○‬ ‭Breaks‬‭Encapsulation:‬‭Directly‬‭accessing‬‭private‬‭members‬
‭That’s why I can’t do something like:‬
‭goes against encapsulation.‬
‭f‬‭
i (list‬‭
instanceof‬‭
List<String>) { }‬‭
// Won't compile‬
Or create arrays like‬‭
‭ new‬‭
T[‬
10‬
‭ ].‬

‭III. Scenario-Based & Best Practice Questions‬
🔹
‭ Why this happens:‬
‭This‬‭design‬‭choice‬‭keeps‬‭Java‬‭backward‬‭compatible‬‭with‬‭older‬‭versions‬ ‭ 5.‬ ‭You‬ ‭need‬ ‭to‬ ‭design‬ ‭a‬ ‭system‬ ‭where‬ ‭various‬ ‭"Shape"‬ ‭objects‬
1
‭—‬ ‭so‬ ‭that‬ ‭code‬ ‭using‬ ‭raw‬ ‭types‬ ‭still‬ ‭works,‬ ‭even‬ ‭though‬ ‭it‬ ‭sacrifices‬ ‭(Circle,‬ ‭Square,‬ ‭Triangle)‬ ‭can‬ ‭calculate‬ ‭their‬ ‭area‬ ‭and‬ ‭perimeter.‬
‭some runtime flexibility.‬ ‭How‬ ‭would‬ ‭you‬ ‭apply‬ ‭OOP‬ ‭principles,‬ ‭specifically‬ ‭choosing‬
‭between‬ ‭an‬ ‭Abstract‬ ‭Class‬ ‭and‬ ‭an‬ ‭Interface‬ ‭for‬ ‭the‬ ‭"Shape"‬
‭🔹 In short:‬ ‭concept?‬
‭●‬ ‭Generics‬ ‭help‬ ‭keep‬ ‭code‬ ‭reusable,‬ ‭safe,‬ ‭and‬ ‭maintainable‬ ‭—‬ ‭●‬ ‭OOP‬ ‭application:‬ ‭Discuss‬ ‭creating‬ ‭a‬ ‭Shape‬ ‭base‬‭(abstract‬‭class‬
‭especially in collection-heavy logic.‬ ‭or‬‭interface),‬‭with‬‭concrete‬‭subclasses‬‭for‬‭Circle,‬‭Square,‬‭Triangle.‬
‭●‬ ‭Type‬ ‭erasure‬ ‭is‬ ‭the‬ ‭behind-the-scenes‬ ‭mechanism‬ ‭that‬ ‭removes‬ ‭Polymorphism allows treating all shapes uniformly.‬
‭the generic types after compilation.‬
‭●‬ ‭As‬ ‭a‬ ‭developer,‬ ‭I‬ ‭still‬ ‭get‬ ‭all‬ ‭the‬ ‭benefits‬ ‭at‬ ‭compile‬ ‭time,‬ ‭even‬ ‭if‬ ‭hape s1 =‬‭
S new‬‭
Circle(‬
5‬
‭ );‬

‭they don't exist at runtime.‬ Shape s2 =‬‭
‭ new‬‭
Square(‬
4‬
‭ );‬

Shape s3 =‬‭
‭ new‬‭
Triangle(‬3‭
‭,
‬‬‭
4‭
,
‬‬‭
5‭
)
‬;‬
System.out.println(s1.calculateArea());‬

‭ystem.out.println(s2.calculatePerimeter()‬
S public‬‭
‭ class‬‭
ConsoleLogger‬‭
implements‬‭
Logger‬‭
{‬
);‬
‭ public‬‭
‭ void‬‭
log‬
(LogLevel level, String message) {‬

‭●‬ A ‭ bstract‬ ‭Class‬ ‭choice:‬ ‭If‬ ‭Shape‬ ‭needs‬ ‭common‬ ‭fields‬ ‭(e.g.,‬ ‭id,‬ System.out.println(‬
‭ "["‬‭
‭ + level +‬‭
"] "‬‭
+ message);‬
‭color)‬ ‭or‬ ‭shared‬ ‭default‬ ‭implementations‬ ‭for‬ ‭some‬ ‭methods‬ ‭(e.g.,‬ }‬

‭displayInfo()), and all shapes‬‭are‬‭fundamentally related.‬ }‬

‭●‬ ‭Interface‬ ‭choice:‬ ‭If‬ ‭Shape‬ ‭is‬ ‭purely‬ ‭a‬ ‭contract‬ ‭for‬ ‭defining‬
‭behavior‬ ‭(calculateArea(),‬ ‭calculatePerimeter())‬ ‭and‬ ‭different‬ public‬‭
‭ class‬‭
FileLogger‬‭
implements‬‭
Logger‬‭
{‬
‭shape‬ ‭implementations‬ ‭might‬ ‭come‬ ‭from‬ ‭entirely‬ ‭unrelated‬ private‬‭
‭ FileWriter writer;‬
‭hierarchies.‬ ‭Emphasize‬ ‭that‬ ‭for‬ ‭this‬‭specific‬‭scenario,‬‭either‬‭could‬
‭work‬ ‭depending‬ ‭on‬ ‭exact‬ ‭requirements,‬ ‭but‬ ‭an‬ ‭interface‬ ‭is‬ ‭often‬ ‭ublic‬‭
p FileLogger‬
(String filePath) {‬

‭preferred for defining contracts like Measurable.‬ try‬‭
‭ {‬
this‬
‭ .writer =‬‭
‭ new‬‭
FileWriter(filePath,‬‭
true‬
);‬

}‬‭
‭ catch‬‭
(IOException e) {‬
e.printStackTrace();‬

‭ 6.‬ ‭Imagine‬ ‭you‬ ‭are‬ ‭building‬ ‭a‬‭logging‬‭framework.‬‭How‬‭would‬‭you‬
1
}‬

‭design‬ ‭the‬ ‭core‬ ‭components‬ ‭using‬ ‭OOP‬ ‭principles‬ ‭to‬ ‭allow‬ ‭for‬
‭different‬‭logging‬‭destinations‬‭(console,‬‭file,‬‭database)‬‭and‬‭different‬ }‬

‭logging levels (INFO, WARN, ERROR)?‬
‭ublic‬‭
p void‬‭
log‬
(LogLevel level, String message) {‬

‭ ns:‬
A try‬‭
‭ {‬

‭ 1. Abstraction – Core Logger Interface‬ writer.write(‬
‭ "["‬ ‭
‭ +‬ ‭
level‬ ‭
+‬ ‭
"]‬ ‭
"‬ ‭
+‬ ‭
message‬ ‭
+‬
‭The‬ ‭first‬ ‭step‬ ‭is‬ ‭defining‬ ‭a‬ ‭common‬ ‭abstraction‬ ‭that‬ ‭all‬ ‭logger‬ "\n"‬
‭ );‬

‭implementations will follow.‬ writer.flush();‬

public‬‭
‭ interface‬‭
Logger‬‭
{‬ }‬‭
‭ catch‬‭
(IOException e) {‬
void‬‭
‭ log‬
(LogLevel level, String message);‬
‭ e.printStackTrace();‬

}‬
‭ }‬

}‬

}‬


‭ 2. LogLevel Enum‬
‭Define logging levels as an enum for type safety and clarity.‬
public‬‭
‭ class‬‭
DatabaseLogger‬‭
implements‬‭ Logger‬‭ {‬
public‬‭
‭ enum‬‭
LogLevel {‬ public‬‭
‭ void‬‭
log‬
(LogLevel level, String message) {‬

INFO, WARN, ERROR;‬
‭ // Stubbed logic, assume a DB connection exists‬

}‬
‭ System.out.println(‬
‭ "Logged‬ ‭
‭ to‬ ‭
DB:‬ ‭
["‬ ‭
+‬ ‭
level‬ ‭
+‬ ‭
"]‬‭
"‬
+ message);‬

}‬


‭ 3. Polymorphism via Strategy Pattern – Logger Implementations‬
}‬

‭Each‬ ‭logging‬ ‭destination‬ ‭(Console,‬ ‭File,‬ ‭Database)‬ ‭will‬ ‭implement‬ ‭the‬
‭Logger interface with its own strategy for output.‬

‭ 4. Encapsulation‬ ‭ublic‬‭
p void‬‭
log‬
(LogLevel level, String message) {‬

‭Each‬ ‭logger‬ ‭encapsulates‬ ‭the‬ ‭specific‬ ‭logic‬ ‭for‬ ‭its‬ ‭destination.‬ ‭For‬ if‬‭
‭ (‭
t
‬his‬
.level == level) {‬

‭example,‬‭the‬‭FileLogger‬‭manages‬‭file‬‭writing‬‭internally‬‭and‬‭exposes‬‭only‬ write(message);‬

‭the log method.‬ }‬

if‬‭
‭ (next !=‬‭
null‬) {‬


‭ 5. Factory Pattern – LoggerFactory‬ next.log(level, message);‬

‭A factory helps create loggers based on config (like properties or XML).‬ }‬

}‬

public‬‭
‭ class‬‭
LoggerFactory‬‭{‬
public‬‭
‭ static‬‭
Logger‬‭getLogger‬(String type) {‬

protected‬‭
‭ abstract‬‭void‬‭
write‬
(String message);‬

return‬‭
‭ switch‬‭
(type.toLowerCase()) {‬
‭‬
}
case‬‭
‭ "console"‬‭
->‬‭new‬‭
ConsoleLogger();‬
Then we extend it:‬

case‬‭
‭ "file"‬‭
->‬‭
new‬‭FileLogger(‬"logs.txt"‬
‭ );‬

public‬‭
‭ class‬‭
ErrorLogger‬‭extends‬‭
AbstractLogger‬‭
{‬
case‬‭
‭ "database"‬‭
->‬‭new‬‭DatabaseLogger();‬
public‬‭
‭ ErrorLogger‬
() {‬

default‬
‭ ->‬
‭ throw‬
‭ new‬

super‬
‭ (LogLevel.ERROR);‬

IllegalArgumentException(‬
‭ "Unknown logger type"‬
‭ );‬

}‬

};‬

}‬

‭rotected‬‭
p void‬‭
write‬
(String message) {‬

}‬

System.err.println(‬
‭ "[ERROR] "‬‭
‭ + message);‬
}‬

‭‬
}

‭ 6. Chain of Responsibility – For Log Level Filtering‬
Set up
‭ the chain:‬
‭Suppose‬ ‭we‬ ‭want‬ ‭a‬ ‭logger‬ ‭to‬ ‭handle‬ ‭only‬ ‭certain‬ ‭levels.‬ ‭We‬ ‭can‬
Logger
‭ errorLogger =‬‭
new‬‭
ErrorLogger();‬
‭implement this pattern to delegate logging through a chain.‬
Logger
‭ warnLogger =‬‭
new‬‭
WarnLogger();‬
public‬‭
‭ abstract‬‭class‬‭
AbstractLogger‬‭
implements‬‭
Logger‬‭
{‬ Logger
‭ infoLogger =‬‭
new‬‭
InfoLogger();‬
protected‬‭
‭ LogLevel level;‬
protected‬‭
‭ Logger next;‬ ‭rrorLogger.setNext(warnLogger);‬
e
warnLogger.setNext(infoLogger);‬

‭ublic‬‭
p AbstractLogger‬
(LogLevel level) {‬

this‬
‭ .level = level;‬
‭ ‭/ Now use errorLogger as the head of the chain‬
/
}‬
‭ errorLogger.log(LogLevel.WARN,‬‭
‭ "This is a warning."‬
);‬

‭ublic‬‭
p void‬‭
setNext‬
(Logger nextLogger) {‬

this‬
‭ .next = nextLogger;‬
‭ ‭🔄 Extensibility & Maintenance‬
}‬
‭ ‭●‬ ‭Adding a new destination?‬‭Just implement Logger (Strategy).‬
‭●‬ ‭Changing‬ ‭log‬ ‭creation‬ ‭logic?‬ ‭Update‬ ‭the‬ ‭LoggerFactory‬
‭(Factory).‬
‭●‬ F
‭ iltering‬ ‭log‬ ‭levels?‬ ‭Adjust‬ ‭the‬ ‭chain‬ ‭handlers‬ ‭(Chain‬ ‭of‬ ·‭ ‬ ‭Modularity:‬ ‭OOP‬ ‭encourages‬ ‭breaking‬ ‭down‬ ‭the‬ ‭system‬ ‭into‬
‭Responsibility).‬ ‭smaller,‬ ‭manageable‬ ‭pieces—each‬ ‭responsible‬ ‭for‬ ‭a‬ ‭specific‬ ‭part‬ ‭of‬
‭the functionality—which is crucial in large systems.‬
‭·‬ ‭Collaboration:‬‭Well-defined‬‭interfaces‬‭allow‬‭different‬‭teams‬‭to‬
‭✅ Summary of Patterns Used‬ ‭work‬‭on‬‭different‬‭modules‬‭simultaneously‬‭without‬‭interfering,‬‭enabling‬
‭parallel development and better coordination.‬
‭Pattern‬ ‭Purpose‬ ‭·‬ ‭Testability:‬‭When‬‭your‬‭classes‬‭are‬‭cohesive‬‭and‬‭encapsulated,‬
‭it’s‬ ‭easier‬ ‭to‬ ‭write‬ ‭focused‬ ‭unit‬ ‭tests‬ ‭for‬ ‭each‬ ‭component,‬ ‭improving‬
‭Strategy‬ ‭ ifferent‬ ‭logging‬ ‭behaviors‬
D ‭reliability.‬
‭via polymorphism‬ ‭·‬ ‭Reduced‬ ‭Complexity:‬ ‭Abstraction‬ ‭helps‬ ‭hide‬ ‭complex‬‭details‬
‭Factory‬ ‭ reation‬ ‭logic‬ ‭abstracted‬
C ‭behind‬ ‭simple‬ ‭interfaces,‬ ‭so‬ ‭developers‬ ‭don’t‬ ‭need‬ ‭to‬ ‭understand‬
‭away from clients‬ ‭every part of the system to work effectively.‬
‭·‬ ‭Extensibility:‬‭Polymorphism‬‭and‬‭inheritance‬‭let‬‭your‬‭application‬
‭ hain‬
C ‭of‬ F
‭ iltering‬ ‭by‬ ‭log‬ ‭level‬ ‭or‬ ‭evolve smoothly as requirements change without major rewrites.‬
‭Responsibility‬ ‭conditional handling‬

‭Encapsulation‬ ‭Hide implementation details‬ ‭ nterprise‬


E ‭OOP Principle(s)‬ ‭How OOP Helps‬
‭Challenge‬
‭Abstraction‬ ‭ ommon‬ ‭Logger‬ ‭interface‬ ‭for‬
C
‭loose coupling‬ ‭Maintainability‬ E
‭ ncapsulation,‬ I‭solate‬ ‭changes,‬ ‭prevent‬
‭Modularity‬ ‭side effects‬

‭Scalability‬ ‭ pen/Closed‬
O ‭ dd‬
A ‭features‬ ‭without‬
‭Principle (OCP)‬ ‭breaking existing code‬
‭ 7.‬ ‭How‬ ‭do‬ ‭OOP‬ ‭principles‬ ‭help‬ ‭in‬ ‭building‬ ‭large-scale,‬
1
‭enterprise-level applications?‬ ‭Reusability‬ I‭nheritance,‬ ‭ euse‬
R ‭code‬ ‭across‬
‭·‬ ‭Maintainability:‬ ‭Encapsulation‬ ‭and‬ ‭modularity‬ ‭mean‬ ‭each‬ ‭Interfaces‬ ‭modules/projects‬
‭component‬‭hides‬‭its‬‭internal‬‭details‬‭and‬‭exposes‬‭a‬‭clear‬‭interface.‬‭So‬
‭when‬‭you‬‭need‬‭to‬‭fix‬‭bugs‬‭or‬‭add‬‭features,‬‭you‬‭can‬‭do‬‭so‬‭in‬‭isolation‬ ‭Modularity‬ ‭Classes, Objects‬ ‭ reak‬
B ‭complexity‬ ‭into‬
‭without breaking the entire system.‬ ‭manageable units‬
‭·‬ ‭Scalability:‬‭Thanks‬‭to‬‭the‬‭Open/Closed‬‭Principle,‬‭you‬‭can‬‭add‬
‭Collaboration‬ I‭nterfaces,‬ ‭ arallel‬ ‭work‬ ‭with‬ ‭clear‬
P
‭new‬ ‭features‬ ‭by‬ ‭extending‬ ‭existing‬ ‭classes‬ ‭or‬ ‭implementing‬
‭Abstraction‬ ‭contracts‬
‭interfaces‬ ‭without‬ ‭modifying‬ ‭stable‬ ‭code.‬ ‭This‬ ‭makes‬ ‭the‬ ‭system‬
‭scalable and reduces regression risks‬ ‭Testability‬ ‭ ohesion,‬
C I‭solate‬ ‭components‬ ‭for‬ ‭unit‬
‭·‬ ‭Reusability:‬‭Inheritance‬‭and‬‭interfaces‬‭promote‬‭writing‬‭reusable‬ ‭Encapsulation‬ ‭tests‬
‭components‬ ‭that‬ ‭can‬ ‭be‬ ‭shared‬ ‭across‬ ‭modules‬ ‭or‬ ‭even‬ ‭different‬
‭projects, cutting down duplication and speeding up development.‬ ‭ educed‬
R ‭Abstraction‬ ‭ ide‬ ‭complexity‬
H ‭behind‬
‭Complexity‬ ‭simple interfaces‬
‭difficult‬ ‭to‬ ‭understand‬ ‭or‬ ‭change.‬ ‭They‬ ‭also‬ ‭can‬ ‭lead‬ ‭to‬
‭Extensibility‬ ‭ olymorphism,‬
P ‭ volve‬‭system‬‭without‬‭major‬
E ‭ nexpected side effects when behavior is inherited unexpectedly.‬
u
‭Inheritance‬ ‭rewrites‬ ‭●‬ ‭How‬ ‭I‬ ‭avoid‬ ‭it:‬ ‭I‬ ‭prefer‬ ‭composition‬ ‭over‬ ‭inheritance‬ ‭where‬
‭possible.‬ ‭I‬ ‭keep‬ ‭inheritance‬ ‭hierarchies‬ ‭shallow‬ ‭and‬ ‭use‬
‭interfaces‬ ‭to‬ ‭define‬ ‭behavior‬ ‭without‬ ‭forcing‬ ‭implementation‬
‭inheritance.‬ ‭This‬ ‭makes‬ ‭the‬ ‭design‬ ‭more‬ ‭flexible‬ ‭and‬ ‭easier‬ ‭to‬
‭ 8.‬ ‭What‬ ‭are‬ ‭some‬ ‭common‬ ‭OOP‬ ‭pitfalls‬ ‭you've‬ ‭encountered,‬ ‭and‬
1 ‭maintain.‬
‭how do you avoid them?‬ ‭5.‬ ‭Design by Copy-Paste‬
‭Copying‬ ‭code‬ ‭instead‬ ‭of‬ ‭abstracting‬‭reusable‬‭logic‬‭leads‬‭to‬‭code‬
‭Ans: Common OOP‬ ‭duplication,‬‭which‬‭makes‬‭bugs‬‭harder‬‭to‬‭fix‬‭and‬‭features‬‭harder‬‭to‬
‭1. God Object / Monolithic Class‬ ‭extend.‬
‭This‬ ‭is‬ ‭when‬ ‭a‬ ‭single‬ ‭class‬ ‭tries‬ ‭to‬ ‭do‬ ‭too‬ ‭much—handling‬ ‭multiple‬ ‭●‬ ‭How‬‭I‬‭avoid‬‭it:‬‭I‬‭constantly‬‭look‬‭for‬‭common‬‭patterns‬‭and‬‭refactor‬
‭responsibilities,‬ ‭managing‬ ‭too‬ ‭many‬ ‭data‬ ‭points,‬ ‭or‬ ‭controlling‬ ‭different‬ ‭duplicated‬ ‭code‬ ‭into‬ ‭shared‬ ‭methods‬‭or‬‭utility‬‭classes‬‭.‬‭When‬
‭parts‬ ‭of‬ ‭the‬ ‭system.‬ ‭It‬ ‭becomes‬ ‭a‬ ‭maintenance‬ ‭nightmare‬ ‭and‬ ‭a‬ ‭appropriate,‬‭I‬‭use‬‭inheritance‬‭or‬‭composition‬‭to‬‭reuse‬‭logic‬‭without‬
‭bottleneck.‬ ‭copying.‬
‭●‬ ‭How‬ ‭I‬ ‭avoid‬ ‭it:‬ ‭I‬ ‭rigorously‬ ‭apply‬ ‭the‬ ‭Single‬ ‭Responsibility‬ ‭6.‬ ‭Over-engineering and Premature Optimization‬
‭Principle‬ ‭(SRP)‬‭.‬ ‭I‬ ‭break‬‭down‬‭large‬‭classes‬‭into‬‭smaller,‬‭focused‬ ‭Sometimes,‬ ‭developers‬ ‭introduce‬ ‭complex‬ ‭patterns‬ ‭or‬
‭components.‬ ‭Often,‬ ‭composition‬ ‭helps‬ ‭to‬ ‭delegate‬‭responsibilities‬ ‭optimizations‬‭before‬‭they’re‬‭really‬‭needed,‬‭making‬‭the‬‭code‬‭harder‬
‭cleanly instead of stuffing everything into one class.‬ ‭to read and maintain.‬
‭2.‬ ‭Tight Coupling‬ ‭●‬ ‭How‬ ‭I‬ ‭avoid‬ ‭it:‬ ‭I‬ ‭start‬ ‭with‬ ‭a‬ ‭simple‬ ‭design‬ ‭that‬ ‭meets‬ ‭current‬
‭When‬ ‭classes‬ ‭are‬ ‭tightly‬ ‭coupled,‬ ‭changes‬ ‭in‬ ‭one‬ ‭ripple‬‭through‬ ‭requirements.‬ ‭I‬ ‭embrace‬ ‭refactoring‬ ‭as‬ ‭the‬ ‭system‬ ‭evolves‬ ‭and‬
‭many others, making the system fragile and hard to test.‬ ‭only‬ ‭apply‬ ‭design‬ ‭patterns‬ ‭when‬ ‭their‬ ‭benefits‬ ‭outweigh‬ ‭the‬
‭●‬ ‭How‬ ‭I‬ ‭avoid‬ ‭it:‬ ‭I‬ ‭program‬ ‭to‬ ‭interfaces‬ ‭or‬ ‭abstractions‬‭,‬ ‭not‬ ‭complexity‬‭they‬‭add.‬‭I‬‭also‬‭prioritize‬‭writing‬‭clean,‬‭readable‬‭code‬
‭concrete‬ ‭implementations.‬ ‭Using‬ ‭dependency‬ ‭injection‬ ‭helps‬ ‭over trying to optimize too early.‬
‭decouple‬‭dependencies‬‭and‬‭makes‬‭components‬‭easier‬‭to‬‭swap‬‭or‬
‭mock‬‭during‬‭testing.‬‭I‬‭also‬‭minimize‬‭direct‬‭references‬‭by‬‭relying‬‭on‬
‭well-defined contracts.‬
‭3.‬ ‭Low Cohesion‬
‭Sometimes‬ ‭a‬ ‭class‬ ‭ends‬ ‭up‬ ‭with‬ ‭unrelated‬ ‭methods‬ ‭and‬ ‭data,‬
‭which‬ ‭means‬ ‭it’s‬ ‭doing‬ ‭too‬ ‭many‬ ‭unrelated‬ ‭things,‬ ‭violating‬ ‭SRP.‬
‭This reduces readability and reusability.‬
‭●‬ ‭How‬ ‭I‬ ‭avoid‬ ‭it:‬ ‭I‬ ‭ensure‬ ‭that‬ ‭every‬ ‭class‬ ‭has‬ ‭a‬ ‭single,‬ ‭clear‬
‭purpose‬‭.‬ ‭If‬ ‭I‬ ‭find‬ ‭unrelated‬ ‭responsibilities‬ ‭creeping‬ ‭in,‬ ‭I‬ ‭refactor‬
‭by splitting the class into smaller, more cohesive units.‬
‭4.‬ ‭Over-inheritance / Deep Inheritance Hierarchies‬
‭Deep‬ ‭or‬ ‭complex‬ ‭inheritance‬ ‭trees‬ ‭make‬ ‭the‬ ‭design‬ ‭rigid‬ ‭and‬

You might also like