Search⌘ K
AI Features

Solution: Secure Banking Documents

Explore how to design secure banking documents by applying inheritance and polymorphism. Understand using abstract classes to define templates, protect sensitive data with access control, and enforce security protocols through final methods. Learn how subclasses implement specific document behaviors by overriding abstract methods.

We'll cover the following...
Java 25
abstract class Document {
protected String owner;
// Abstract classes have constructors to initialize their state
public Document(String owner) {
this.owner = owner;
}
// final ensures this logic cannot be tampered with
public final void printOwner() {
System.out.println("Document Owner: " + owner);
}
public abstract void printContent();
}
class PDFDocument extends Document {
public PDFDocument(String owner) {
// Must pass the owner to the parent constructor
super(owner);
}
@Override
public void printContent() {
System.out.println("Decrypting PDF binary data...");
}
}
class TextDocument extends Document {
public TextDocument(String owner) {
super(owner);
}
@Override
public void printContent() {
System.out.println("Reading text data...");
}
}
public class Main {
public static void main(String[] args) {
Document pdf = new PDFDocument("Alice");
Document txt = new TextDocument("Bob");
pdf.printOwner();
pdf.printContent();
System.out.println("---");
txt.printOwner();
txt.printContent();
}
}
...