Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Using a constructor within a subclass

  1. #1
    Junior Member
    Join Date
    Jun 2020
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Using a constructor within a subclass

    So I use Eclipse IDE. I am just practising with Java, so I'm only doing silly practise projects with stuff like Fruit classes and what not. To try out class extensions, I have one file, Fruit.java, and another, FruitRunner.java (to hold main, because putting main inside of an inner class causes Eclipse to malfunction).
    Here is the content of Fruit.java:
    package basics;
     
     
    class Fruit {
    	protected boolean has_seeds = true;
    }
     
    class Apple extends Fruit {
    	String color;
    	String type;
     
    	public Apple(String color_, String type_) {
    		String color = color_;
    		String type = type_;
    	}
     
    }

    And here is my runner:

    package basics;
     
    public class FruitRunner {
     
    	public static void main(String[] args) {
    		Apple apple1 = new Apple("Green", "Granny Smith");
     
    		System.out.println("This is a " + apple1.color + ' ' + apple1.type);
     
    		if (apple1.has_seeds) {
    			System.out.println("It has seeds");
    		}
    }
    }
    Some things to note:

    >The problem is that this will print "This is a null null" instead of a "This is a Green Granny Smith".
    >In Fruit.java, Eclipse says that apple1.color and apple1.type are never used, which of course makes sense because it manifests as null in the runner.
    >The only reason I have FruitRunner.java is because when I tried declaring main inside of that inner class, Eclipse said it failed to find main() .

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,186
    Thanks
    53
    Thanked 2,465 Times in 2,416 Posts

    Default Re: Using a constructor within a subclass

    There are two variables named color declared in the Apple class, one in the constructor and one in the class. The variable in the constructor shadows/hides the class variable. IE changes to the variable declared in the constructor are not seen in the class variable.
    Change the code so there is only one variable declared at class level and refer to that one in the constructor.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jun 2020
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using a constructor within a subclass

    I'm afraid I don't understand. Could you be more concrete?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,186
    Thanks
    53
    Thanked 2,465 Times in 2,416 Posts

    Default Re: Using a constructor within a subclass

    class Apple extends Fruit {
    	String color;     //  Declare a variable named color at the class level
    	String type;
     
    	public Apple(String color_, String type_) {
    		String color = color_;   //  Declare a local variable named color.  This variable goes away when the constructor exits.
    		String type = type_;
    	}
    }
    The constructor needs to assign the value of color_ to the color variable declared at the class level.
    There should not be a variable named color declared in the constructor.

    I had mistakenly referred to the Fruit class when I meant the Apple class. Comment has been fixed.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2020
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Using a constructor within a subclass

    Change the constructor as Norm says.

    class Apple extends Fruit {
    String color;
    String type;

    public Apple(String color_, String type_) {
    this.color = color_;
    this.type = type_;
    }
    }

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,186
    Thanks
    53
    Thanked 2,465 Times in 2,416 Posts

    Default Re: Using a constructor within a subclass

    http://www.java-forums.org/misc.php?do=bbcode#code
    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Subclass not compiling.
    By Praetorian in forum What's Wrong With My Code?
    Replies: 6
    Last Post: May 3rd, 2013, 07:08 PM
  2. How to end a subclass?
    By Purple01 in forum Java Theory & Questions
    Replies: 2
    Last Post: November 21st, 2012, 02:26 AM
  3. which class has a default constructor? (Req. guidance on constructor)
    By DragBall in forum Java Theory & Questions
    Replies: 1
    Last Post: June 27th, 2012, 04:42 PM
  4. [SOLVED] Invoking a superclass constructor in my subclass
    By kari4848 in forum Object Oriented Programming
    Replies: 5
    Last Post: April 29th, 2011, 01:12 PM