0% found this document useful (0 votes)
8 views

Implementing ADT Operations-2

Uploaded by

Shyamali Sarkar
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)
8 views

Implementing ADT Operations-2

Uploaded by

Shyamali Sarkar
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/ 5

java

public interface Animal {


void makeSound();
}

public class Dog implements Animal {


public void makeSound() {
System.out.println("Woof");
}
}

public class Cat implements Animal {


public void makeSound() {
System.out.println("Meow");
}
}

// 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

Inheritance is a mechanism in Object-Oriented Programming (OOP) where a class (subclass/child class)


derives properties and behaviors (methods) from another class (superclass/parent class). It enables
code reuse, hierarchical classification, and supports polymorphism.

Key Concepts of Inheritance

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;

public Animal(String name) {


this.name = name;
}

public void eat() {


System.out.println(name + " is eating.");
}
}

// Subclass
public class Dog extends Animal {
public Dog(String name) {
super(name);
}

@Override
public void eat() {
System.out.println(name + " is eating dog food.");
}

public void bark() {


System.out.println(name + " says Woof!");
}
}

// Usage
Animal genericAnimal = new Animal("Generic Animal");
genericAnimal.eat();

Dog myDog = new Dog("Buddy");

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.

Classification of Design Patterns

Design patterns are generally classified into three main categories:

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

The Iterator Pattern is a behavioral design pattern that provides a way to access elements of a
collection sequentially without exposing its underlying representation.

Key Components of the Iterator Pattern

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;

public ArrayIterator(T[] items) {


this.items = items;
}

@Override
public boolean hasNext() {
return index < items.length;
}

@Override
public T next() {
return items[index++];
}
}

// Aggregate
public class Collection<T> {
private T[] items;

public Collection(T[] items) {


this.items = items;
}

public Iterator<T> iterator() {


return new ArrayIterator<>(items);
}
}

// Client
public class Main {
public static void main(String[] args) {
String[] data = {"Apple", "Banana", "Cherry"};
Collection<String> fruits = new Collection<>(data);

Iterator<String> iterator = fruits.iterator();


while (iterator.hasNext()) {
System.out.println(iterator.next());
}
}
}

Advantages of the Iterator Pattern

1. Decouples the collection and its traversal logic.


2. Simplifies iteration logic for clients.
3. Supports different traversal strategies (e.g., forward, backward).

Summary

Inheritance enables code reuse and specialization.


Design Patterns provide standard solutions to recurring problems, classified into creational,
structural, and behavioral categories.
The Iterator Pattern abstracts the traversal of collections, promoting clean and flexible design.

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

Separation of concerns improves modularity.


Easier to maintain and test individual components.
Promotes reusability of the Model and View.

Example:

java

// Model
public class CounterModel {
private int count = 0;
private List<CounterObserver> observers = new ArrayList<>();

public int getCount() {


return count;
}

public void increment() {


count++;
notifyObservers();
}

public void addObserver(CounterObserver observer) {


observers.add(observer);
}

private void notifyObservers() {


for (CounterObserver observer : observers) {
observer.update(count);
}
}
}

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

You might also like