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?

Because you are assigning "pl" to all the elements and your "FamousPainting" object is "f". What good does it do to have a FamousPainting object and then not use it? Instead of creating that at the beginning, create it at the same place as you are now creating your Painting objects. Just create one or the other, then set the value and assign it to the array, regardless of which it is.

Because you are assigning "pl" to all the elements and your "FamousPainting" object is "f". What good does it do to have a FamousPainting object and then not use it? Instead of creating that at the beginning, create it at the same place as you are now creating your Painting objects. Just create one or the other, then set the value and assign it to the array, regardless of which it is.

Could you please show me about the correction on my code ?I 'm not so understand about your suggestion.

Painting pl = new Painting(/*whatever*/);
pl.setValue(/*whatever*/);
if (/*famous painting check*/) {
  pl = new FamousPainting(/*whatever*/);
  pl.setValue(/*whatever*/);
}
p[i] = pl;

It's not that hard to figure out.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.