This question already has an answer here:
I have three classes: an abstract class inherited by a concrete class, which is tested by a tester class.
The getter methods (getWidth, getHeight) of the abstract inherited class are protected. One of the concrete classes' analoguous getter methods (getBase) calls getWidth. The other getter method (getHeight) calls super.getHeight. The first one works, and the second leads to a compiler error.
Why? I have imported the package that the abstract inherited class is in. I would've thought that the protected status of the fields wouldn't stop them from being accessible in the tester class, because of that import. How do I properly test that method?
Also, why does calling super.[protected method here] work whereas calling the protected method on its own does not?
Thanks everyone for their time.
Inherited Abstract Class
package csc143.junit;
/**
* Simple shape class, an abstraction of a shape, the base class.
*/
public abstract class CSC143Shape {
// fields to maintain size information
private int width;
private int height;
/**
* Create a shape.
* @param w The width of the shape
* @param h The height of the shape
*/
public CSC143Shape(int w, int h) {
if(w<0 || h<0)
throw new IllegalArgumentException("Dimensions cannot be negative");
if(w>1000 || h>1000)
throw new IllegalArgumentException("Dimensions cannot be greater than 1,000");
width = w;
height = h;
}
/**
* Get the width of the shape object.
* @return the width of the shape
*/
protected int getWidth() {
return width;
}
/**
* Get the height of the shape object.
* @return the height of the shape
*/
protected int getHeight() {
return height;
}
/**
* Returns the area of the shape object.
* @return the area of the shape object
*/
public abstract double getArea();
}
Concrete Class
package csc143.junit;
/**
* Simple shape class, an abstraction of a triangle.
*/
public class CSC143Triangle extends CSC143Shape {
/**
* Create a triangle.
* @param b The base of the triangle
* @param h The height of the triangle
*/
public CSC143Triangle(int b, int h) {
super(b, h);
}
/**
* Get the base of the triangle object.
* @return the base of the triangle
*/
public int getBase() {
return getWidth();
}
/**
* Get the height of the triangle object.
* @return the height of the triangle
*/
public int getHeight() {
return super.getHeight();
}
/**
* Returns the area of the triangle object.
* @return the area of the triangle object
*/
public double getArea() {
return getBase() * getHeight() / 2;
}
}
Tester Class
package csc143.test.junit;
import csc143.junit.*;
import org.junit.*;
import static org.junit.Assert.assertEquals;
import java.lang.Exception.*;
public class TestCSC143Triangle {
CSC143Triangle t1;
CSC143Triangle t2;
CSC143Triangle t3;
@Before
public void initTests() {
CSC143Triangle t1 = new CSC143Triangle(0, 1000);
CSC143Triangle t2 = new CSC143Triangle(1000, 0);
CSC143Triangle t3 = new CSC143Triangle(1000, 1000);
}
@Test (expected = IllegalArgumentException.class)
public void testBounds() {
CSC143Triangle f1 = new CSC143Triangle(0, -1);
CSC143Triangle f2 = new CSC143Triangle(1001, 500);
CSC143Triangle f3 = new CSC143Triangle(500, 1001);
CSC143Triangle f4 = new CSC143Triangle(1001, -1);
}
@Test
public void testGets () {
assertEquals("width low boundary misgot", 0, t1.getWidth());
assertEquals("height high boundary misgot", 1000, t1.getHeight());
assertEquals("width high boundary misgot", 1000, t2.getWidth());
assertEquals("height low boundary misgot", 0, t2.getHeight());
}
@Test
public void testAreas() {
assertEquals("area low boundary miscalculated", 0, t1.getArea());
assertEquals("area high boundary miscalculated", (1000*1000/2), t3.getArea());
}
}
Aucun commentaire:
Enregistrer un commentaire