Results 1 to 6 of 6
  1. #1
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default How can I overide the parent class constructor?

    This may be bad practice but I still want to learn how to do this.

    I would like to override my parent class constructor. But as this code turns out, Truck displays the parent class constructor.

    How can I accomplish overriding the parent class constructor? And if this is bad practice let me know, but I would still like to know if this is possible.


    Java Code:
    public class Vehicle {
         
          Vehicle() {
                System.out.println("I'm a vehicle");
          }
    }
    class Car extends Vehicle {
          Car() {
                super();
                System.out.println("I'm also a CAR.");
          }
    }
    class Truck extends Vehicle {
          Truck() {
                System.out.println("Please call me TRUCK only. Don't call me Vehicle!"); // 
          }
    }

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

    Default

    Quote Originally Posted by AcousticBruce View Post
    This may be bad practice but I still want to learn how to do this.

    I would like to override my parent class constructor. But as this code turns out, Truck displays the parent class constructor.

    How can I accomplish overriding the parent class constructor? And if this is bad practice let me know, but I would still like to know if this is possible.
    It's not "bad practice", rather it's impossible. Constructors are not virtual and can't be overridden. This makes sense because the object doesn't exist when the constructor is called, and so it can't be called on an object.

  3. #3
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    Quote Originally Posted by Fubarable View Post
    It's not "bad practice", rather it's impossible. Constructors are not virtual and can't be overridden. This makes sense because the object doesn't exist when the constructor is called, and so it can't be called on an object.
    Now that makes sense, thanks a lot Fubarable.

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

    Default

    Quote Originally Posted by AcousticBruce View Post
    Now that makes sense, thanks a lot Fubarable.
    Constructors should have code that is necessary to create the object and initialize its state, so I don't know why you'd want to not initialize the parent object. Is there an unmentioned objective behind your thread? Also, you can overload constructors, so if you want a different initialization for the parent, then you could use more than one constructor. Also, there are other ways to create objects that may be useful to you such as use of an abstract factory pattern, something I'm still learning about.

  5. #5
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    No objective. I was just trying things in the code to see what happens. It was a question that popped up in my head.

    Effective Java 2nd chapter starts talking about factory methods and it lost me almost instantly :D That was when I was first starting though.

  6. #6
    donepudi4u is offline Member
    Join Date
    Jan 2011
    Posts
    1
    Rep Power
    0

    Post constructor could not have "return" type....as the same time java does allow methods

    it is not possible to override the super class constructor ...because by default any constructor could not have "return" type....as the same time java does allow methods with out "return" types so it is impossible to override parent class constructor

Similar Threads

  1. substract Parent class object from child class
    By nikosv in forum New To Java
    Replies: 0
    Last Post: 12-08-2010, 12:30 AM
  2. Replies: 2
    Last Post: 05-01-2010, 08:59 AM
  3. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  4. Replies: 7
    Last Post: 05-06-2009, 12:42 PM
  5. Replies: 0
    Last Post: 12-19-2007, 09:10 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
  •