Implementing ADT Operations-2
Implementing ADT Operations-2
// Usage
Animal myPet = new Dog(); // Reference of type Animal
myPet.makeSound(); // Output: Woof (runtime decision)
myPet = new Cat();
myPet.makeSound(); // Output: Meow
Summary
Encapsulation ensures that data and methods are bundled together, promoting modularity and
safety.
Object Identity gives each object a unique identity, even when states are similar.
Polymorphism allows flexible and dynamic behavior, enabling a single interface to manage
diverse data types and behaviors.
These features make OOP a powerful paradigm for building modular, reusable, and maintainable
software.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/17
Inheritance in Object-Oriented Design
1. Code Reuse:
Common functionality is implemented in a superclass and inherited by subclasses, reducing
duplication.
2. Hierarchical Relationships:
Establishes "is-a" relationships, where the child class is a specialized version of the parent
class.
3. Method Overriding:
Subclasses can override methods of the superclass to provide specific behavior.
4. Access Control:
Members with `protected` or `public` access modifiers can be inherited, while `private`
members cannot.
Example:
java
// Superclass
public class Animal {
protected String name;
// Subclass
public class Dog extends Animal {
public Dog(String name) {
super(name);
}
@Override
public void eat() {
System.out.println(name + " is eating dog food.");
}
// Usage
Animal genericAnimal = new Animal("Generic Animal");
genericAnimal.eat();
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/17
myDog.eat(); // Overridden method
myDog.bark();
Design Patterns
Design Patterns are reusable solutions to common software design problems. They are templates, not
concrete implementations, that guide developers in structuring their code for maintainability and
scalability.
1. Creational Patterns:
Focus on object creation mechanisms to promote flexibility and reuse.
Examples: Singleton, Factory Method, Builder, Prototype.
2. Structural Patterns:
Deal with object composition and relationships.
Examples: Adapter, Composite, Proxy, Decorator.
3. Behavioral Patterns:
Focus on communication and responsibility between objects.
Examples: Observer, Strategy, Command, Iterator.
The Iterator Pattern is a behavioral design pattern that provides a way to access elements of a
collection sequentially without exposing its underlying representation.
1. Iterator Interface:
Defines methods like `hasNext()` and `next()` to traverse a collection.
2. Concrete Iterator:
Implements the iterator interface and manages the traversal.
3. Aggregate (Collection):
A collection that provides an iterator to iterate over its elements.
4. Client:
Uses the iterator to access elements of the collection.
Example:
java
// Iterator Interface
public interface Iterator<T> {
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/17
boolean hasNext();
T next();
}
// Concrete Iterator
public class ArrayIterator<T> implements Iterator<T> {
private T[] items;
private int index = 0;
@Override
public boolean hasNext() {
return index < items.length;
}
@Override
public T next() {
return items[index++];
}
}
// Aggregate
public class Collection<T> {
private T[] items;
// Client
public class Main {
public static void main(String[] args) {
String[] data = {"Apple", "Banana", "Cherry"};
Collection<String> fruits = new Collection<>(data);
Summary
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 9/17
Model-View-Controller (MVC) Pattern
The Model-View-Controller (MVC) pattern is a widely used architectural pattern that separates an
application into three interconnected components:
1. Model:
Represents the data and business logic of the application.
Notifies the View of changes to data.
Independent of the user interface (UI).
2. View:
Displays the data to the user.
Observes the Model for updates and reflects changes in the UI.
Does not directly modify the Model.
3. Controller:
Acts as the intermediary between the Model and View.
Handles user input and updates the Model accordingly.
Determines the flow of the application.
Advantages of MVC
Example:
java
// Model
public class CounterModel {
private int count = 0;
private List<CounterObserver> observers = new ArrayList<>();
interface CounterObserver {
void update(int newCount);
}
// View
public class CounterView implements CounterObserver {
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 10/17