Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


[Java] Abstract class problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 Cuajin   Members   -  Reputation: 155

Like
0Likes
Like

Posted 27 March 2012 - 01:14 PM

Hi.

I have a Point3D class, Shapes abstract class, and a Sphere that extends Shapes.

I want Shapes to have a member object from Point3D.

public class Point3D
{
	private float x, y, z;
	public Point3D(float x, float y, float z)
	{
		this.x = x;
		this.y = y;
		this.z = z;
	}
}

public abstract class Shape
{
	private Point3D point;
  
	public Shape()
	{
		point = new Point3D() ;
	}
}

public class Sphere extends Shape
{
	private double radius;
	public Sphere(float x, float y, float z, float radius)
	{
		 // How do I pass the x, y ,z values to point3D?
	}
}

I want to create the sphere but by making sure that I pass values to the point3D, how do I do it?

Also I have a feeling that putting a constructor on an abstract class is wrong.

Please help.

Sponsor:

#2 dmatter   Crossbones+   -  Reputation: 3262

Like
0Likes
Like

Posted 27 March 2012 - 02:19 PM

I want to create the sphere but by making sure that I pass values to the point3D, how do I do it?

Also I have a feeling that putting a constructor on an abstract class is wrong.


Constructors on abstract classes are quite common and is not inherently wrong in any way.
The solution therefore is to add a constructor to Shape that takes the x, y, z components and initialises the Point3D member. You can call the constructor from Sphere using the super keyword: super(x, y, z);

As an aside, the code you posted shows you attempting to use a default constructor on the Point3D class but no such constructor is defined; you would need to explicitly add a default constructor for that code to compile.

#3 Cuajin   Members   -  Reputation: 155

Like
0Likes
Like

Posted 27 March 2012 - 08:51 PM

Thank you very much.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS