Lesson 6
Applying Object-Oriented Principles in Complex Tasks
Introduction and Lesson Goal

Today's mission involves using multiple Object-Oriented Programming (OOP) principles to tackle complex tasks. When principles like Encapsulation, Abstraction, Polymorphism, and Composition are blended, the resulting code becomes streamlined and easier to manage.

Our goal is to dissect two real-world examples, gaining insights into how these principles can seamlessly orchestrate solutions.

Real-life Example 1: Building an Online Library System

Let's design an online library system, as we aim to reinforce our understanding of Encapsulation and Polymorphism. Encapsulation will help us protect the attributes of books, members, and transactions, ensuring they are accessible in a controlled manner. Polymorphism will demonstrate its power by enabling a single interface to represent different underlying forms, such as digital and print versions of books.

Java
1// Base class for different types of library users 2class Member { 3 private String name; 4 5 public Member(String name) { 6 this.name = name; 7 } 8 9 public void checkOutBook(Book book) { 10 System.out.println(name + " checked out " + book.getBookType() + " book " + book.getTitle() + "."); 11 } 12} 13 14// Base class for different types of books 15abstract class Book { 16 private String title; 17 18 public Book(String title) { 19 this.title = title; 20 } 21 22 public String getTitle() { 23 return title; 24 } 25 26 public abstract String getBookType(); 27} 28 29// Inherits from Book, represents a digital book 30class DigitalBook extends Book { 31 public DigitalBook(String title) { 32 super(title); 33 } 34 35 @Override 36 public String getBookType() { 37 return "Digital"; 38 } 39} 40 41// Inherits from Book, represents a physical book 42class PhysicalBook extends Book { 43 public PhysicalBook(String title) { 44 super(title); 45 } 46 47 @Override 48 public String getBookType() { 49 return "Physical"; 50 } 51} 52 53// Library class that manages members and books 54class Library { 55 private ArrayList<Member> members; 56 private ArrayList<Book> books; 57 58 public Library() { 59 members = new ArrayList<Member>(); 60 books = new ArrayList<Book>(); 61 } 62 63 public void addMember(Member member) { 64 members.add(member); 65 } 66 67 public void addBook(Book book) { 68 books.add(book); 69 } 70} 71 72public class Main { 73 public static void main(String[] args) { 74 Library myLibrary = new Library(); 75 76 Member alice = new Member("Alice"); 77 Member bob = new Member("Bob"); 78 79 myLibrary.addMember(alice); 80 myLibrary.addMember(bob); 81 82 Book digitalBook = new DigitalBook("The Java Handbook"); 83 Book physicalBook = new PhysicalBook("Learning Java Design Patterns"); 84 85 myLibrary.addBook(digitalBook); 86 myLibrary.addBook(physicalBook); 87 88 alice.checkOutBook(digitalBook); // Prints: Alice checked out Digital book The Java Handbook. 89 bob.checkOutBook(physicalBook); // Prints: Bob checked out Physical book Learning Java Design Patterns. 90 } 91}

In this code snippet, Encapsulation is observed clearly through the class structures and the controlled access to their attributes. Polymorphism is vividly illustrated by how both DigitalBook and PhysicalBook classes inherit from the Book class but provide their own implementations of the getBookType method. This setup allows objects of DigitalBook and PhysicalBook to be used interchangeably when a book's type needs to be identified, demonstrating polymorphism's capability to work with objects of different classes through a common interface.

  • Encapsulation ensures that details about members and books are well-contained within their respective classes.
  • Polymorphism showcases flexibility by treating different book types uniformly, making the system more adaptive and scalable.
Real-life Example 2: Building a Shape Drawing Application

Next, we'll develop a shape-drawing application capable of drawing various shapes. For this, we'll employ the principles of Abstraction and Composition.

  • Abstraction simplifies the complexity associated with drawing different shapes.
  • Composition takes care of composite shapes.

Here's how we translate these principles into our shape-drawing application:

Java
1// Define the basic Shape class 2abstract class Shape { 3 // Abstract method that will be implemented in each subclass 4 public abstract void draw(); 5} 6 7// Define the Circle class 8class Circle extends Shape { 9 // Implement the draw method for circle 10 @Override 11 public void draw() { 12 System.out.println("Drawing a circle."); 13 } 14} 15 16// Define the Square class 17class Square extends Shape { 18 // Implement the draw method for square 19 @Override 20 public void draw() { 21 System.out.println("Drawing a square."); 22 } 23} 24 25// Define the ShapeComposite class 26class ShapeComposite extends Shape { 27 // Initialize with an empty list of shapes 28 private ArrayList<Shape> shapes; 29 30 public ShapeComposite() { 31 shapes = new ArrayList<Shape>(); 32 } 33 34 // Add a new shape to the composite 35 public void addShape(Shape shape) { 36 shapes.add(shape); 37 } 38 39 // Implement the draw method to draw each shape in composite 40 @Override 41 public void draw() { 42 for (Shape shape : shapes) { 43 shape.draw(); 44 } 45 } 46} 47 48public class Main { 49 public static void main(String[] args) { 50 Circle circle = new Circle(); 51 Square square = new Square(); 52 53 // Drawing individual shapes 54 circle.draw(); // Output: Drawing a circle. 55 square.draw(); // Output: Drawing a square. 56 57 // Create a ShapeComposite instance for composite shapes 58 ShapeComposite compositeShape = new ShapeComposite(); 59 60 // Add individual shapes to the composite 61 compositeShape.addShape(circle); 62 compositeShape.addShape(square); 63 64 // Drawing the composite shape 65 compositeShape.draw(); 66 // Output: 67 // Drawing a circle. 68 // Drawing a square. 69 } 70}
  • Abstraction: In this example, the Shape class is abstract. We don’t care about the specific details of how each shape is drawn here, but we know that each shape must have a draw() method. The abstract class helps us define this rule for all shapes.
  • Composition: The ShapeComposite class demonstrates composition by combining multiple shapes. It can hold and draw multiple shapes together. Composition is used when one object (a composite shape) contains other objects (individual shapes).
Lesson Summary

Well done! You combined multiple OOP principles to respond to complex tasks. By dissecting real-world examples, we understood how these principles found their applications. Now, it's time to put this knowledge to work. Practice fortifies concepts, transforming knowledge into expertise. So, let's get coding!

Enjoy this lesson? Now it's time to practice with Cosmo!
Practice is how you turn knowledge into actual skills.