Create a Java program to demonstrate method overriding with a subclass that overrides a method from an interface and implements the same method with different behavior
The code defines an interface Printable with a method print, and two classes, Printer and Scanner, both of which implement the Printable interface by providing their respective implementations for the print method. In the Main class, you create instances of both Printer and Scanner, assign them to Printable references, and then call the print method on each of them. Here's an explanation of the code:
interface Printable { void print(); } class Printer implements Printable { @Override public void print() { System.out.println("Printing..."); } } class Scanner implements Printable { @Override public void print() { System.out.println("Scanning..."); } } public class Main { public static void main(String[] args) { Printable printer = new Printer(); Printable scanner = new Scanner(); printer.print(); scanner.print(); } }
Printing... Scanning...
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions