0% found this document useful (0 votes)
37 views1 page

Polymorphism Java

The document contains a Java program demonstrating polymorphism through a base class 'Shape' and its derived classes 'Circle', 'Triangle', and 'Square'. Each shape class overrides the 'draw' and 'erase' methods to provide specific implementations. The main method creates an array of shapes and iterates through it to call the draw and erase methods for each shape.

Uploaded by

Nagaraj Naik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views1 page

Polymorphism Java

The document contains a Java program demonstrating polymorphism through a base class 'Shape' and its derived classes 'Circle', 'Triangle', and 'Square'. Each shape class overrides the 'draw' and 'erase' methods to provide specific implementations. The main method creates an array of shapes and iterates through it to call the draw and erase methods for each shape.

Uploaded by

Nagaraj Naik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

File: /home/mybfan1011/Polymorphism.

java Page 1 of 1

import java.util.Scanner;
class Shape{
public void draw(){
System.out.println("Drawiing a generic shape");
}

public void erase(){


System.out.println("Erasing a generic shape");
}
}

class Circle extends Shape{


public void draw(){
System.out.println("Drawing a circle");
}

public void erase(){


System.out.println("Erashing a circle");
}
}

class Triangle extends Shape{


public void draw(){
System.out.println("Drawing a triangle");
}
public void erase(){
System.out.println("Erashing a triangle");
}
}
class Square extends Shape{
public void draw(){
System.out.println("Drawing a square");
}
public void erase(){
System.out.println("Erashing a square");
}
}

public class Polymorphism{


public static void main(String[]args){
Shape[] shapes=new Shape[3];
shapes[0]= new Circle();
shapes[1]= new Triangle();
shapes[2]=new Square();

for(Shape shape:shapes){
shape.draw();
shape.erase();
System.out.println();
}
}
}

You might also like