Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Composition and Inheritance

    Hello,

    I'm reading "Thinking in Java 4th Edition" and I try to come up with some exercises to understand the concepts taught in this book. So I wrote this very basic program, and I would appreciate if someone could take a glance on this example and check if I used Composition and Inheritance the right way.

    So there are basically 4 classes: Plane, Engine, FuelTank and F16

    I use Composition in the Plane class, because a Plane has an engine and a fueltank.
    I inherent the class Plane in the F-16 class because a F-16 is a Plane

    Java Code:
    public class FuelTank {
    	int capacity = 0;
    	int fuelgauge = 0;
    	
    	public FuelTank(int cap, int fuel) {
    		capacity = cap;
    		fuelgauge = fuel;
    	}
    
    public class Engine {
    	boolean isOn;
    	int thrustValve;
    	
    	public Engine(boolean isOn, int valve){
    		this.isOn = isOn;
    		thrustValve = valve;
    	}
    }
    
    public abstract class Plane {
    	//creates an empty FuelTank with a capacity of 120L
    	FuelTank mainTank= new FuelTank(120, 0);
    	//creates an Engine that is turned off, thrustValve set to 0 
    	Engine mainEngine = new Engine(false, 0);
    }
    
    public class F16 extends Plane{
    	
    	public F16(int cap, int fuel, boolean isOn, int throttle){
    		this.mainTank.capacity = cap;
    		this.mainTank.fuelgauge = fuel;
    		this.mainEngine.isOn = isOn;
    		this.mainEngine.thrustValve = throttle;
    	}
    	
    	public static void main(String[] args) {
    		//create an F16
    		F16 f16 = new F16(120, 0, false, 0);	
    	}	
    }
    I'd also like to know if it makes sense to define Plane as an abstract class. In the one hand I don't want anyone to create an instance of Plane, but on the other hand I created an instance of the F-16 class which inherents the Plane.

    Many Thanks in advance and sorry for my bad English

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Composition and Inheritance

    I would give Plane a constructor that either takes a Tank and Engine, or at least the parameters.
    The former would allow you to supply different types of FuelTank and Engine later on if you wanted to change the structure...Ramjet as opposed to Prop (say).

    Subclasses should (generally) not have direct access to the parents attributes. You might have some logic in Plane that requires certain things to be done when the engine values are changed, but allowing a subclass to change those values directly would bypass that logic.

  3. #3
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Re: Composition and Inheritance

    So I tried to achieve your concept, but I don't think I did it the right way - it works, but I have an empty constructor for the Plane class

    Subclasses should (generally) not have direct access to the parents attributes. You might have some logic in Plane that requires certain things to be done when the engine values are changed, but allowing a subclass to change those values directly would bypass that logic.
    So I guess that means make the fields private and access them via set and get methods ?



    Java Code:
    public class FuelTank {
    	private int capacity = 0;
    	private int fuelGauge = 0;
    	
    	public FuelTank(int cap, int fuel) {
    		setCapacity(cap);
    		setFuelGauge(fuel);
    	}
    	
    	public void setCapacity(int capacity) {
    		this.capacity = capacity;
    	}
    	
    	public int getCapacity() {
    		return capacity;
    		
    	}
    	
    	public void setFuelGauge(int fuelGauge) {
    		this.fuelGauge = fuelGauge;
    	}
    	
    	public int getFuelGauge() {
    		return fuelGauge;
    	}
    }
    
    public class Engine {
    	private boolean isOn;
    	private int thrustValve;
    	
    	public Engine(boolean isOn, int thrustValve){
    		setIsOn(false);
    		setThrottleValve(thrustValve);
    	}
    	
    	public void setThrottleValve(int thrustValve) {
    		this.thrustValve = thrustValve;
    	}
    	
    	public int getThrottleValve() {
    		return thrustValve;
    	}
    	
    	public void setIsOn(boolean isOn) {
    		this.isOn = isOn;
    	}
    	
    	public boolean getIsOn() {
    		return isOn;
    	}
    }
    
    abstract class Plane {
    	private int airSpeed = 0;
    	private int throttleController = 0;
    	
    	public Plane(FuelTank tank, Engine engine) {	
    		
    	}
    	
    	public void setAirSpeed(int airSpeed) {
    		this.airSpeed = airSpeed;
    	}
    	
    	public int getAirSpeed() {
    		return airSpeed;
    	}
    	
    	public void setThrottleController(int throttleController) {
    		this.throttleController = throttleController;
    	}
    	
    	public int getThrottleController() {
    		return throttleController;
    	}
    }
    
    public class F16 extends Plane{
    
    	public F16(FuelTank tank, Engine engine) {
    		super(tank, engine);
    	}
    }
    
    public class PlaneFactory {
    
            //create an FuelTank and Engine and build a Plane with that components
    	public static void main(String [] args) {
    		Engine GE100 = new Engine(false, 0);
    		FuelTank mainTank = new FuelTank(4000, 0);
    		F16 f16 = new F16(mainTank, GE100);
    		f16.setThrottleController(0);
    		f16.getThrottleController();
    		
    		f16.setAirSpeed(0);
    		f16.getAirSpeed();
    		System.out.println("AirSpeed: " + f16.airSpeed + "\tThrottle Control: " + f16.throttleController);
    	}
    }
    Thanks for your help

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Composition and Inheritance

    It's empty because your Plane has no engine or fuel tank attributes.

Similar Threads

  1. Change program written with inheritance to composition
    By subiedude101 in forum New To Java
    Replies: 1
    Last Post: 03-21-2011, 02:14 AM
  2. Composition vs inheritance
    By mindus in forum Advanced Java
    Replies: 3
    Last Post: 09-27-2010, 07:41 AM
  3. composition Vs Inheritance
    By mindus in forum New To Java
    Replies: 1
    Last Post: 09-26-2010, 07:53 PM
  4. I need some help with composition, i think
    By Nerijus in forum New To Java
    Replies: 2
    Last Post: 03-04-2010, 06:24 PM
  5. What is the Composition in Java
    By Santoshbk in forum Advanced Java
    Replies: 4
    Last Post: 04-10-2008, 11:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •