Show
Nội dung chính
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 super(); or super(theName); as in the code below. The actual parameters given to super() are used to initialize the inherited instance variables, for example the name instance variable in the Person superclass. public class Employee extends Person { public Employee() { super(); // calls the Person() constructor } public Employee(String theName) { super(theName); // calls Person(theName) constructor } } The super(theName) in the Employee constructor will call the constructor that takes a String object in the Person class to set the name. 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 public Person(). If a subclass has no call to a superclass constructor using super as the first line in a subclass constructor then the compiler will automatically add a super() call as the first line in a constructor. So, be sure to provide no-argument constructors in parent classes or be sure to use an explicit call to super() as the first line in the constructors of subclasses. 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 understanding
9-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 When you instantiate a subclass the superclass constructor will be also invoked True or False True False?Answer: Yes, this is absolutely true. Because of course without the superclass constructor invoked the global variables of tge super class and its function won't be able to work. When you instantiate a subclass the superclass constructor will be also executed?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. How superclass constructors are invoked from subclasses?To explicitly call the superclass constructor from the subclass constructor, we use super() . It's a special form of the super keyword. super() can be used only inside the subclass constructor and must be the first statement. When you create an object of a subclass the constructor of the superclass calls the constructor of the subclass True or false?(*2) A subclass constructor always calls the constructor of its superclass, and so on up to the Object constructor, to initialize all the aspects of the instance that the subclass inherits from its superclass. Which constructor is called first that of the subclass or the superclass?Subclass Constructors
Invocation of a superclass constructor must be the first line in the subclass constructor.
How superclass constructors are invoked from subclasses?To explicitly call the superclass constructor from the subclass constructor, we use super() . It's a special form of the super keyword. super() can be used only inside the subclass constructor and must be the first statement.
What happens if you use the superclass method in subclass?If you want the superclass constructor called with specific arguments, explicitly call the superclass constructor from the subclass constructor. The call to the superclass constructor must come before any other references to the object. In this class, the MySub object is initialized by the MySuperClass constructor.
When you use inheritance in Java you can create a new class that contains all the data and the methods of an existing class?When you use inheritance in Java, you can create a new class that contains all the data and methods of an existing class. A subclass inherits all the data and methods of its superclass, except the private ones.
|