Does a subclass inherit the constructor?

  • Home
  • Interview Questions
  • J2EE
  • Java

You cannot inherit a constructor. That is, you cannot create a instance of a subclass using a constructor of one of it's superclasses. One of the main reasons is because you probably don't want to overide the superclasses constructor, which would be possible if they were inherited. By giving the developer the ability to override a superclasses constructor you would erode the encapsulation abilities of the language.

  • Interview Candidate
  • Aug 28th, 2004
  • 2
  • 13121

Java

Answer

  • First
  • Prev
  • Next
  • Last
  

Showing Answers 1 - 2 of 2 Answers

ishita

  • Jan 3rd, 2006

Constructors are not inherited. In other words, just because a base class has a constructor taking a certain list of parameters doesn't mean that a derived class has a constructor taking that list of parameters. (It can, by providing one itself, but it doesn't inherit it from the base class.)

manjunath shenoy

  • Jan 18th, 2006

no,the constructors cannot be inherited.the main reason for that is that we dont want to override the superclass constructor.Yes, asubclass can call its parent's class constructor.This is possible by using SUPER keyword in java.Through this keyword we can call the constructors of our choice(of parent class).egclass abc{abc(){}abc(int a){System.out.println(a+");}}class def extends abc{def(){super(10);System.out.println("hi");}}o/p 10 hi//this o/p will be generated when v create instances of class def.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Does a subclass inherit the constructor?

Does a subclass inherit the constructor?

Related Answered Questions

  •   Mainframe service vs initiator
  •   Object inheritance
  •   Class a ip address and sub-net mask
  •   API functionality testing
  •   Spliting the file in datastage
  •   Constructor use
  •   Create an anonymous PL/SQL block to enroll a student
  •   Return multiple values at a time from function
  •   How many students are there in the class?
  •   What is the output of the following program?

Related Open Questions

  •   Drop a table and its child tables from a database using procedure
  •   Memory of class member variable
  •   Telecom project explanation in data stage
  •   Subclass of component class
  •   Adding interface to multiple inheritance in C++ language
  •   Uploading data through bdc call transaction and session method
  •   How to launch 5 diff application from 5 child process
  •   What is meant by class space and object space (with regards to memory) in c++?
  •   Customer support job
  •   How do you deal and overcome major challenges?

9.2 Calling the Superclass Constructor

A subclass can have its own private data members, so a subclass can also have its own constructors.

The constructors of the subclass can initialize only the instance variables of the subclass. Thus, when a subclass object is instantiated the subclass object must also automatically execute one of the constructors of the superclass.

To call a superclass constructor the super keyword is used. The following example programs demonstrate use of super keyword.

Does a subclass inherit the constructor?

(Rectangle.java)

/**
 *  This class holds data of a Rectangle.
 */

public class Rectangle
{
   private double length;  // To hold length of rectangle
   private double width;  // To hold width of rectangle

   /**
    *  The constructor initialize rectangle's 
    *  length and width with default value    
    */

   public Rectangle()
   {
      length = 0;
      width = 0;
   }

   /**
    *  The constructor accepts the rectangle's  
    *  length and width.   
    */

   public Rectangle(double length, double width)
   {
      this.length = length;
      this.width = width;
   }

   /**
    *  The getArea method returns the area of 
    *  the rectangle.
    */

   public double getArea()
   {
      return length * width;
   }
}

(Box.java)

/**
 *  This class holds data of a Box.
 */

public class Box extends Rectangle
{
   private double height;  // To hold height of the box

   /**
    *  The constructor initialize box's 
    *  length, width and height with default value.  
    */

   public Box()
   {
      // Call the superclass default constructor to
      // initialize length and width.
      super();
      //Initialize height.
      height = 0;
   }

   /**
    *  The constructor accepts the box's  
    *  length, width and height.   
    */

    public Box(double length, double width, double height)
    {
      // Call the superclass constructor to
      // initialize length and width.
      super(length, width);
      
      // Initialize height.
      this.height = height;
   }

   /**
    *  The getVolume method returns the volume of 
    *  the box.
    */

   public double getVolume()
   {
      return getArea() * height;
   }
}

(BoxDemo.java)

/**
 * This program demonstrates calling
 * of superclass constructor.
 */

public class BoxDemo
{
   public static void main(String[] args)
   {
      // Create a box object.
      Box myBox1 = new Box();

      // Display the volume of myBox1.
      System.out.println("Volume: " + myBox1.getVolume());

      // Create a box object.
      Box myBox2 = new Box(12.2, 3.5, 2.0);

      // Display the volume of myBox2.
      System.out.println("Volume: " + myBox1.getVolume());
   }
}

Does a subclass need its own constructor?

Unfortunately, subclasses don't inherit the constructor from the superclass, so they need their own, either explicitly created or a default constructor created by Java. In order to use a superclass, the subclass constructor must call the parent constructor.

Does a subclass inherit constructors C++?

Subclasses inherit public methods from the superclass that they extend, but they cannot access the private instance variables of the superclass directly and must use the public accessor and mutator methods. And subclasses do not inherit constructors from the superclass.

Can constructors be inherited through a subclass that calls the superclass constructor?

Constructors are not inherited, you must create a new, identically prototyped constructor in the subclass that maps to its matching constructor in the superclass.

Which constructor Cannot be inherited?

A parent class constructor is not inherited in child class and this is why super() is added automatically in child class constructor if there is no explicit call to super or this.