Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#9.3.2. Inherited Get/Set Methods Inheritpleance means that an object of the child class automatically includes the object instance variables and methods defined in the parent

#9.3.2. Inherited Get/Set Methods

Inheritpleance means that an object of the child class automatically includes the object instance variables and methods defined in the parent class. But, if the inherited instance variables are private, which they should be, the child class can not directly access the them using dot notation. The child class can use public accessors (also called getters or get methods) which are methods that get instance variable values and public mutators (also called modifier methods or setters or set methods) which set their values.

For example, if a parent has a private instance variables, name, then the parent typically provides a public getName method and a public setName method as shown below. In the setName method below, the code checks if the passed string is null before it sets it and returns true if the set was successful or false otherwise. The Employee class inherits the name field but must use the public method getName and setName to access it.

Demonstrated inherited get/set methods.

please edit and fix this code for the problem. IN JAVA:

class Person

{

private String name;

public String getName()

{

return name;

}

public boolean setName(String theNewName)

{

if (theNewName != null)

{

this.name = theNewName;

return true;

}

return false;

}

}

public class Employee extends Person

{

private static int nextId = 1;

private int id;

public Employee()

{

id = nextId;

nextId++;

}

public int getId()

{

return id;

}

public static void main(String[] args)

{

Employee emp = new Employee();

emp.setName("Dina");

System.out.println(emp.getName());

System.out.println(emp.getId());

}

}

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

Students also viewed these Databases questions

Question

What do they need to do differently?

Answered: 1 week ago

Question

Trudy, when she first saw the bull pawing the ground, ran.

Answered: 1 week ago