Results 1 to 13 of 13

Thread: Inheritance

  1. #1
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Inheritance

    Do the objects/instances of a subclass share the non static attributes ( or instance variables) of the objects/instances of the super class?

    EXAMPLE:-

    Java Code:
    public Class Plant{
    protected static String color;
    protected static int size;
    protected int cone_size; //****************************************************
    Plant(String col, int siz, int con){
    color=col;
    size=siz;
    cone_size= con; 
    }
    };
    
    
    
    Class FloweringPlant extends Plant{ //floweringplants
    public String flower_color;
    FloweringPlant( String col){
    flower_color= col;}
    };
    
    
    Class Returning{
    returnPlantValues(){
    Plant angio= new Plant("green", 3, 1);
    return angio;
    }
    };
    
    
    //FLOWERING PLANTS= PRODUCE FLOWERS AND SEEDS PROTECTED IN FRUIT, ALSO CALLED ANGIOSPERMS
    //NONFLOWERING PLANTS = PRODUCE SEEDS PROTECTED BY WOODEN CONE, ALSO CALLED GYMNOSPERMS
    public static void main(String[] args){
    Returning r=new Returning;
    System.out.println("PLANT CHARACTERISTICS: " + r.returnPlantValues();
    System.out.println("FLOWERING PLANT CHARACTERISTICS:"flower_color + r.returnPlantValues();  //PROBLEM*****************
    }

    See the line of code with the comment: //PROBLEM*****************

    It prints the flower_color which is the attribute of the class FloweringPlant, as well as the information contained in the angio instance of Plant class. Now angio has the static fields of plant i.e. color and size, as well as the nonstatic field cone_size which is not shared with other instances of the Plant class.

    My question is that will the subclass FloweringPlant inherit the nonstatic field i.e. cone_size of its superclass Plant.
    Last edited by Nazneen Ali; 10-10-2013 at 10:20 AM.
    Don't forget to smile :-)

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default Re: Inheritance

    Java Code:
    System.out.println("FLOWERING PLANT CHARACTERISTICS:"flower_color + r.returnPlantValues();
    The problem with that line of code has nothing to do with inheritance and all to do with the three syntax errors.

    Wow!

    I just spotted numerous syntax errors throughout the rest of the code. Please fix these before trying to do anything else.

  3. #3
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Inheritance

    Like Junky has pointed... there are a lot of syntax errors in your code... try to fix them
    I'll point out the ones that are easily notable

    1) the semicolons on lines 10, 18, 26
    2) in Java all methods should be defined inside a class... your main method does'nt seem to be within any class
    3)line 22... that's not how a method is declared ... a method should have modifiers
    4) costructor chaining

    try to sort your code out
    Last edited by Kagiso; 10-10-2013 at 12:59 PM.

  4. #4
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,930
    Rep Power
    19

    Default Re: Inheritance

    You have two different things going on there.
    The things an object has (which you ask in the first sentence) and the things a subclass inherits.

    An object of a class has all the members of its parent class. Note, this is not inheritance, at this point we are talking the "physical" object on the heap.

    A subclass does not inherit private members. This is defined in the JLS.

    Classes inherit, objects don't.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: Inheritance

    The problem with that line of code has nothing to do with inheritance and all to do with the three syntax errors.

    Wow!

    I just spotted numerous syntax errors throughout the rest of the code. Please fix these before trying to do anything else.






    OK! Let's forget about the code please I can't compile it due to some reason, I was just trying to give an example. Please tell me does a subclass inherit "non-static fields(instance variables)" of a super class?
    Don't forget to smile :-)

  6. #6
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: Inheritance

    Like Junky has pointed... there are a lot of syntax errors in your code... try to fix them
    I'll point out the ones that are easily notable

    1) the semicolons on lines 10, 18, 26
    2) in Java all methods should be defined inside a class... your main method does'nt seem to be within any class
    3)line 22... that's not how a method is declared ... a method should have modifiers

    try to sort your code out

    OK! Let's forget about the code please I can't compile it due to some reason, I was just trying to give an example. Please tell me does a subclass inherit "non-static fields(instance variables)" of a super class?
    Don't forget to smile :-)

  7. #7
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Inheritance

    Quote Originally Posted by Nazneen Ali View Post


    OK! Let's forget about the code please I can't compile it due to some reason, I was just trying to give an example. Please tell me does a subclass inherit "non-static fields(instance variables)" of a super class?
    It's worthless for you to try and understand concepts like inheritance, when you can't rectify syntax errors..... But answering your question I'd say yes it does
    I'll post a simple code to make it clearer for you

  8. #8
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Inheritance

    Java Code:
    class Parent
    {
    	String language = "English";  // note that this is an instance Variable
    }
    
    class Child extends Parent 
    {
            // Child doesn't have a string variable called language bu it inherits it from parent
    }
    
    public class Parent_Child
    {
    	public static void main(String[] a)
    	{
    		Child c = new Child();
    		System.out.println("Child's language is " + c.language); // prints a child's language
    	}
    }
    Last edited by Kagiso; 10-10-2013 at 12:57 PM.

  9. #9
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: Inheritance

    But answering your question I'd say yes it does
    Thank you very much. I'll work on my syntax flaws. Thanks for the code as well.
    Don't forget to smile :-)

  10. #10
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,930
    Rep Power
    19

    Default Re: Inheritance

    Quote Originally Posted by Kagiso View Post
    It's worthless for you to try and understand concepts like inheritance, when you can't rectify syntax errors..... But answering your question I'd say yes it does
    I'll post a simple code to make it clearer for you
    If the non-static field in question is private then no, it doesn't.
    As the link I posted earlier to the JLS says.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    Kagiso is offline Member
    Join Date
    Nov 2012
    Location
    Johannesburg, South Africa
    Posts
    92
    Rep Power
    0

    Default Re: Inheritance

    Quote Originally Posted by Tolls View Post
    If the non-static field in question is private then no, it doesn't.
    As the link I posted earlier to the JLS says.
    Yes I forgot to mention it ....thanx Tolls

  12. #12
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,930
    Rep Power
    19

    Default Re: Inheritance

    Quote Originally Posted by Kagiso View Post
    Yes I forgot to mention it ....thanx Tolls
    Essentially it boils down to "if you can't access it, you haven't inherited it", which covers private methods as well.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  13. #13
    Nazneen Ali is offline Senior Member
    Join Date
    Jul 2011
    Posts
    112
    Rep Power
    0

    Default Re: Inheritance

    Thank you everyone.
    Don't forget to smile :-)

Similar Threads

  1. Inheritance
    By Rubel Rana in forum New To Java
    Replies: 3
    Last Post: 10-10-2013, 05:37 PM
  2. using inheritance
    By droidus in forum New To Java
    Replies: 1
    Last Post: 10-18-2011, 02:38 AM
  3. Inheritance
    By terahawks in forum New To Java
    Replies: 1
    Last Post: 04-23-2010, 09:58 AM
  4. Inheritance
    By Nerijus in forum New To Java
    Replies: 5
    Last Post: 04-20-2010, 03:47 AM
  5. Inheritance
    By gpio in forum Advanced Java
    Replies: 14
    Last Post: 11-20-2009, 10:41 AM

Tags for this Thread

Posting Permissions

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