StudyLover
  • Home
  • Study Zone
  • Features
    • Workout Interval Timer
    • Typing Tutor
  • Profiles
  • Contact us
  • Sign in
StudyLover The Interface Agreement
Download
  1. Java
  2. Programs
The Abstract & Final Contract : The String Performance Lab
Programs
Initiative: To bypass single inheritance limits by using multiple interfaces for role-based capabilities.
interface Camera { // Interface defining a camera capability/role
    void takePhoto(); // Abstract method to capture an image
} // End of Camera interface

interface Phone { // Interface defining a telecommunication capability/role
    void makeCall(String num); // Abstract method to place a call to a number
} // End of Phone interface

class SmartDevice implements Camera, Phone { // Class fulfilling multiple interface roles
    private String model; // Private attribute for the device name/model
    
    public SmartDevice(String m) { // Constructor for the smart device
        this.model = m; // Initialize the model string
    } // End of constructor
    
    @Override // Implementation of the Camera role
    public void takePhoto() { // logic to snap a picture
        System.out.println(model + " is capturing a high-res photo..."); // Print capture status
    } // End of takePhoto method
    
    @Override // Implementation of the Phone role
    public void makeCall(String n) { // logic to dial a number
        System.out.println(model + " is dialing " + n + "..."); // Print dialing status
    } // End of makeCall method
    
    public void browse() { // Native method specific to the SmartDevice class
        System.out.println(model + " is opening the web browser."); // Print browsing status
    } // End of browse method
} // End of SmartDevice class

public class InterfaceAgreement { // Main class to test multi-interface implementation
    public static void main(String[] args) { // Program entry point
        System.out.println("--- Multi-Interface Role Lab ---"); // Print lab header
        
        SmartDevice myPhone = new SmartDevice("Samsung Galaxy"); // Instantiate the multi-role device
        
        myPhone.makeCall("98765-43210"); // Call method from Phone interface
        myPhone.takePhoto(); // Call method from Camera interface
        myPhone.browse(); // Call native class-specific method
        
        System.out.println("\n--- Polymorphism with Interfaces ---"); // Section header
        
        Camera camRole = myPhone; // Treat the phone purely as a camera (Upcasting)
        camRole.takePhoto(); // Only camera methods are accessible via this reference
        
        System.out.println("Role Lab Finished."); // Final log for the interface demo
    } // End of main method
} // End of InterfaceAgreement class
The Abstract & Final Contract The String Performance Lab
Our Products & Services
  • Home
Connect with us
  • Contact us
  • info.studylover@gmail.com

StudyLover - About us

The Best knowledge for Best people.

Copyright © StudyLover
Powered by Odoo - Create a free website