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 4 of 4

Thread: Constructors and Inheritance in Java

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Constructors and Inheritance in Java

    Hi there,

    I'm going over access modifiers in Java from this website and noticed that the following output is displayed if you run the snippet of code.

    Cookie Class
    import javax.swing.*;
     
    public class Cookie {
     
    	public Cookie() {
    		System.out.println("Cookie constructor");
    	}
     
    	protected void foo() {
    		System.out.println("foo");
    	}
    }

    ChocolateChip class
    public class ChocolateChip extends Cookie {
     
    	public ChocolateChip() {
    		System.out.println("ChocolateChip constructor");
    	}
     
    	public static void main(String[] args) {
    		ChocolateChip x = new ChocolateChip();
    		x.foo();
    	}
    }

    Output:
    Cookie constructor
    ChocolateChip constructor
    foo

    I've been told that constructors are never inherited in Java, so why is "Cookie constructor" still in the output? I know that ChocolateChip extends Cookie, but the Cookie constructor isn't enacted when the new ChocolateChip object is defined... or is it?

    If you can shed any light on this, I would greatly appreciate it.

    Many thanks!


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Constructors and Inheritance in Java

    You are correct that constructors are not inherited. However, each constructor calls the super constructor all the way upto Object class (which doesn't call super as it is the top level class). If you do not insert a call to the super class constructor then the compiler will insert one implicitly. Therefore the constructor in the ChoclateChip class is actually:
    public ChocolateChip() {
        super();
        System.out.println("ChocolateChip constructor");
    }
    Improving the world one idiot at a time!

  3. #3
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Constructors and Inheritance in Java

    One more thing to note, due to this many people make the following mistake and wonder why their code will not compile.
    public class Cookie {
        public Cookie(String s) {
            System.out.println("Cookie constructor");
        }
     
        protected void foo() {
            System.out.println("foo");
        }
    }
     
    public class ChocolateChip extends Cookie {
        public ChocolateChip() {
            System.out.println("ChocolateChip constructor");
        }
    }
    The error is caused by the child class trying to call the default no arg constructor in the parent class but it does not exist.
    Improving the world one idiot at a time!

  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Constructors and Inheritance in Java

    Quote Originally Posted by Junky View Post
    You are correct that constructors are not inherited. However, each constructor calls the super constructor all the way upto Object class (which doesn't call super as it is the top level class). If you do not insert a call to the super class constructor then the compiler will insert one implicitly. Therefore the constructor in the ChoclateChip class is actually:
    public ChocolateChip() {
        super();
        System.out.println("ChocolateChip constructor");
    }
    Hi Junky,

    I didn't realize that the compiler can insert super() implicitly.

    Many thanks for your clarification and quick reply!

Similar Threads

  1. Need help with classes and constructors!!
    By rayp4993 in forum What's Wrong With My Code?
    Replies: 8
    Last Post: October 14th, 2011, 01:02 PM
  2. Constructors
    By av8 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: July 19th, 2011, 06:40 PM
  3. constructors in servlets
    By the light in forum JavaServer Pages: JSP & JSTL
    Replies: 4
    Last Post: June 29th, 2011, 10:32 AM
  4. [SOLVED] Overloading constructors(Multiple Constructors)
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 11th, 2011, 12:55 PM
  5. Java Inheritance Help
    By danielparry in forum Java Theory & Questions
    Replies: 3
    Last Post: March 17th, 2011, 03:20 PM