-1

Here is code that is part of my program. I'm concerned of the constructor override warning with addTotalPerimeter. All other parts of the program that are not important have been left out. What can I do to fix the constructor override?

public Triangle(int side_a,int side_b,int side_c){
    if(isValid() == true)
    {
        this.side_a = side_a;
        this.side_b = side_b;
        this.side_c = side_c;
        accumulator = addTotalPerim();
    }
    else
    {  
        this.side_a = 1;
        this.side_b = 1;
        this.side_c = 1;
    }
    counter++;     
}
private boolean isValid(){
    int x = 6;
    int y = 5;

    if(side_a > 0 && side_b > 0 && side_c > 0){
        y = 1;
    }

    if((side_a + side_b > side_c) && (side_a + side_c > side_b) && (side_b + side_c > side_a)){

        x = 1;
    }

    if(x == y)
        return true;
    else
        return false;
}
public int addTotalPerim(){
    accumulator += calcPerim();
    return accumulator += calcPerim();
}
Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • What do you mean by "constructor override"? And where is there a constructor anywhere in your posted code? And if you're seeing an error message and want help, it would make sense to show us the complete message. – Hovercraft Full Of Eels Nov 03 '14 at 21:03
  • 1
    Is it warning you that you're calling an overrideable method inside of the constructor? – Hovercraft Full Of Eels Nov 03 '14 at 21:05
  • I am assuming that he is talking about `public Triangle`. – Buhake Sindi Nov 03 '14 at 21:05
  • 2
    @BuhakeSindi: I'm assuming nothing til he tells us. – Hovercraft Full Of Eels Nov 03 '14 at 21:06
  • Yep, it's about calling an overrideable method inside of a constructor, a potentially dangerous thing to do. – Hovercraft Full Of Eels Nov 03 '14 at 21:09
  • Sorry I know my code looks horrendous. I Got turned away by all the people saying different things. I rewrote the program. I had it cleanly written but then I got stressed and screwed the whole thing, fixed as much as I could today. And yes the constructor is the problem, yes this is my code, and Its an assignment I wrote myself. Assuming makes an ass out of you and me. – MrTumble Nov 03 '14 at 21:47

1 Answers1

0

What you describe isn't overriding. If you don't specify a default constructor, the compiler will create a default constructor. If it's a subclass, it will call the default parent constructor(super()), it will also initialize all instance variables to a default value determined by the type's default value(0 for numeric types, false for booleans, or null for objects).

Overriding happens when a subclass has the same name, number/type of parameters, and the same return type as an instance method of the superclass. In this case, the subclass will override the superclass's method.

Lawrence Aiello
  • 4,560
  • 5
  • 21
  • 35
  • 1
    I don't think that this is his problem, but it's hard to tell given that it's a very poorly written and incomplete question. Note that I'm not your down-voter. – Hovercraft Full Of Eels Nov 03 '14 at 21:05