Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 18 of 18

Thread: Having trouble dealing with objects using abstract methods/classes and polymorphism

  1. #1
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Having trouble dealing with objects using abstract methods/classes and polymorphism

    We are supposed to create several classes including rectangle,square, and right triangle. Each class must have a uniform way of calling methods, hence abstract methods. What I don't understand is that my instructor wants us to take the rectangle class, and derive a square and right triangle class from it which makes sense in a way, but when I create objects I can't understand how to setup the constructors so that when I create a right triangle object, I don't have to also enter a length and width of a rectangle. Here is my code.

    import java.util.Scanner;
     
    public class ProgramDriver{
     
    	public static void main(String[] args){
     
    		Scanner sc = new Scanner(System.in);
     
    		Shapes[] container = new Shapes[13];
     
     
     
    		Rectangle rec1 = new Rectangle(10, 12);
    		Rectangle rec2 = new Rectangle(6, 8);
     
    		container[0] = rec1;  //putting rectangle objects in container
    		container[1] = rec2;
     
    		Square sq1 = new Square(0,0,6);
    		Square sq2 = new Square(0,0,8);
     
    		container[2] = sq1;
    		container[3] = sq2; //putting square objects in container
     
    		RightTriangle rt1 = new RightTriangle(0,0,5,10);
    		RightTriangle rt2 = new RightTriangle(0,0,6,12);
     
    		container[4] = rt1;
    		container[5] = rt2; //putting right triangle objects in container
     
    		Circle c1 = new Circle(8);
    		Circle c2 = new Circle(10);
     
    		container[6] = c1;
    		container[7] = c2; //putting circle objects in container
     
    		Sphere s1 = new Sphere(15);
    		Sphere s2 = new Sphere(10);
     
    		container[8] = s1;
    		container[9] = s2; //putting sphere objects in container
     
    		Prism p1 = new Prism(5,3,2);
    		Prism p2 = new Prism(6,3,4);
     
    		container[10] = p1; //putting rectangular prism objects in container
    		container[11] = p2;
     
     
     
    		System.out.println("Rectangle 1" + "\n\nArea: " + container[0].getArea() + "\nPerimeter: " + container[0].getPerimeter() + "\nVolume: " + container[0].getVolume());
    		System.out.println("\nRectangle 2" + "\n\nArea: " + container[1].getArea() + "\nPerimeter: " + container[1].getPerimeter() + "\nVolume: " + container[1].getVolume());
    		System.out.println("\nSquare 1" + "\n\nArea: " + container[2].getArea() + "\nPerimeter: " + container[2].getPerimeter() + "\nVolume: " + container[2].getVolume());
    		System.out.println("\nSquare 2" + "\n\nArea: " + container[3].getArea() + "\nPerimeter: " + container[3].getPerimeter() + "\nVolume: " + container[3].getVolume());
    		System.out.println("\nRight Triangle 1" + "\n\nArea: " + container[4].getArea() + "\nPerimeter: " + container[4].getPerimeter() + "\nVolume: " + container[4].getVolume());
    		System.out.println("\nRight Triangle 2" + "\n\nArea: " + container[5].getArea() + "\nPerimeter: " + container[5].getPerimeter() + "\nVolume: " + container[5].getVolume());
    		System.out.println("\nCircle 1" + "\n\nArea: " + container[6].getArea() + "\nPerimeter: " + container[6].getPerimeter() + "\nVolume: " + container[6].getVolume());
    		System.out.println("\nCircle 2" + "\n\nArea: " + container[7].getArea() + "\nPerimeter: " + container[7].getPerimeter() + "\nVolume: " + container[7].getVolume());
    		System.out.println("\nSphere 1" + "\n\nArea: " + container[8].getArea() + "\nPerimeter: " + container[8].getPerimeter() + "\nVolume: " + container[8].getVolume());
    		System.out.println("\nSphere 2" + "\n\nArea: " + container[9].getArea() + "\nPerimeter: " + container[9].getPerimeter() + "\nVolume: " + container[9].getVolume());
    		System.out.println("\nRectangular Prism 1" + "\n\nArea: " + container[10].getArea() + "\nPerimeter: " + container[10].getPerimeter() + "\nVolume: " + container[10].getVolume());
    		System.out.println("\nRectangular Prism 2" + "\n\nArea: " + container[11].getArea() + "\nPerimeter: " + container[11].getPerimeter() + "\nVolume: " + container[11].getVolume());
    		}
     
     
    }
    public abstract class Shapes{
     
    	public Shapes(){
     
     
    	}
     
    	public abstract double getArea();
    	public abstract double getPerimeter();
    	public abstract double getVolume();
    }

    public class Rectangle extends Shapes{
     
    	private double length;
    	private double width;
     
    	public Rectangle(double lr, double wr){
     
    		length = lr;
    		width = wr;
    	}
     
    	public double getArea(){
     
    		double area = length * width;
    		return area;
    	}
     
    	public double getPerimeter(){
     
    		double perimeter = (width*2)+(length*2);
    		return perimeter;
    	}
     
    	public double getVolume(){
     
    	  //2-d shapes cannot have volume
    	return 0;
    	}
     
     
     
    }

     
    public class Square extends Rectangle{
     
    	private double side;
     
     
    	public Square(double lr, double wr, double s){
     
    		super(lr,wr);
    		side = s;
    	}
     
    	public double getArea(){
     
    		double area = side * side ;
    		return area;
    	}
     
    	public double getPerimeter(){
     
    		double perimeter = side * 4;
    		return perimeter;
    	}
     
    	public double getVolume(){
     
    		 //2-d shapes cannot have volume
    		return 0;
    	}
     
     
     
    }
    public class RightTriangle extends Rectangle{
     
    	private double base; //this is an equilateral triangle
    	private double height;
     
    	public RightTriangle(double lr, double wr, double b, double h){
     
    		super(lr,wr);
    		base = b;
    		height = h;
    	}
     
    	public double getArea(){
    		double area = .5 * (base * height);
    		return area;
     
    	}
     
    	public double getPerimeter(){
    		double perimeter = base * 3;
    		return perimeter;
     
    	}
     
    	public double getVolume(){
     
    		return 0;//2-d shapes cannot have volume
    	}
    }


    If you look at the programdriver class, when I create several of the objects, I don't understand how to setup the right triangle/square class so that I don't need to enter data for a non-existant rectangle. Any tips?


  2. #2
    Junior Member awinston's Avatar
    Join Date
    Jul 2012
    Location
    United States
    Posts
    10
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble dealing with objects using abstract methods/classes and polymorphi

    For Square, you can just have one parameter for its constructor because it's length and width are the same.

    However, I don't think it makes sense to have the right triangle extend Rectangle. A square actually is a rectangle, but a right triangle is not. Therefore, right triangle shouldn't be a subclass of Rectangle.

    However, if Right Triangle needs to extend Rectangle, then it's okay to just have base and height as its parameters for the constructor. There's no point in calling Rectangle's constructor within Right Triangle's constructor, though.

    Also, you're getPerimeter() method for Right Triangle doesn't work.

  3. #3
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble dealing with objects using abstract methods/classes and polymorphi

    Quote Originally Posted by awinston View Post
    For Square, you can just have one parameter for its constructor because it's length and width are the same.

    However, I don't think it makes sense to have the right triangle extend Rectangle. A square actually is a rectangle, but a right triangle is not. Therefore, right triangle shouldn't be a subclass of Rectangle.

    However, if Right Triangle needs to extend Rectangle, then it's okay to just have base and height as its parameters for the constructor. There's no point in calling Rectangle's constructor within Right Triangle's constructor, though.

    Also, you're getPerimeter() method for Right Triangle doesn't work.
    I don't understand how I can have just one parameter for a constructor if it extends the Rectangle class. Don't you have to also take in the width and length of the rectangle too for the super keyword?

    right triangle must extend Rectangle as it states in the assignment, so that's the best I could do.

  4. #4
    Junior Member awinston's Avatar
    Join Date
    Jul 2012
    Location
    United States
    Posts
    10
    My Mood
    Fine
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble dealing with objects using abstract methods/classes and polymorphi

    Quote Originally Posted by orbin View Post
    I don't understand how I can have just one parameter for a constructor if it extends the Rectangle class. Don't you have to also take in the width and length of the rectangle too for the super keyword?
    I think you might be under the impression that the arguments in the constructor of a subclass have to mirror the arguments in the constructor of its superclass, and this is not the case. A subclass also doesn't need to call its superclass' constructor (technically, it will call its default constructor if you don't specify something else). Try what I suggested, and let me know if you get any errors.
    Last edited by awinston; July 25th, 2012 at 08:26 PM. Reason: Forgot something

  5. #5
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Having trouble dealing with objects using abstract methods/classes and polymorphi

    right triangle must extend Rectangle as it states in the assignment
    What your teacher may have in mind is that both rectangles and right triangles are "rectangular shapes" in the sense that they both have a length and a width. (or a width and a height - call these things what you will)

    The RightTriangle need not have any special instance variables of its own: it can use the width it already has and the length (rather weirdly viewing the length as its height). It does not need a four argument constructor, for the same reason. Of course how it calculates perimeter and area based on these variables will differ from the parent class implementation.

    -----

    It doesn't pay to read too much into this exercise. Classes are what classes do: in other words classes are about behaviour. From this perspective even saying Square extends Rectangle could be dodgy. If setters were ever added some might regard the square's behaviour as odd in as much as the length could change even though no setLength() call was ever made.

    Java actually has a RectangularShape shape which is extended by Rectangle2D, Ellipse2D, Arc2D, etc. A RightTriangle2D would fit in to this structure. But I think it makes more sense for rectangles and right triangles to be specialisations of something else, rather than having right triangles specialise rectangles. "extends" is not supposed to be confusing, any more than homework problems are supposed to confuse.

  6. #6
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble dealing with objects using abstract methods/classes and polymorphi

    Thank you for helping out! I think I'm going to leave the assignment the way it is. I figure I get the point, but it still bugs me that I would have to set the objects to 0,0 to put a right triangle in. I could change it, but I get the point. I just needed to fix the triangle class by adding a hypotenuse variable and adding that for the perimeter method. I just don't like that sometimes the homework is very vague. Thanks again!

  7. #7
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Having trouble dealing with objects using abstract methods/classes and polymorphi

    Actually if you consider the relationship among the limited shapes in the assignment, there is a clear answer.
    All 3 shapes can have the area volume and perimeter calculated by knowing the length of 2 sides. The rectangle requires knowing 2 sides. So the constructor for it would have an imput for 2. The square however, you only need one. So take only one, and when you call the super(rectangle) constructor, send the same thing twice. As far as the right triangle is concerned, no, it is not a square. But it sure is exactly half a square. Seems very possible to extend a right triangle class from a square class to me. At least for this assignment.

  8. #8
    Super Moderator pbrockway2's Avatar
    Join Date
    Jan 2012
    Posts
    987
    Thanks
    6
    Thanked 206 Times in 182 Posts

    Default Re: Having trouble dealing with objects using abstract methods/classes and polymorphi

    As far as the right triangle is concerned, no, it is not a square. But it sure is exactly half a square
    <nitpick>It's half a rectangle. Not a square unless it's a right isosceles triangle.</nitpick>

  9. #9
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Having trouble dealing with objects using abstract methods/classes and polymorphi

    I want to add to this a bit more as it seems to me you have missed a crucial (and possibly the main) point of this assignment, as this is a very crucial concept in object oriented programming.

    When you extend a class, try to picture the "Extends NameOfSuperclass" as being "Import NameOfSuperclass", in a sense.
    All of those pretty methods you have that are doing the same thing, is repeated code. One reason to extend a class is to reuse the code, not rewrite it.

    Consider your getArea method.
    How do you get the area of a rectangle? Simple, multiply a length by a width.
    Very well, seems you have done just that.

    Now how do you get the area of a square? Multiply side by side right? No!
    It just so happens the area of a square is calculated the same as the rectangle! Use the code you already wrote!

    So now how do you get the area of a triangle? With the same concept. You ask the square to calculate it, and modify the value you get from the square to suit the triangle. Which, when you ask the square to calculate it, it is going to ask the rectangle.

    While the triangle asking the square asking the rectangle to calculate area, so the rectangle can tell the square so the square can tell the triangle.... may seem like a lot of extra work, this is what it is all about. In a simple sample like this, it seems far more work than is necessary, but the idea is to get your head around how to reuse code by providing a simple example where you can easily see the relationship from super to sub. As your programs get more involved, you will not be able to hold all of the factors in your head. If you do not learn to let go of the details now, and rely on existing code in the super class to do the work it is present to do, programming will forever be harder than is necessary. Hmmm, Trying to walk the thin line between giving away the answer and providing help on an important concept.

  10. #10
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Having trouble dealing with objects using abstract methods/classes and polymorphi

    Quote Originally Posted by pbrockway2 View Post
    <nitpick>It's half a rectangle. Not a square unless it's a right isosceles triangle.</nitpick>
    lol touche'

  11. #11
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Having trouble dealing with objects using abstract methods/classes and polymorphi

    Ok so geometry was longer ago than I can recall.
    So a correction to my last post.

    As far as the triangle, it should extend the rectangle class. Now that pbrockway2 pointed out my mistake, and i read your instructions again, yes it is clear still. You would still have the triangle use the method from the rectangle class and within the triangle class still modify that return value to get the value for the triangle. Skipping the square class as you would not be extending the square class.
    Somehow I jumped off the deep end on that but I hope this clears it up.

  12. #12
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble dealing with objects using abstract methods/classes and polymorphi

    I just read through the tips you guys gave me, and worked my code over again. This is what I have...but am I understanding what you were getting at?

    import java.util.Scanner;
     
    public class ProgramDriver{
     
    	public static void main(String[] args){
     
    		Scanner sc = new Scanner(System.in);
     
    		Shapes[] container = new Shapes[13];
     
     
     
    		Rectangle rec1 = new Rectangle(10, 12);
    		Rectangle rec2 = new Rectangle(6, 8);
     
    		container[0] = rec1;  //putting rectangle objects in container
    		container[1] = rec2;
     
    		Square sq1 = new Square(6);
    		Square sq2 = new Square(8);
     
    		container[2] = sq1;
    		container[3] = sq2; //putting square objects in container
     
    		RightTriangle rt1 = new RightTriangle(3,4);
    		RightTriangle rt2 = new RightTriangle(5,12);
     
    		container[4] = rt1;
    		container[5] = rt2; //putting right triangle objects in container
     
    		Circle c1 = new Circle(8);
    		Circle c2 = new Circle(11);
     
    		container[6] = c1;
    		container[7] = c2; //putting circle objects in container
     
    		Sphere s1 = new Sphere(15);
    		Sphere s2 = new Sphere(10);
     
    		container[8] = s1;
    		container[9] = s2; //putting sphere objects in container
     
    		Prism p1 = new Prism(5,3,2);
    		Prism p2 = new Prism(6,3,4);
     
    		container[10] = p1; //putting rectangular prism objects in container
    		container[11] = p2;
     
     
     
    		System.out.println("Rectangle 1" + "\n\nArea: " + container[0].getArea() + "\nPerimeter: " + container[0].getPerimeter() + "\nVolume: " + container[0].getVolume());
    		System.out.println("\nRectangle 2" + "\n\nArea: " + container[1].getArea() + "\nPerimeter: " + container[1].getPerimeter() + "\nVolume: " + container[1].getVolume());
    		System.out.println("\nSquare 1" + "\n\nArea: " + container[2].getArea() + "\nPerimeter: " + container[2].getPerimeter() + "\nVolume: " + container[2].getVolume());
    		System.out.println("\nSquare 2" + "\n\nArea: " + container[3].getArea() + "\nPerimeter: " + container[3].getPerimeter() + "\nVolume: " + container[3].getVolume());
    		System.out.println("\nRight Triangle 1" + "\n\nArea: " + container[4].getArea() + "\nPerimeter: " + container[4].getPerimeter() + "\nVolume: " + container[4].getVolume());
    		System.out.println("\nRight Triangle 2" + "\n\nArea: " + container[5].getArea() + "\nPerimeter: " + container[5].getPerimeter() + "\nVolume: " + container[5].getVolume());
    		System.out.println("\nCircle 1" + "\n\nArea: " + container[6].getArea() + "\nPerimeter: " + container[6].getPerimeter() + "\nVolume: " + container[6].getVolume());
    		System.out.println("\nCircle 2" + "\n\nArea: " + container[7].getArea() + "\nPerimeter: " + container[7].getPerimeter() + "\nVolume: " + container[7].getVolume());
    		System.out.println("\nSphere 1" + "\n\nArea: " + container[8].getArea() + "\nPerimeter: " + container[8].getPerimeter() + "\nVolume: " + container[8].getVolume());
    		System.out.println("\nSphere 2" + "\n\nArea: " + container[9].getArea() + "\nPerimeter: " + container[9].getPerimeter() + "\nVolume: " + container[9].getVolume());
    		System.out.println("\nRectangular Prism 1" + "\n\nArea: " + container[10].getArea() + "\nPerimeter: " + container[10].getPerimeter() + "\nVolume: " + container[10].getVolume());
    		System.out.println("\nRectangular Prism 2" + "\n\nArea: " + container[11].getArea() + "\nPerimeter: " + container[11].getPerimeter() + "\nVolume: " + container[11].getVolume());
    		}
     
     
    }

    public abstract class Shapes{
     
    	public Shapes(){
     
     
    	}
     
    	public abstract double getArea();
    	public abstract double getPerimeter();
    	public abstract double getVolume();
    }

    public class Rectangle extends Shapes{
     
    	private double length;
    	private double width;
     
    	public Rectangle(double lr, double wr){
     
    		length = lr;
    		width = wr;
    	}
     
    	public double getArea(){
     
    		double area = length * width;
    		return area;
    	}
     
    	public double getPerimeter(){
     
    		double perimeter = (width*2)+(length*2);
    		return perimeter;
    	}
     
    	public double getVolume(){
     
    	  //2-d shapes cannot have volume
    	return 0;
    	}
     
     
     
    }

    public class Square extends Rectangle{
     
     
     
    	public Square(double side){
     
    		super(side,side);
    	}
     
    	public double getArea(){
     
    		return super.getArea();
     
    	}
     
    	public double getPerimeter(){
     
    	return super.getPerimeter();
    	}
     
    	public double getVolume(){
     
    		 //2-d shapes cannot have volume
    		return super.getVolume();
    	}
     
     
     
    }

    public class RightTriangle extends Rectangle{
     
    	private double base;
    	private double height;
     
    	public RightTriangle(double b, double h){
     
    		super(b,h);
    		base = b;
    		height = h;
    	}
     
    	public double getArea(){
    		return super.getArea()*.5;
     
    	}
     
    	public double getPerimeter(){
     
    	double hyp = Math.sqrt((base * base) + (height * height));
    	return base+height+hyp;
     
    	}
     
    	public double getVolume(){
     
    		return super.getVolume();//2-d shapes cannot have volume
    	}
    }

    I couldn't really "reuse" the code in the triangle class since everything is calculated differently, but I was still able to take in the base and the height. Let me know what you think!

    Output is correct.

    Rectangle 1
     
    Area: 120.0
    Perimeter: 44.0
    Volume: 0.0
     
    Rectangle 2
     
    Area: 48.0
    Perimeter: 28.0
    Volume: 0.0
     
    Square 1
     
    Area: 36.0
    Perimeter: 24.0
    Volume: 0.0
     
    Square 2
     
    Area: 64.0
    Perimeter: 32.0
    Volume: 0.0
     
    Right Triangle 1
     
    Area: 6.0
    Perimeter: 12.0
    Volume: 0.0
     
    Right Triangle 2
     
    Area: 30.0
    Perimeter: 30.0
    Volume: 0.0

  13. #13
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Having trouble dealing with objects using abstract methods/classes and polymorphi

    Quote Originally Posted by orbin View Post
    I couldn't really "reuse" the code in the triangle class since everything is calculated differently
    The reason you extended rectangle rather than just using it, is because some things are different. The important concept is to reuse what you can, and modify for your specific case in the new class. Perfect.
    public double getArea(){
    		return super.getArea()*.5;
    	}

  14. #14
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Having trouble dealing with objects using abstract methods/classes and polymorphi

    private double base;
    private double height;

    public RightTriangle(double b, double h){

    super(b,h);
    base = b;
    height = h;
    }
    Are you storing the same value in 2 different places? Once witin (super)rectangle's variables and once again within the triangle? Certianly don't need to keep the same data twice... Should be a way to store it only once.
    Sure the rectangle calls it length and width, but when the triangle uses it, you can call it fred and joe, makes no difference. Still need store it in only one place, and since the super class has the variables (which translate well to triangle) might as well use that existing code where you can.

  15. #15
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble dealing with objects using abstract methods/classes and polymorphi

    Quote Originally Posted by jps View Post
    Are you storing the same value in 2 different places? Once witin (super)rectangle's variables and once again within the triangle? Certianly don't need to keep the same data twice... Should be a way to store it only once.
    Sure the rectangle calls it length and width, but when the triangle uses it, you can call it fred and joe, makes no difference. Still need store it in only one place, and since the super class has the variables (which translate well to triangle) might as well use that existing code where you can.
    Yes I store the same variable in two places. In the constructor I needed to send the numbers back to the super, but I also had to use the variables in other methods within the class to do different calculations that rectangle couldn't handle. I'm not sure how else I could have reused anything else. The perimeter is something completely different from the rectangle.

  16. #16
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Having trouble dealing with objects using abstract methods/classes and polymorphi

    When you say:
    super(b,h) you store your base and height in your super(rectangle) class. Rather than using new variables in the extended(triangle) class, you can refer to those in the super. Base could go in the length variable for example.

    You did a great job on getting the reuse from the methods. Just do the same thing with your variables.

  17. #17
    Member
    Join Date
    Jun 2012
    Posts
    59
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Having trouble dealing with objects using abstract methods/classes and polymorphi

    Quote Originally Posted by jps View Post
    When you say:
    super(b,h) you store your base and height in your super(rectangle) class. Rather than using new variables in the extended(triangle) class, you can refer to those in the super. Base could go in the length variable for example.

    You did a great job on getting the reuse from the methods. Just do the same thing with your variables.

    Do you mean that I should make the variables in Rectangle class public? If so, I did that and decided to use them in the right triangle class. This is the result.


    public class RightTriangle extends Rectangle{
     
    	public RightTriangle(double b, double h){
     
    		super(b,h);
    		length = b;
    		width = h;
    	}
     
    	public double getArea(){
    		return super.getArea()*.5;
     
    	}
     
    	public double getPerimeter(){
     
    	double hyp = Math.sqrt((length * length) + (width * width));
    	return length+width+hyp;
     
    	}
     
    	public double getVolume(){
     
    		return super.getVolume();//2-d shapes cannot have volume
    	}
    }

  18. #18
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Having trouble dealing with objects using abstract methods/classes and polymorphi

    Well I wouldn't necessarily make the variables public, rather keep them private and use get and set methods to provide access to them. Protected would provide access to them in the triangle class because the triangle class extends rectangle.

Similar Threads

  1. Why and where abstract methods & classes and static methods are used?
    By ajaysharma in forum Object Oriented Programming
    Replies: 7
    Last Post: July 14th, 2012, 01:16 AM
  2. Replies: 3
    Last Post: July 8th, 2012, 03:44 PM
  3. Replies: 1
    Last Post: April 14th, 2012, 01:45 PM
  4. New Programming Student, Having Trouble With Abstract Classes/Interfaces
    By bulx0001 in forum Object Oriented Programming
    Replies: 2
    Last Post: February 10th, 2012, 07:24 AM
  5. [SOLVED] Abstract Classes Help
    By SweetyStacey in forum Object Oriented Programming
    Replies: 10
    Last Post: May 6th, 2010, 06:15 AM