Answered step by step
Verified Expert Solution
Question
1 Approved Answer
9.3. Inheritance and Constructors Coding Exercise The super(theName) in the Employee constructor will call the constructor that takes a String object in the Person class
9.3. Inheritance and Constructors Coding Exercise 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?
Given code:
Please answer the coding exercise question and fill in the code completely. Thank you!
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. Coding Exercise 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 \} public class Employee extends Person //Employee class inherited Person class \{ private static int nextId =1;// static variable nextId //to update each time private int id; // local variable "id" // private variable is used in same class only public Employee(String theName) //Employee class constructor with String obj \{ super(theName); //super() is used for constructors // by sub class or child class // to use super class or parent class constructor //here it is Person() id = nextId; // store nextId value in "id" nextId++; // each time update nextId for next process \} public int getId() // method to return present "id" value \{ return id; //returns "id" value \} public static void main(String[] args) //main methodStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started