Search⌘ K
AI Features

Solution: Secure Banking Documents

Explore how to secure banking documents in Java by defining an abstract Document class and using inheritance to create concrete document types. Learn to enforce security with final methods and implement flexible content handling through polymorphism.

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();
}
}
...