6

I have these 4 java clases: 1

public class Rect {
    double width;
    double height;
    String color;

    public Rect( ) {
        width=0;
        height=0;
        color="transparent";      
    }

    public Rect( double w,double h) {
        width=w;
        height=h;
        color="transparent";
    }

    double area()
    {
        return  width*height;
    } 
}

2

public class PRect extends Rect{
    double depth;

    public PRect(double w, double h ,double d) {
        width=w;
        height=h;
        depth=d;
    }

    double area()
    {
        return  width*height*depth;
    }     
}

3

public class CRect extends Rect{ 
    String color;

    public CRect(double w, double h ,String c) {
        width=w;
        height=h;
        color=c;
    }

    double area()
    {
        return  width*height;
    }     
}

4

public class test {

    public test() { }

    public static void main(String[] args) {  
        Rect r1=new Rect(2,3);
        System.out.println("area of r1="+r1.area());

        PRect pr1=new PRect(2,3,4);
        System.out.println("area of pr1="+pr1.area());


        CRect cr1=new CRect(2,3,"RED");
        System.out.println("area of cr1="+cr1.area()+"  color = "+cr1.color);


        System.out.println("\n POLY_MORPHISM ");
        Rect r2=new Rect(1,2);
        System.out.println("area of r2="+r2.area());

        Rect pr2=new PRect(1,2,4);
        System.out.println("area of pr2="+pr2.area());


        Rect cr2=new CRect(1,2,"Blue");
        System.out.println("area of cr2="+cr2.area()+"  color = "+cr2.color); 

    }
}

I got the output:

area of r1=6.0
area of pr1=24.0
area of cr1=6.0  color = RED
POLY_MORPHISM 
area of r2=2.0
area of pr2=8.0
area of cr2=2.0  color = transparent***

why consider cr2 as Rect(super class) and with "transparent" color not as CRect (sub class) with "Blue" color ?

Rob
  • 415,655
  • 72
  • 787
  • 1,044
engy
  • 141
  • 2
  • 4

3 Answers3

13

This is one of the problems of using visible fields - you end up using them...

You've got a color field in both Rect and CRect. Fields are not polymorphic, so when you use cr2.color, that uses the field declared in Rect, which is always set to "transparent".

Your CRect class should not have its own color field - it should supply the colour to the superclass constructor. It makes no sense for a single rectangle to have two different color fields - it could have borderColor and fillColor, of course - but just color is too ambiguous...

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • or instance variables are not overridden but they are visible in your subclass's – PermGenError Dec 06 '12 at 16:35
  • @GanGnaMStYleOverFlowErroR: I'm not sure I understand your comment, but the idea of overriding a field is simply not applicable, as there's no polymorphism over fields... – Jon Skeet Dec 06 '12 at 16:36
  • yes, `polymorphism doesn't apply to fields(instance variables)`, doesn't that mean, they can't be overridden ?? – PermGenError Dec 06 '12 at 16:39
  • @GanGnaMStYleOverFlowErroR: Yes, they can't be overridden, because polymorphism simply doesn't apply. It doesn't apply to static variables either, which are also fields. – Jon Skeet Dec 06 '12 at 16:41
  • thats what my first comment implied, :P – PermGenError Dec 06 '12 at 16:42
  • @GanGnaMStYleOverFlowErroR: Not really. Your first comment basically didn't make sense - hence *my* first comment. I was trying to clarify what you meant. – Jon Skeet Dec 06 '12 at 16:53
0

You should include an explicit super() call in your subclasses' constructors:

public CRect(double w, double h ,String c) {
    super(w, h);
    width=w;
    height=h;
    color=c;
}
sampson-chen
  • 45,805
  • 12
  • 84
  • 81
0

cr2.area() will call CRect.area() but cr2.color will use the field Rect.color. You should use the function style getArea() and have CRect.getColor() { return color; } as well as Rect.getColor() { return color; }

opi
  • 244
  • 2
  • 9