Ly LLD Class 2
Ly LLD Class 2
Programming paradigms
Introduction to Oops 1
Description: Declarative programming focuses on describing what the
program should accomplish, rather than explicitly stating how it should be
done. It emphasizes expressing computations without specifying the
control flow.
Introduction to Oops 2
6. Event-Driven Programming Paradigm:
2. Providing public setter and getter methods to access and update the value of
private variables
Encapsulation Benefits:
Introduction to Oops 3
Let's model a Bank Account class:
Introduction to Oops 4
public void deposit(double amount) {
// perform validation before applying
balance += amount;
}
The public getter/setter methods are used to access and update these
attributes in a controlled fashion
Methods like deposit() and withdraw() encapsulate the logic to manipulate the
balance attribute
This encapsulation provides abstraction barrier between object state and behavior
and how other parts of the code use it. For example, we can change the data type
of balance to BigDecimal without any code change required in other classes.
Abstraction
Abstraction refers to exposing only essential details and hiding complexity from
users. It focuses on the outside view of an object.
In Java, abstraction can be achieved using:
1. Abstract classes
Introduction to Oops 5
2. Interfaces
Abstract Class:
Example:
//abstract method
public abstract double getArea();
//concrete method
public void setColor(String color) {
this.color = color;
}
}
Introduction to Oops 6
public double getArea() {
return length * width;
}
}
Introduction to Oops 7
public void applyBrakes(int decrement){
currentSpeed -= decrement;
}
}
In this example:
Bicycle and Car classes implement these methods to provide custom vehicle
behavior
Key benefits:
Introduction to Oops 8
So with interfaces we define a contract for capabilities that implementing classes
should provide. This helps hide unnecessary details from outside code.
Key points:
Inheritance
Inheritance allows a new class to be based on an existing class, inheriting its
attributes and behaviors. The existing base class is called superclass or parent
class while the new class is called subclass or child class.
The subclass inherits the public and protected members of the superclass. It can
also override inherited methods and add new attributes and behaviors. This
enables code reusability.
Here is an example of inheritance from the open source Spring Framework
Introduction to Oops 9
public class UserDao extends DAOSupport {
2. Extensibility:
3. Maintainability:
Introduction to Oops 10
polymorphism
Polymorphism means "many forms". It allows objects with different underlying
forms to be treated uniformly. In Java, polymorphism is achieved via:
1. Method overloading
2. Method overriding
Method overloading refers to methods with same name but different parameters.
Based on parameters passed, the appropriate method is invoked.
Method overriding refers to sub-classes providing specific implementation for
methods declared in parent class. The call is bound to implementation in the
runtime based on type of object.
Introduction to Oops 11
public Response execute(Request request) {
//custom caching logic
return response;
}
}
//client code
HttpClient client = new DefaultHttpClient();
//OR
HttpClient cachingClient = new CachingHttpClient(new DefaultH
ttpClient());
Another example
Here is an example demonstrating both method overloading and overriding
polymorphism
Introduction to Oops 12
@Override
public String getText() {
//custom logic before delegating to overloaded method
return getText(this.currToken);
}
Method Overloading:
Method Overriding:
This enables:
Introduction to Oops 13