i have a composition off two classes A,B. I want to change the code so i can't create objects A in main only through B can anyone help ?????
Printable View
i have a composition off two classes A,B. I want to change the code so i can't create objects A in main only through B can anyone help ?????
Why do you think you want to do this?
You could make A a private inner class of B?
its a project for my school and i want the composition to remain after the transform
I have no idea what that means. Have you tried my suggestion?
yes i know i tried that is there another way ot do this
What was the problem with that method? What about your requirements did it not satisfy?
package askisisi1;
class Member
{
private String fname;
private int age;
private int gender;
public Member(String fn,int a,int g)
{
fname=fn;
age=a;
gender=g;
}
public int getAge()
{
return age;
}
}
class Family
{
private String lname;
private int nextChild;
private Member father;
private Member mother;
private Member children[];
private Member createMember(String n, int age, int gd)
{
if((age >= 0) && (gd >= 1) && (gd <= 2))
{
return new Member(n, age, gd);
}
else
{
System.out.println("Error in Member creation !");
return null;
}
}
public Family(String ln, int maxChildren)
{
lname = ln;
children = new Member [maxChildren];
}
public void setFather (String n, int age, int gd)
{
father = createMember(n, age, gd);
}
public void setMother(String n, int age, int gd)
{
mother = createMember(n, age, gd);
}
public void addChild(String n, int age, int gd)
{
children[nextChild++] = createMember(n, age, gd);
}
public float avgAge()
{
float avg = 0;
int memberNo = 0;
if (father != null)
{
memberNo++;
avg += father.getAge();
}
if(mother != null)
{
memberNo++;
avg += mother.getAge();
}
for (int i = 0; i < nextChild; i++)
{
memberNo++;
avg += children[i].getAge();
}
return (memberNo > 0) ? avg/memberNo : 0 ;
}
}
public class Main
{
public static void main(String[] args)
{
Family f=new Family("makis",5);
//f.setFather("sjdhgjkshg",55,0);
Member m=new Member("hgfjyh",45,1);
System.out.println(m.getAge());
}
}
i have this code and i want to change it so i can't create objects off class member into main and the composition i have to remain...
Again, you say what you want to do, but no why you want to do it. There is almost definitely a better way to accomplish your goal.
You could also try using packages, I suppose.
the code i sent before i didn't write it my self its part of school project so i would like to change it so then i create an object Family and then to create objects member. and not to have an object meber that doesn't belong to a Family object
...yes, and I've given you two suggestions about how to do that. You've simply denied them without an explanation as to why they aren't adequate.
i want the two class to be separate....and member constructor must called only from class family...because i want every member to participate in a family always...(never alone)
Make the Member constructor require a Family object
as an argument... and throw exception if that Family object
is null?
I'm not sure that would satisfy the OP's requirement that a Member should not be instantiable from main- because there is nothing stopping main from instantiating a Family Object and passing that in.
That's why I've been asking the OP to be more specific about his requirements, or why the suggestions he has already received are inadequate. Chances are he's simply approaching the problem from the wrong angle.
I thought of that.
I think his main requirement is the second one of the above. If not I was going to point him to method modifiers.. and packages, like you suggested.Quote:
and member constructor must called only from class family...because i want every member to participate in a family always...