Answered step by step
Verified Expert Solution
Link Copied!

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?

image text in transcribed

Given code: image text in transcribed image text in transcribed image text in transcribed

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 method

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Beginning VB 2008 Databases

Authors: Vidya Vrat Agarwal, James Huddleston

1st Edition

1590599470, 978-1590599471

More Books

Students also viewed these Databases questions

Question

60 Workplace safety risks.

Answered: 1 week ago

Question

Explain budgetary Control

Answered: 1 week ago

Question

Solve the integral:

Answered: 1 week ago

Question

What is meant by Non-programmed decision?

Answered: 1 week ago

Question

What are the different techniques used in decision making?

Answered: 1 week ago