HAS-A relationship

As compared to an IS-A relationship, a HAS-A relationship is easy to identify and implement. I hope this statement relieves you! Consider this definition of the bare-bones class Engine:

Of all the preceding classes—Statistics, Car, PartsFactory, and TestCar—only Car shares a HAS-A relationship with the class Engine because Car defines an instance variable of type Engine. Note that it doesn’t matter whether the instance variable engine in class Car is initialized with an object. The HAS-A relationship is shared by the classes.

Case Study 1:

Employee and Department :

package com.gs.corejava.composition;

public class Depratment {

	private int dpId;
	private String dname;

	// dept has one emp
	private Emply[] emplarray;

	/**
	 * @return the emply
	 */
	public Emply[] getEmply() {
		return emplarray;
	}

	/**
	 * @param emply
	 *            the emply to set
	 */
	public void setEmply(Emply[] emplarray) {
		this.emplarray = emplarray;
	}

	public Depratment() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Depratment(int dpId, String dname, Emply[] emply) {
		this.dpId = dpId;
		this.dname = dname;
		this.emplarray = emply;
	}

	/**
	 * @return the dpId
	 */
	public int getDpId() {
		return dpId;
	}

	/**
	 * @param dpId
	 *            the dpId to set
	 */
	public void setDpId(int dpId) {
		this.dpId = dpId;
	}

	/**
	 * @return the dname
	 */
	public String getDname() {
		return dname;
	}

	/**
	 * @param dname
	 *            the dname to set
	 */
	public void setDname(String dname) {
		this.dname = dname;
	}

}
package com.gs.corejava.composition;

//bean pojo  model
public class Emply {

	private int id;
	private String name;
	private int age;
	private double salary;

	public Emply() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Emply(int id, String name, int age, double salary) {
		this.id = id;
		this.name = name;
		this.age = age;
		this.salary = salary;
	}

	/**
	 * @return the id
	 */
	public int getId() {
		return id;
	}

	/**
	 * @param id
	 *            the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}

	/**
	 * @return the name
	 */
	public String getName() {
		return name;
	}

	/**
	 * @param name
	 *            the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * @return the age
	 */
	public int getAge() {
		return age;
	}

	/**
	 * @param age
	 *            the age to set
	 */
	public void setAge(int age) {
		this.age = age;
	}

	/**
	 * @return the salary
	 */
	public double getSalary() {
		return salary;
	}

	/**
	 * @param salary
	 *            the salary to set
	 */
	public void setSalary(double salary) {
		this.salary = salary;
	}

}
package com.gs.corejava.composition;

public class CompanyTest {

	public static void main(String[] args) {
		Emply emply1 = new Emply(12, "mohit", 23, 22423423);

		Emply emply2 = new Emply(14, "rohit", 25, 234324);

		Emply[] empArray = new Emply[2];
		empArray[0] = emply1;
		empArray[1] = emply2;

		Depratment IT = new Depratment();
		IT.setDpId(133);
		IT.setDname("IT");
		IT.setEmply(empArray);

		dislayDeptDetails(IT);

	}

	private static void dislayDeptDetails(Depratment iT) {

		System.out.println("Dept id is " + iT.getDpId());
		System.out.println("Dept name is " + iT.getDname());
		Emply[] emplyarr = iT.getEmply();
		for (Emply emply : emplyarr) {
			System.out.println("Emp name for IT is " + emply.getName());
		}

	}

}

Last updated