hi need help with inheritance
hi i m a beginner creating a national database program for practice.
i have 5 packages in my program each having 1 class
1. general (class gen)
class gen contains basic info like name age etc.
2. nii ( class nii)
this class contains identity information like nicno, mark of identification etc
3. medical (class medical)
this information contains the medical info like diseases, treatments etc.
4. insurance (class insure)
this class contains insurance info like insurance id, accidents etc.
5. joint (class national)
this class contains all the information of a person. gen+nii+health+medical
now i want to extend medical with nii and nii with gen + insure with nii, and nii with general
then i want to inherit all the classes variables above in nationals.
so that only nationals have all the information, insure have only insrance + general+ nii information and
medical have their information and medical and insurance cant share information with eachother..
can any one help me please how i can do this??
Re: hi need help with inheritance
Re: hi need help with inheritance
i thought may be i could get answer fast. or i can get more replies and learn more -_-.
Re: hi need help with inheritance
Quote:
Originally Posted by
lisa.abraham
i thought may be i could get answer fast. or i can get more replies and learn more -_-.
Sure, but please look at it from our point of view. Imagine you've spent a good time crafting an answer including sample code only to find that the question was already answered an hour ago in a cross-post. If this happens enough, you get pretty jaded at cross-posters.
So the best solution is to go ahead and cross-post if you feel you must, but also please be considerate and let all your posts know about the other cross-posts including links if possible.
Re: hi need help with inheritance
Ummmm.... you know you cannot extend beyond one class right?... Right?
You can extend to one class and inheritances all of it's stuff, but you can only do this for one class. You want Nii gen + insure + etc. (That line got really confusing.) Aside from that, do you have to use inheritance? Why not just use Objects? You can have your classes create objects from the other classes. Make sure that the variables in the classes you are going to use in other classes are public so all classes that create that class as an object can use them. You might want to make a lot of the variables and methods static so that all Objects have the same values.
For example:
Code:
public class Medical {
Nii nii = new Nii();
}
public class Nii {
Gen gen = new Gen();
//this will keep medical from using this Object.
private Insure ins = new Insure();
}
public class National {
Medical med = new Medical();
//using med like this can allow you access to all variables and functins within Medical, including Nii.*
med.nii.gen.variable = true;
//This will throw an error because insurance is private to only nii.
med.ins.variable = false;
}
Quote:
so that only nationals have all the information, insure have only insrance + general+ nii information and
medical have their information and medical and insurance cant share information with eachother..
This is where I get confused! I tried my best to figure out what you wanted, but I might be wrong. Can you rephrase this to make it more clear as to what you are going to do?
If you are going to set in inheritance (or even use objects), be careful not to cross inherent two classes. Because you get a nasty loop that will kill your program if it goes on for too long.
Re: hi need help with inheritance
Inheritance represents a "is a sort of" relationship. It's always possible to graft insurance information onto medical information, but such chimeras are apt to be confusing. A Person (capitals whereever I'm thinking of a class) has both Medical and Insurance information. But their Medical isn't a sort of special MedicalPlusInsurance, it's just a Medical instance with no special type relationship to other aspects of their identity.
Prefer composiition over inheritance, as the slogan goes. Give the Person a Medical instance variable and an Insurance instance variable. And so on.
Avoid static though. Unless you can express clearly why something should be static.
Re: hi need help with inheritance
well, it helped me alot :) thnx mates :)
i wanted that the access of the medical instance should not be given to the insure instance, and access of insure instance should not be given to the medical method :) .
and another question, does it really necessary to use privated and protected specifers. whats wrong with public when we can always use it?
Re: hi need help with inheritance
What happens if you classes or components that you don't wish other classes use? Hiding information is a hugely important part of good OOPs practice. It allows you to control what parts of your code can be manipulated by outside forces, and what parts should be sacrosanct and untouchable from the outside.