When we inherit class into another class then object of base class is initialized first. If a class do not have any constructor then default constructor will be called. But if we have created any parameterized constructor then we have to initialize base class constructor from derived class. Show
We have to call constructor from another constructor. It is also known as constructor chaining. When we have to call same class constructor from another constructor then we use this keyword. In addition, when we have to call base class constructor from derived class then we use base keyword. In the following example we have created Abc is as a base class and inherit it into Pqr. We have created parameterized constructor in class Abc so we have to initialize this constructor from derived class constructor in the following manner:- When we create the object of Pqr class then first it will call Pqr class constructor but Pqr class constructor first initialize the base class constructor then Pqr constructor will be initialized. It is very important point to note down the base class constructor initialized first. 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. So, how do you initialize the superclass’ private variables if you don’t have direct access to them in the subclass? In Java, the superclass constructor can be called from the first line of a subclass constructor by using the special keyword super() and passing appropriate parameters, for example public class Employee extends Person { public Employee() { super(); // calls the Person() constructor } public Employee(String theName) { super(theName); // calls Person(theName) constructor } }Coding Exercise The Try creating another Employee object in the main method that passes in your name and then use the get methods to print it out. Which class constructor sets the name? Which class constructor sets the id? If a class has no constructor in Java, the compiler will add a no-argument constructor. A no-argument constructor is one that doesn’t have any
parameters, for example If a subclass has no call to a superclass constructor using Regardless of whether the superclass constructor is called implicitly or explicitly, the process of calling superclass constructors continues until the Object constructor is called since every class inherits from the Object class. Check your understanding9-2-2: Given the class definitions of MPoint and NamedPoint below, which of the constructors that follow (labeled I, II, and III) would be valid in the NamedPoint class? class MPoint { private int myX; // coordinates private int myY; public MPoint( ) { myX = 0; myY = 0; } public MPoint(int a, int b) { myX = a; myY = b; } // ... other methods not shown } public class NamedPoint extends MPoint { private String myName; // constructors go here // ... other methods not shown } // Proposed constructors for this class: I. public NamedPoint() { myName = ""; } II. public NamedPoint(int d1, int d2, String name) { myX = d1; myY = d2; myName = name; } III. public NamedPoint(int d1, int d2, String name) { super(d1, d2); myName = name; } You can step through this code using the Java Visualizer by clicking the following link Named Point. 9.2.1. Programming Challenge : Square is-a Rectangle¶In this challenge, you are giving a class called Rectangle that has two instance variables, length and width, and two constructors that initialize them, and a method called draw() that uses nested loops to draw a length x width rectangle of stars. Try it out below. You will write a new class called Square that inherits from Rectangle. Is a square a rectangle? Yes! A square is a rectangle where the length and width are equal. Square will inherit length, width, and the draw method. You will write square constructors that will call the Rectangle constructors.
Create a Square class that inherits from Rectangle. For a more complex example of drawing shapes, try running this repl.it Java Swing code (or download the files here by clicking on Download on the top right and use the files in your own Java IDE). When the yellow panel comes up, click on either the Rectangle or the Oval button and then click and drag somewhere on the yellow panel to draw that shape. Take a look at the Rectangle.java and Oval.java files to see how they inherit from the Shape class in Shape.java. Java Swing graphical programming is not covered on the AP CS A exam, but it is a lot of fun! 9.2.2. Summary¶
You have attempted of activities on this page Which keyword would be used in a subclass to call the constructor in a parent class?Subclass Constructors
Invocation of a superclass constructor must be the first line in the subclass constructor. super();
Which of this keyword can be used in subclass to call the constructor of superclass?The super keyword refers to superclass (parent) objects. It is used to call superclass methods, and to access the superclass constructor.
Which of this keyword can be used in a sub class to call the constructor of super class A Super B This C extent D extends?The super keyword
It is used to invoke the superclass constructor from subclass.
Which keyboard can be used to call the constructor of a parent class?The super keyword can be used to invoke the parent class methods and constructors. It can also be used to access the fields of the parent class. The this keyword is used to represent the current instance of a class. It is used to access the instance variables and invoke current class methods and constructors.
|