Results 1 to 8 of 8
Like Tree3Likes
  • 1 Post By kammce
  • 2 Post By pbrockway2

Thread: hi need help with inheritance

  1. #1
    lisa.abraham is offline Member
    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Default 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??

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,642
    Rep Power
    7

    Default Re: hi need help with inheritance


  3. #3
    lisa.abraham is offline Member
    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Default Re: hi need help with inheritance

    i thought may be i could get answer fast. or i can get more replies and learn more -_-.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: hi need help with inheritance

    Quote Originally Posted by lisa.abraham View Post
    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.

  5. #5
    kammce's Avatar
    kammce is offline Senior Member
    Join Date
    Dec 2010
    Location
    California
    Posts
    194
    Rep Power
    4

    Default 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:
    Java 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;
    }
    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.
    lisa.abraham likes this.
    My API:
    Java Code:
    cat > a.out || cat > main.class

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default 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.
    Fubarable and lisa.abraham like this.

  7. #7
    lisa.abraham is offline Member
    Join Date
    Jul 2012
    Posts
    7
    Rep Power
    0

    Default 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?

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

Similar Threads

  1. using inheritance
    By droidus in forum New To Java
    Replies: 1
    Last Post: 10-18-2011, 02:38 AM
  2. Inheritance
    By new_2_j in forum New To Java
    Replies: 5
    Last Post: 02-20-2011, 05:56 PM
  3. JPA Inheritance
    By videanuadrian in forum New To Java
    Replies: 1
    Last Post: 01-10-2011, 06:44 AM
  4. Inheritance
    By gpio in forum Advanced Java
    Replies: 14
    Last Post: 11-20-2009, 10:41 AM
  5. Inheritance in GUI
    By Marty in forum SWT / JFace
    Replies: 2
    Last Post: 05-11-2007, 12:54 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
  •