Write a Java program to demonstrate method overriding with a subclass that overrides a method from an abstract class and implements an interface
The code defines an interface Drawable with a method draw, an abstract class Shape that implements the Drawable interface and provides a default implementation for the draw method, and a concrete subclass Circle that extends the Shape class and overrides the draw method. Here's an explanation of the code:
The use of the Drawable interface and the Shape abstract class allows for method overriding and polymorphism, and your code effectively demonstrates this. When you run the code, it will produce the output "Drawing a circle" as expected.
interface Drawable { void draw(); } abstract class Shape implements Drawable { @Override public void draw() { System.out.println("Drawing a shape"); } } class Circle extends Shape { @Override public void draw() { System.out.println("Drawing a circle"); } } public class Main { public static void main(String[] args) { Circle circle = new Circle(); circle.draw(); } }
Drawing a circle
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions