Jump to content
  • Advertisement
Sign in to follow this  
Cuajin

[Java] Abstract class problem

This topic is 2280 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
Advertisement

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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!