import java.util.*;
class Painting
{
private String title;
private String name;
int value;
public Painting(){}
public Painting(String t,String n)
{
title = t;
name = n;
}
public void setValue(int v)
{
value = v;
}
public void setPrice()
{
value = 400;
}
public String getTitle()
{
return title;
}
public String getName()
{
return name;
}
public int getValue()
{
return value;
}
}
class FamousPainting extends Painting
{
public void setValue(int v)
{
value = v;
}
}
public class TestPainting
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
Painting[] p = new Painting[4];
Painting f = new FamousPainting();
for(int i=0;i<4;i++)
{
System.out.print("Artwork's name: ");
String t = in.nextLine();
System.out.print("Artist's name: ");
String n = in.nextLine();
if(n == "Picasso")
{f.setValue(2500);}
Painting p1 = new Painting(t,n);
p1.setValue(400);
p[i] = p1;
}
for(int i=0;i<4;i++)
{
System.out.println(p[i].getTitle());
System.out.println(p[i].getName());
System.out.println(p[i].getValue()+"\n");
}
}
}
These coding is about an assignment ,which request to store 4 Paintings object and if the artist name is somewhat like Picasso the class FamousPainting would override the value with 25000 instead of 400.But i thought that i might have some mistake on the override method since both the setValue() properties is same in both superclass and subclass.Furthermore the output of the program is all with the same value : 400,even i had input one of the object with Picasso,why?