I am confused about composition in java. I am able to write code using inheritance...but have no idea how to turn it into composition...from what my textbook said the two are quite similar has a is a relationship and all.

I ultimately would like to write the cylinder code using composition...but I just dont think i get how to apply the syntax.

public class Circle 
{
	private double radius;
	
	public Circle(){}
		public Circle(double radius)
		{
			this.radius = radius;
		}
		public void setRadius(double radius)
		{
			this.radius = radius;
		}
		public double findArea()
		{
			return radius*radius*3.14;
		}
		public double findPerimeter()
		{
			return (radius*2)*3.14;
		}
}
public class Cylinder extends Circle
{
	private double length = 1;
	
		public double getLength()
		{
			return length;
		}
	public void setLength(double length)
	{
		this.length = length;
	}
	public double findCylinderArea()
	{
		return (findArea()* 2) + (findPerimeter()*length);
	}
	public double findVolume()
	{
		return findArea() * length;
	}
}

I also have a test class...

public class TestCircle extends Circle
{
	public static void main(String[] args)
	{
		TestCircle tc1 = new TestCircle();
		tc1.setRadius(3);
		System.out.println("Circle Perimeter = "+tc1.findPerimeter());
		System.out.println("Circle Area = "+tc1.findArea());
		
		Cylinder c1 = new Cylinder();
		c1.setRadius(3);
		c1.setLength(8);
		System.out.println("\nCylinder Area = "+ c1.findCylinderArea());
		System.out.println("Cylinder Volume = "+ c1.findVolume());
	}
}

To use composition in Cylinder, you would add a Circle instance variable instead of extending Circle. When you need any circle info, you make the calls on your circle reference instead of inherited methods. So for example, where are calling findArea() in the findVolumne() method, you would instead call circle.findArea() .

You may need to expose new method on the Cylinder class to set properties of the circle.

Edited 5 Years Ago by Ezzaral: n/a

So basically like this?

public class Cylinder // Composition
{
	private double length = 1;
	
	Circle mycircle = new Circle();

	private Object radius;

	public double getLength()
	{
		return length;
	}
	
	public void setLength(double length)
	{
		this.length = length;
	}
	
	public double findCylinderArea()
	{
		return (Circle.findArea()* 2) + (Circle.findPerimeter()*length);
	}

	public double findVolume()
	{
		return Circle.findArea() * length;
	}

	public void setRadius(double r) 
	{
		this.radius = radius;
	}
}

Close. You need to call the methods on your variable 'myCircle' instead of the class. You should set the radius on your circle variable too. You do not need a separate radius property for the Cylinder.

The basic idea is to let the circle variable handle all of the data and methods that you were previously inheriting. If you need to get that data, you simply ask the circle for it.

This is kind of similar to what i have to do so i am going to pot here. I have trouble with the composition code. Is my code bellow correct?

public class Comp
	{
		private double length;
		Circle mycircle;
		 
		public CylinderComp (double l, double r)
		{
			setLength(l);
			mycircle = new Circle(r);
		}
		
		public double getLength()
		{
			return length;
		}
		 
		public void setLength(double length)
		{
			this.length = length;
		}
		 
		public double getCylinderArea()
		{
			return (mycircle.getArea()* 2) + (mycircle.getPerimeter()*length);
		}
		 
		public double getVolume()
		{
			return mycircle.getArea() * length;
		}
		 
	}
This article has been dead for over six months. Start a new discussion instead.